简体   繁体   中英

How to show/hide items(?) in active view on maya with python?

Now I know how to hide all the nurbs curves on active viewport. But how can I do same thing for all the items on show menu on viewport such as cameras, manipulator, grid...etc at the same time?

I think that I need to use for loop for this but I need some guide. Thank you.

import maya.cmds as cmds

actView = cmds.getPanel(wf=True)

if cmds.modelEditor(actView, q=True, nurbsCurves=True) == 1:
    cmds.modelEditor(actView, e=True, nurbsCurves=False)

here is a method to hide everything :

actView = cmds.getPanel(wf=True) # actView='modelPanel4'
# list the flag we use to hide, not that alo hide nearly everything
hide_attrs = ['alo', 'manipulators', 'grid', 'hud', 'hos', 'sel']
# value is used to make visible or to hide (0 is for hiding)
value = 0
# flags is used to create a dictionary that will be used in the command to do : manipulators = 0
flags = { i : value for i in hide_attrs }
# the double star unpack a python dictionnary the key = value, i.e in this case : alo = 0, hud = 0....
cmds.modelEditor(actView, e=1, **flags)

if you want to be more specific, you can build another dictionnary for visible attrs

# merge dictionnaries
def merge_two_dicts(x, y):
    # In Python 3.5 or greater, : z = {**x, **y}
    # or w = {'foo': 'bar', 'baz': 'qux', **y}

    z = x.copy()  # start with x's keys and values
    z.update(y)  # modifies z with y's keys and values & returns None
    return z

actView = cmds.getPanel(wf=True) # actView='modelPanel4'
hide_attrs = ['alo', 'manipulators', 'grid', 'hud', 'hos']
vis_attrs = ['sel']
hide_flags = { i : 0 for i in hide_attrs }
vis_flags = { i : 1 for i in vis_attrs }
flags = merge_two_dicts(hide_flags, vis_flags)
cmds.modelEditor(actView, e=1, **flags)

but if i were you, I would just encapsulate this into a single def :

def set_actviewVis(value, attrs=list):
    actView = cmds.getPanel(wf=True) # actView='modelPanel4'    
    flags = { i : value for i in attrs }
    cmds.modelEditor(actView, e=1, **flags)

hide_attrs = ['alo', 'manipulators', 'grid', 'hud', 'hos']
set_actviewVis(0, hide_attrs)
vis_attrs = ['sel']
set_actviewVis(1, vis_attrs)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM