Python脚本将编译但不在maya中执行任何操作

时间:2014-01-06 04:59:55

标签: python scripting maya

我为一个工具编写了一段代码,当在maya中选择一个关节时,当选择关节时,用户按下工具界面上的一个按钮,它应该将关节重新命名为按钮的文字。代码在maya的脚本编辑器中编译,工具UI正确显示。但是当你选择一个关节然后按下jnt_L_toe按钮(唯一一个当前正在工作的按钮)时,联合名称不会被jnt_L_toe替换,我的问题是为什么?

以下是代码:

#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 个答案:

答案 0 :(得分:1)

每次看到的代码没有任何问题。这就是你所展示的代码之外的问题。你不能真正接受python片段并省略import语句,因为那些是故事的核心。您通常也应该发布报告的错误。

最可能的问题是您在函数中使用的命名空间与您体内的命名空间不同。请参阅您在主体中使用maya.cmds,它表示您已导入:

import maya.cmds

另一方面,该函数使用cmds表示:

import maya.cmds as cmd

到目前为止,这两种流行的惯例都没有真正意义。然而,很难说这是你真正缺失的问题。

另一个错误可以在:

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

这应该是:

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

有点神秘但确定。无论如何,我建议你将代码更改为:

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)