cmds.eval always exceeds maximum recursion limit

cmds.eval always exceeds maximum recursion limit

Anonymous
Not applicable
2,940 Views
7 Replies
Message 1 of 8

cmds.eval always exceeds maximum recursion limit

Anonymous
Not applicable

Hey y'all! I've gotten stuck with a Python issue. evalDeferred is fine, but eval runs me into a maximum recursion limit error every time. Simple example from the script editor:

 

import sys
print sys.getrecursionlimit()
1000
cmds.eval('print \'hello\'')
# Error: RuntimeError: file C:\Program Files\Autodesk\Maya2018\Python\lib\site-packages\maya\app\commands.py line 19: maximum recursion depth exceeded while calling a Python object #

 

I've tried setting the recursion limit as high as a couple hundred thousand, and crashed without ever getting the script editor to echo 'hello'.

 

Is there a setting elsewhere I should look into? Fairly stumped. Thanks!

 

--John.

0 Likes
Accepted solutions (1)
2,941 Views
7 Replies
Replies (7)
Message 2 of 8

RFlannery1
Collaborator
Collaborator

To fix the problem, you probably want to use mel.eval.  For example:

from maya import mel
mel.eval('print "hello\\n";')

As for why the infinite recursion with cmds.eval is happening, I have no idea.  It seems weird to me that there is even something called "eval" in the "cmds" namespace.

0 Likes
Message 3 of 8

Anonymous
Not applicable

Thanks for the suggestion!

 

I have used mel.eval to get around the issue before, but this time I'm evaluating an external Python file. It's part of a game engine export script: bake bones, load and process post-bake external scripts to do special case tweaks to the baked bones as necessary, and finally export to FBX.

 

I was about to say I couldn't use mel.eval because the formatting of the Python file would be wrong, but on second thought it doesn't seem all that difficult to reformat it after getting the string via read(). I'll give it a try!

0 Likes
Message 4 of 8

RFlannery1
Collaborator
Collaborator

What exactly do you mean when you say "evaluating an external Python file"?

  • Executing a Python file outside of the current Maya environment, like on the command line or in a new instance of Maya?
  • Running code in a Python file that's not on the python path?
  • Something else entirely?

There may be other ways around this issue depending on what you're trying to do.

0 Likes
Message 5 of 8

Anonymous
Not applicable

I may not know the difference between running and evaluating. Here's the gist:

 

The main export script, the one that runs when I click a shelf button, handles the baking, deleting, and exporting that every animation requires. I want to add the ability to run special case scripts on animations that need something extra.

 

E.g. in the Mantle animation, the character hops up and grabs a ledge, then pulls itself up and over. My programmer wants the character to translate downward during the hop so that its fingers meet the ground at frame 8, and he will counter-animate it back up to support arbitrary ledge heights. I don't want that weird downward motion to exist in the scene but would rather take care of it at bake-time.

 

The main script does the baking, deleting, and so on. It then looks for an attribute on the root joint called postExport, whose string value is a filename. If it finds it, it loads and runs that script. In this case, postExport_Mantle.py adds a layer over the baked animation, then adds the translation my programmer wants to see. Once postExport_Mantle.py is completed, the main script carries on with the export to FBX.

 

Basically, I want to run special case scripts inside a more generic script. Does that make sense?

 

0 Likes
Message 6 of 8

RFlannery1
Collaborator
Collaborator

Ah, okay.  Now I see what you are trying to do.  I think Python's built-in "execfile" function might work.  Something like:

if cmds.attributeQuery('postExport', node='rootJoint', exists=True):
    postExportScriptPath = cmds.getAttr('rootJoint.postExport')
    execfile(postExportScriptPath)
Message 7 of 8

Anonymous
Not applicable
Accepted solution

Hey, that's done it! I couldn't get execFile to work, but you lead me here:

 

 

with open(pathToScriptFile) as f:
    script = compile(f.read(), pathToScriptFile, 'exec')
    exec(script)

 

Thanks for the help! I feel like a scripting level-up.

 

Message 8 of 8

RFlannery1
Collaborator
Collaborator

Awesome!  Glad you got it working.  Teamwork for the win!

0 Likes