Maya script nodes and Python

So I’ve built a nice, shiny selector for my character rig, and everything works perfectly - except for one little detail. The selector code doesn’t survive a restart of Maya. That’s because when you restart Maya, it clears out all of the code that you’ve run from memory. I decided the best way to get it to work is to use a script node. I only want it for this one scene, and I only need for it to run once on loading the file.

The code runs perfectly if I copy-paste it into the script editor. However, if I put it into a script node that gets saved with the file, it starts giving me syntax errors. Now, my company doesn’t like for me to post source code, so before I do that, I want to eliminate any possible causes outside of my source code. So my questions are: does the Maya script node support Python, or is it MEL only? Do I need to do anything specific to the source code in order to get it to work? Am I going about this the wrong way altogether? I’m not married to using a script node, if I can get portable results another way (operative word being “portable”).

Thanks!

This should get you as scriptNode that acts on python syntax. Is that something you’ve already done?
cmds.scriptNode(sourceType='python')

Does that need to be done on creation of the script node? Because I think I tried setting it to python after I created it. I created the script node originally through the expression editor per Autodesk’s documentation (what? an error in Atuodesk’s docs?! Naaaaah . . .) The docs don’t mention Python, though.

I’ll test it and find out. Thanks!

Update: Ok, so I tested it and the script works when you load the file. It still errors out and complains when I hit the “Test Script” button in the expression editor. I’m not sure if it’s because it’s a Python script or what, but it does work in production.

After a bit more exploring, here’s what I found out. You can also select (and edit) the script by opening the Outliner, and turn off “show DAG obejcts only” and it will show up there. Select it and you can also adjust the script type from there. I just recreated it using a command. For anyone else who’s interested, heres the procedure I used.

First, make a Python string (in the script editor) that has your Python code. You can make a multi-line string by enclosing it in three single quotes:

codeToRun= '''
import maya.cmds as cmds

def myScript:
    print("hello tech-artists.org")

myScript()

'''

Then you call the command that R.White pointed out (and I’ll expand):

cmds.scriptNode(scriptType=2, beforeScript=codeToRun, name='myCoolScriptNode', sourceType='python')

. . . and DON’T trust the “Test Script” button. That was my main mistake. Also, don’t rely on Autodesk’s main script node docs. They don’t tell you much. Instead, here’s the link for the scriptNode command.

Thanks for pointing me in the right direction, R.White!

3 Likes

Thank you very much for this solution. I am trying create real time (Time change) evaluating script node,
that calculates up vector from position of joints during animation. Please could you help me, how to solve that evaluation of final position will not delay 1 frame, but it will correspond with input data (positions of joints) in exact time? I am sending example scene where is “script1” script node which does the job. On hand_l_ik_upv_ctrl is animation which moves the joints from which hand_l_ik_upv_ctrl_caculated is calculated. Thanks a lot for any advice.