Python code function executes incorrectly

Python code function executes incorrectly

Anonymous
Not applicable
1,357 Views
3 Replies
Message 1 of 4

Python code function executes incorrectly

Anonymous
Not applicable

Hey, I hope this is the right place to post this question.

I have been working on this Python code to be able to get a hierarchy of joints and display their local rotation axis. Here is what happens: when the "Show Axis" button is pressed it selects the joint hierarchy but only the first joint has the LRA displayed, but when I press the button again the other joints also display their LRA.  It should be that all joints show the LRA when the button is pressed.

Here is the code:

from maya import cmds

 

window = cmds.window( title="Local Rotation Axis", iconName="LRA", widthHeight=(300, 55))
cmds.columnLayout( adjustableColumn=True)
cmds.button( label="Show Axis", c = "showAxis()") ## c = The function to be activated when the button is pressed.
cmds.button( label="Hide Axis", c = "hideAxis()")
cmds.showWindow ( window )

 

def showAxis(display=True):
    selectJoints = cmds.ls(sl=True)
    cmds.select(selectJoints, hi=True)
    for jnt in selectJoints:
        cmds.setAttr(jnt + ".displayLocalAxis", display)

 

def hideAxis(display=False):
    hideJoints = cmds.ls(sl=True)
    cmds.select(hideJoints, hi=True)
    for jnt in hideJoints:
        cmds.setAttr(jnt + ".displayLocalAxis", display)

 

Any ideas why this is happening? Any help would be appreciated!

Thanks

0 Likes
Accepted solutions (1)
1,358 Views
3 Replies
Replies (3)
Message 2 of 4

jmreinhart
Advisor
Advisor
selectJoints = cmds.ls(sl=True)

So you store the selection when you press the button

cmds.select(selectJoints, hi=True)

And then you change the selection to include the children. But the selectJoints variable still holds the original selection.

for jnt in selectJoints:
        cmds.setAttr(jnt + ".displayLocalAxis", display)

 So when you run this the first time it runs it on the one object you had selected. When you run it a second time you have all the objects selected so it works as expected.

Message 3 of 4

Anonymous
Not applicable

Yes, this exactly what happens, how do I change it? Is it a case of changing the cmds. on the variable selectJoints?

0 Likes
Message 4 of 4

jmreinhart
Advisor
Advisor
Accepted solution

Get the new selection after you run

the "select heirarchy" line and then use that new selection for your for loop.

0 Likes