[Python] Maya declaring variables are not defined, but all cases are correct

Discussão em 'Python' iniciado por Stack, Dezembro 21, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to write a Maya table that allows the user to put in inputs and a name, and then it will spawn an object with those attributes.

    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)


    I've ensured all the fields are filled out, including the name field. The expected result is that a cylinder is created with the respective attributes. However, the script brings up errors that the names are not defined.


    Error: NameError: file <maya console> line 9: name 'newtx' is not defined
    ​

    Continue reading...

Compartilhe esta Página