1

I've written a piece of code for a tool that should, when a joint is selected in maya and whilst the joint is selected the user presses a button on the interface of the tool it should re-name the joint to the text of the button. The code compiles in maya's script editor and the tool UI appears correctly. However when you select a joint then press on the jnt_L_toe button (the only one that should be currently working), the joint name is not replaced with jnt_L_toe, and my question is why?

Here is the code:

#Global variable contains all joints in model joints_list = maya.cmds.ls(type="joint") #Variable names Ltoe = "jnt_L_toe" # create the window wnd_name = maya.cmds.window(title="Rename-A-Joint", widthHeight=[300, 500]) # create the layout maya.cmds.rowColumnLayout(numberOfColumns = 2, rowSpacing=[(1,5), (2,5)], columnWidth=[(1,120),(2,120)] ) maya.cmds.text(label="Please select a \n joint then one\n of the following\n buttons to rename it:", font = "boldLabelFont") maya.cmds.text(label=" \n \n ", font = "boldLabelFont") # create the controls maya.cmds.text(label="Legs", font = "boldLabelFont") maya.cmds.text(label="Hands", font = "boldLabelFont") maya.cmds.button(label="jnt_L_toe", command="renameJoint(Ltoe)") maya.cmds.button(label="jnt_L_thumb1", command="pass") maya.cmds.button(label="jnt_L_ball", command="pass") maya.cmds.button(label="jnt_L_thumb2", command="pass") maya.cmds.button(label="jnt_L_ankle", command="pass") maya.cmds.button(label="jnt_L_thumb3", command="pass") maya.cmds.button(label="jnt_L_knee", command="pass") maya.cmds.button(label="jnt_L_thumb4", command="pass") maya.cmds.button(label="jnt_L_thigh", command="pass") maya.cmds.button(label="jnt_L_thumb5", command="pass") maya.cmds.text(label="Arms", font = "boldLabelFont") maya.cmds.button(label="jnt_L_index1", command="pass") maya.cmds.button(label="jnt_L_clavicle", command="pass") maya.cmds.button(label="jnt_L_index2", command="pass") maya.cmds.button(label="jnt_L_shoulder", command="pass") maya.cmds.button(label="jnt_L_index3", command="pass") maya.cmds.button(label="jnt_L_elbow", command="pass") maya.cmds.button(label="jnt_L_index4", command="pass") maya.cmds.button(label="jnt_L_forearm", command="pass") maya.cmds.button(label="jnt_L_middle1", command="pass") maya.cmds.button(label="jnt_L_wrist", command="pass") maya.cmds.button(label="jnt_L_middle2", command="pass") maya.cmds.text(label="") maya.cmds.button(label="jnt_L_middle3", command="pass") maya.cmds.text(label="") maya.cmds.button(label="jnt_L_middle4", command="pass") maya.cmds.text(label="") maya.cmds.button(label="jnt_L_ring1", command="pass") maya.cmds.text(label="") maya.cmds.button(label="jnt_L_ring2", command="pass") maya.cmds.text(label="") maya.cmds.button(label="jnt_L_ring3", command="pass") maya.cmds.text(label="") maya.cmds.button(label="jnt_L_ring4", command="pass") maya.cmds.text(label="") maya.cmds.button(label="jnt_L_pinky1", command="pass") maya.cmds.text(label="") maya.cmds.button(label="jnt_L_pinky2", command="pass") maya.cmds.text(label="") maya.cmds.button(label="jnt_L_pinky3", command="pass") maya.cmds.text(label="") maya.cmds.button(label="jnt_L_pinky4", command="pass") # show the window maya.cmds.showWindow(wnd_name) #Function to change name of joint def renameJoint(name): currentjoint = cmds.ls(type = "joint", selection=True) for connect in joints_list: if(connect == currentjoint): cmds.rename(connect, 'name')` 

1 Answer 1

1

There is nothing wrong with the code per see. That is the problem is outside the code you show. You can not really take python snippets and omit the import statements as those are the central meat of the story. Also you should in general post the reported error.

The most likely issue is that you use different namespace in the function than in your body. See you use maya.cmds in the main body which would indicate you have imported:

import maya.cmds 

On the other hand the function uses cmds which indicates:

import maya.cmds as cmd 

Which is by far the prevalent convention doing both makes no real sense. However its hard to say as your really missing problem.

Another error can be found in:

def renameJoint(name): currentjoint = cmds.ls(type = "joint", selection=True) for connect in joints_list: if(connect == currentjoint): cmds.rename(connect, 'name')` 

This should probably be:

def renameJoint(name): currentjoint = cmds.ls(type = "joint", selection=True) for connect in joints_list: if(connect == currentjoint[0]): cmds.rename(connect, name) 

Bit mysterious but sure. Anyway, I would suggest you change your code to something like:

import maya.cmds as cmds def renameJoint(name): currentjoint = cmds.ls(type = "joint", selection=True) if currentjoint[0] in joints_list: cmds.rename(currentjoint[0], name) def multipleButtonGrp(title,lst): cmds.text(label=title, font = "boldLabelFont") cmds.text(label="") for item in lst: cmds.button(item, label=item, command="renameJoint('%s')"%item) joints_list = maya.cmds.ls(type="joint") wnd_name = cmds.window(title="Rename-A-Joint", widthHeight=[300, 500]) cmds.rowColumnLayout(numberOfColumns = 2) #add your options multipleButtonGrp("Hands", ["jnt_L_toe", "jnt_L_thumb1", "jnt_L_ball", "jnt_L_thumb2", "jnt_L_ankle", "jnt_L_thumb3", "jnt_L_knee", "jnt_L_thumb4", "jnt_L_thigh", "jnt_L_thumb5"]) cmds.showWindow(wnd_name) 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for providing a solution. Yes as you inferred plain old import maya.cmds was used at the start (which I forgot to include in the code). However the whole script still ran without a glitch save the renameJoint function where the line if(connect==currentjoint) was the thing that was hindering it. You have obviously provided a more eloquent solution for the UI. The only problem is blank buttons shouldn't be appearing for the column after legs and arms - so I guess a check is neccessary. At any rate thanks again - I don't have 15 rep otherwise I'd have voted up the answer a few times :)
No but you can accept the answer (the checkmark) and than youll have 15 points. Besides now you do :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.