in Maya Autodesk>>trouble with Exec ( ) with the error "name is not defined"

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
drewdavis
User
Beiträge: 1
Registriert: Freitag 12. Juli 2024, 09:51

hi, this is the function run() >>

Code: Alles auswählen

def run ():
    driver = cmds.textField('cat_driver_tf', q=1, tx=1 )
    driven = cmds.textField('cat_driven_tf', q=1, tx=1 )
    toExec =  ( "['{}']".format(driven) )
    toExec =  toExec.replace( ", ", "', '" )
    print( toExec)
    exec( 'drivens= {}'.format(toExec) )
    for i in drivens:
        print (i)
        #cmds.connectAttr (driver, i)
It is supposed to give me each individual objects in the list called toExec, something like >>

Code: Alles auswählen

toExec = ['pSphere1.rz', 'pSphere2.rz', 'pSphere4.rz']
I did >>

Code: Alles auswählen

exec( 'drivens= {}'.format(toExec) )
before i wanted to test out with >>

Code: Alles auswählen

    for i in drivens:
        print (i)
It's giving me error "py line 72: name 'drivens' is not defined."

The tutorial i am following had no issue whatsoever with this. What am I doing wrong? Any help would be appreciated. Thanks.
Sirius3
User
Beiträge: 18252
Registriert: Sonntag 21. Oktober 2012, 17:20

You should not use `exec` in any situation. In your function, `drivens` is a local variable and you cannot set local variables with exec.
If you have a string with ‹, ›-separated parts, you can simply use split:

Code: Alles auswählen

def run():
    driver = cmds.textField('cat_driver_tf', q=1, tx=1)
    driven = cmds.textField('cat_driven_tf', q=1, tx=1)
    drivens = driven.split(", ")
    for driven in drivens:
        print(driven)
        # cmds.connectAttr(driver, driven)
Antworten