Python脚本将在5月份编译,但不执行任何操作

2024-04-29 03:44:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我为一个工具编写了一段代码,当在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')`

Tags: thetextnamebuttonpasslabelcommandtoe
1条回答
网友
1楼 · 发布于 2024-04-29 03:44:18

代码没什么问题。问题出在你展示的代码之外。你不能真的接受python代码片段而忽略import语句,因为这些是故事的中心内容。你也应该在一般情况下公布报告的错误。在

最有可能的问题是,在函数中使用的名称空间与在主体中使用的名称空间不同。看你用的玛雅.cmds在主体中,将指示您已导入:

import maya.cmds

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

^{pr2}$

到目前为止,这是一个普遍的惯例,两者兼而有之,毫无意义。然而,很难说你真正缺少的问题。在

可以在以下错误中找到另一个错误:

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)

相关问题 更多 >