【问题标题】:Maya: Python script works in some scenes, but not in othersMaya:Python 脚本在某些场景中有效,但在其他场景中无效
【发布时间】:2021-10-30 01:38:17
【问题描述】:

我可以找到另一个专门解决此问题的地方,但它似乎更加个人化,the other thread 没有提供任何帮助。这是错误:

# Error: Maya command error
# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
#   File "<maya console>", line 26, in createJoints
# RuntimeError: Maya command error # 

下面是代码,沿预制曲线创建关节链:

import maya.cmds as mc

##UI##

jointCurve = mc.window(title = "Create Joint Chain") #window title#
mc.rowColumnLayout(nc = 2) #divides window into two columns#
mc.text(l = "Number of Joints") #dialog prompt#
jntAmount = mc.intField (v = 5, min = 2) #default value of 5, minimum value of 2 joints#
mc.button(l = "Create", w = 150, c = "createJoints()") #confirms entry#
mc.button(l = "Cancel", w = 150, c = "mc.deleteUI(jointCurve)") #closes window, deletes UI#
mc.showWindow() #now that all parameters are defined, the window is opened#

##Method##

def createJoints():
    #Gathering Information#
    selCurve = mc.ls(sl = True) [0]
    jointAmount = mc.intField(jntAmount, q = True, v = True)
    prevJnt = ""
    rootJnt = ""
    
    for i in range(0, jointAmount): #for each increment from 0 to the amount defined in the field,#
        mc.select(cl = True) #clear the selection,#
        newJnt = mc.joint() #create a joint,#
        motionPath = mc.pathAnimation(newJnt, c = selCurve, fractionMode = True) #affix to curve using a motion path,#
        mc.cutKey(motionPath + ".u", time = ()) #remove animation keys from joint's U value (position on curve),#
        mc.setAttr(motionPath + ".u", i * (1.0/(jointAmount - 1))) #distribute joints evenly along curve#
        #delete motion path#
        mc.delete(newJnt + ".tx", icn = True)#deletes x translate value's input connections#
        mc.delete(newJnt + ".ty", icn = True)#deletes y translate value's input connections#
        mc.delete(newJnt + ".tz", icn = True)#deletes z translate value's input connections#
        mc.delete(motionPath) #deletes motion path itself#
        
        if i == 0:
            prevJnt = newJnt
            rootJnt = newJnt
            continue #skips next instructions for root joint since it has no parent#
        
        mc.parent(newJnt, prevJnt) #parents current joint to previous joint#
        prevJnt = newJnt #sets status of "previous" joint to current joint#
    mc.joint(rootJnt, e = True, oj = "xyz", sao = "yup", ch = True, zso = True) #orients joints along chain#
    mc.deleteUI(jointCurve) #closes prompt window#

    

每当我在新文件中运行代码时,它都能完美运行,没有问题。

但是我有另一个文件,当我运行此代码时,它会引发上述错误。

所以看起来它在导入 Maya 命令时遇到了问题? 以及这一行,特别是:

mc.cutKey(motionPath + ".u", time = ())

由于某种原因,即使我启动一个新文件并将另一个文件导入到场景中,也会引发此错误。

我完全不知道从哪里开始寻找..

【问题讨论】:

    标签: python maya pymel


    【解决方案1】:

    问题是你的用户界面在你的命令创建之前

    import maya.cmds as mc
    from functools import partial
    
    ##Method##
    
    def createJoints(*args):
        #Gathering Information#
        selCurve = mc.ls(sl = True) [0]
        jointAmount = mc.intField(jntAmount, q = True, v = True)
        prevJnt = ""
        rootJnt = ""
        
        for i in range(0, jointAmount): #for each increment from 0 to the amount defined in the field,#
            mc.select(cl = True) #clear the selection,#
            newJnt = mc.joint() #create a joint,#
            motionPath = mc.pathAnimation(newJnt, c = selCurve, fractionMode = True) #affix to curve using a motion path,#
            mc.cutKey(motionPath + ".u", time = ()) #remove animation keys from joint's U value (position on curve),#
            mc.setAttr(motionPath + ".u", i * (1.0/(jointAmount - 1))) #distribute joints evenly along curve#
            #delete motion path#
            mc.delete(newJnt + ".tx", icn = True)#deletes x translate value's input connections#
            mc.delete(newJnt + ".ty", icn = True)#deletes y translate value's input connections#
            mc.delete(newJnt + ".tz", icn = True)#deletes z translate value's input connections#
            mc.delete(motionPath) #deletes motion path itself#
            
            if i == 0:
                prevJnt = newJnt
                rootJnt = newJnt
                continue #skips next instructions for root joint since it has no parent#
            
            mc.parent(newJnt, prevJnt) #parents current joint to previous joint#
            prevJnt = newJnt #sets status of "previous" joint to current joint#
        mc.joint(rootJnt, e = True, oj = "xyz", sao = "yup", ch = True, zso = True) #orients joints along chain#
        mc.deleteUI(jointCurve) #closes prompt window#
    
    
    def cancelUI(jointCurve, *args):
        mc.deleteUI(jointCurve)
        
    ##UI##
    
    jointCurve = mc.window(title = "Create Joint Chain") #window title#
    mc.rowColumnLayout(nc = 2) #divides window into two columns#
    mc.text(l = "Number of Joints") #dialog prompt#
    jntAmount = mc.intField (v = 5, min = 2) #default value of 5, minimum value of 2 joints#
    mc.button(l = "Create", w = 150, c = createJoints) #confirms entry#
    mc.button(l = "Cancel", w = 150, c = partial(cancelUI, jointCurve)) #closes window, deletes UI#
    mc.showWindow() #now that all parameters are defined, the window is opened#
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-05
      • 2020-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多