Seite 1 von 1

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

Verfasst: Donnerstag 18. Juli 2024, 11:54
von drewdavis
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.

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

Verfasst: Sonntag 21. Juli 2024, 18:10
von Sirius3
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)