Maya script nodes and Python

maya
python

#1

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!


#2

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


#3

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!