Maya 声明变量未定义,但所有情况都是正确的

问题描述 投票:0回答:1

我正在尝试编写一个 Maya 表,允许用户输入输入和名称,然后它将生成一个具有这些属性的对象。

import maya.cmds as cmds

def spawn_cylinder(*args):
    newobject = cmds.polyCylinder(n="newobject")
    get_attributes()
    apply_attributes()

def get_attributes(*args):
    newtx = cmds.floatField("new_x", q=True, v=True)
    newty = cmds.floatField("new_y", q=True, v=True)
    newtz = cmds.floatField("new_z", q=True, v=True)
    
    newrx = cmds.floatField("new_rx", q=True, v=True)
    newry = cmds.floatField("new_ry", q=True, v=True)
    newrz = cmds.floatField("new_rz", q=True, v=True)
    
    newsx = cmds.floatField("new_sx", q=True, v=True)
    newsy = cmds.floatField("new_sy", q=True, v=True)
    newsz = cmds.floatField("new_sz", q=True, v=True)
    
    newname = cmds.textField("renamefield", q=True, tx=True)

def apply_attributes(*args):
    for axis in ["x","y","z"]:
        cmds.setAttr("newobject" + ".translateX", newtx)
        cmds.setAttr("newobject" + ".translateY", newty)
        cmds.setAttr("newobject" + ".translateZ", newtz)
        
        cmds.setAttr("newobject" + ".rotateX", newrx)
        cmds.setAttr("newobject" + ".rotateY", newry)
        cmds.setAttr("newobject" + ".rotateZ", newrz)
        
        cmds.setAttr("newobject" + ".scaleX", newsx)
        cmds.setAttr("newobject" + ".scaleY", newsy)
        cmds.setAttr("newobject" + ".scaleZ", newsz)
    
    for object in cmds.ls("newobject"):
        cmds.rename(newname)

cmds.floatField("new_x", w=60, v=0)
cmds.floatField("new_y", w=60, v=0)
cmds.floatField("new_z", w=60, v=0)

cmds.floatField("new_rx", w=60, v=0)
cmds.floatField("new_ry", w=60, v=0)
cmds.floatField("new_rz", w=60, v=0)

cmds.floatField("new_sx", w=60, v=1)
cmds.floatField("new_sy", w=60, v=1)
cmds.floatField("new_sz", w=60, v=1)

cmds.textField("renamefield", w=200)

cmds.button(l="Cylinder", w=60,h=50,c=spawn_cylinder)

我已确保填写所有字段,包括姓名字段。预期结果是创建具有相应属性的圆柱体。但是,该脚本会出现名称未定义的错误。

错误:NameError:文件

<maya console>
第9行:名称“newtx”未定义

python variables maya
1个回答
0
投票

我修改了您的代码并添加了布局,所以现在它可以正常工作了。执行时,会出现一个 Maya 窗口,其中包含

translate
rotate
scale
name
参数。这就是它的样子。

from maya import cmds

class CylinderClass():
    def __init__(self):
        self.window = cmds.window(t="Wizard", wh=(250,320))
        cmds.columnLayout()
        # Moving tx
        cmds.rowLayout(numberOfColumns=2)
        cmds.text(label='tx:')
        self.tx = cmds.floatField(pre=1)
        cmds.setParent('..')
        # Moving ty
        cmds.rowLayout(numberOfColumns=2)
        cmds.text(label='ty:')
        self.ty = cmds.floatField(pre=1)
        cmds.setParent('..')
        # Moving tz
        cmds.rowLayout(numberOfColumns=2)
        cmds.text(label='tz:')
        self.tz = cmds.floatField(pre=1)
        cmds.setParent('..')
        
        # Rotating rx
        cmds.rowLayout(numberOfColumns=2)
        cmds.text(label='rx:')
        self.rx = cmds.floatField(pre=1, min=-360, max=360)
        cmds.setParent('..')
        # Rotating ry
        cmds.rowLayout(numberOfColumns=2)
        cmds.text(label='ry:')
        self.ry = cmds.floatField(pre=1, min=-360, max=360)
        cmds.setParent('..')
        # Rotating rz
        cmds.rowLayout(numberOfColumns=2)
        cmds.text(label='rz:')
        self.rz = cmds.floatField(pre=1, min=-360, max=360)
        cmds.setParent('..')
        
        # Scaling sx
        cmds.rowLayout(numberOfColumns=2)
        cmds.text(label='sx:')
        self.sx = cmds.floatField(pre=1, v=1)
        cmds.setParent('..')
        # Scaling sy
        cmds.rowLayout(numberOfColumns=2)
        cmds.text(label='sy:')
        self.sy = cmds.floatField(pre=1, v=1)
        cmds.setParent('..')
        # Scaling sz
        cmds.rowLayout(numberOfColumns=2)
        cmds.text(label='sz:')
        self.sz = cmds.floatField(pre=1, v=1)
        cmds.setParent('..')
        
        # Naming
        cmds.rowLayout(numberOfColumns=2)
        cmds.text(label='nm:')
        self.txt = 'name'
        self.name = cmds.textField(text=self.txt, w=150)
        cmds.setParent('..')
        # Separator
        cmds.separator(style='double', height=30)

        # Button
        cmds.button(label="Create Cylinder", command=self.makeCylinder)
        cmds.showWindow(self.window)
        
    def makeCylinder(self, *args):
        qtx = cmds.floatField(self.tx, q=True, v=True)
        qty = cmds.floatField(self.ty, q=True, v=True)
        qtz = cmds.floatField(self.tz, q=True, v=True)
        qrx = cmds.floatField(self.rx, q=True, v=True)
        qry = cmds.floatField(self.ry, q=True, v=True)
        qrz = cmds.floatField(self.rz, q=True, v=True)
        qsx = cmds.floatField(self.sx, q=True, v=True)
        qsy = cmds.floatField(self.sy, q=True, v=True)
        qsz = cmds.floatField(self.sz, q=True, v=True)
        qnm = cmds.textField(self.name, q=True, text=True)
        
        cmds.polyCylinder(n=qnm)
        cmds.move(qtx, qty, qtz)
        cmds.rotate(qrx, qry, qrz)
        cmds.scale(qsx, qsy, qsz)
      
CylinderClass()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.