Using items in the list as keyword arguments

Anonymous

Using items in the list as keyword arguments

Anonymous
Not applicable

Hi, I wanted to re-iterate a check for nurbs, joints, motiontrails and locators in a viewport and if they are visible, I want maya to turn it off . Here is what I did,

 

import maya.cmds as cmds

#check viewport
panelnow=cmds.getPanel(wf=True)
#items to hide in a list
thingstoHide = ['nurbsCurves', 'joints','motionTrails','locators']
# for loop to check through all 4 items on top. 
for i in range(4):
    print thingstoHide[i]
    thingCheck = cmds.modelEditor(panelnow, query=True, thingstoHide[i]=True)
    if thingCheck == True :
        cmds.modelEditor(panelnow,edit=True, thingstoHide[i]=False)
    else :
        print "%s is already hidden " % thingstoHide[i]

But it seems impossible to use a list for keyword arguments is it  true? am i getting anything wrong? or is there a better way to do this? 

 

0 Likes
Reply
Accepted solutions (1)
657 Views
2 Replies
Replies (2)

rflannery
Collaborator
Collaborator
Accepted solution

You cannot use a list for keyword arguments, but you can use a dictionary.  Put all the keywords and their values in the dictionary.  When calling the function, use the ** syntax to unpack all the dictionary entries.  (The ** syntax can be a bit confusing.  Here is a page that may help: https://stackoverflow.com/questions/3394835/args-and-kwargs)

 

Here is your code modified to use a dictionary.  (Also, I changed the loop to iterate over the items directly.  In Python you don't need to access each item through an index; the for-loop will give you each item in order.)

import maya.cmds as cmds

#check viewport
panelnow=cmds.getPanel(wf=True)
#items to hide in a list
thingstoHide = ['nurbsCurves', 'joints','motionTrails','locators']
# for loop to check through all 4 items on top. 
for thing in thingsToHide::
    print thing
kwargs = {'query': True, thing: True} thingCheck = cmds.modelEditor(panelnow, **kwargs) if thingCheck == True :
kwargs = {'edit': True, thing: False} cmds.modelEditor(panelnow, **kwargs) else : print "%s is already hidden " % thing

Anonymous
Not applicable

Awesome answer. Thanks =D 

0 Likes