I want to run some python code (function) as soon as I open .ma (maya ascii) file. So can i manually edit ma file and write some python code in it. so my question is how do I "Embed" python code in .MA file. MEL also fine. some mel code works, but not every. i don,t understand why it happens.
-
1Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. StackOverflow is not a coding or tutorial service. – Prune Jun 30 '17 at 16:39
-
In the maya context this is actually not as minimal a question as it seems. – theodox Jul 02 '17 at 02:39
1 Answers
You should not be editing the files manually to do what you're trying -- MA files look like Mel but they aren't exactly the same, and you could easily break your scene with a bad edit.
The standard way to embed script functionality in a Maya file is with a script node. For the application you're looking at you want to create a script node and set it up to run when the file is opened in Maya.
The script node is created with cmds.scriptNode()
. The node has two string attributes, .before
and .after
; you set one of these to the text of the script you want to execute. The .scriptType
attribute is an enum which tells Maya when to execute the script and the .sourceType
indicates whether to use Mel or Python. To run a script when the file is opened in Maya, you typically use .scriptType
of 2, which is "On GUI open/close" and the .before
attribute (which, a bit confusingly, is "beforeScript" in the command flags)
So, for example, to print "hello world" when the scene is opened:
sn = cmds.scriptNode(beforeScript = "print('hello world')")
cmds.setAttr(sn + '.scriptType', 2) # gui open/close
cmds.setAttr(sn + '.sourceType', 1) # python
save the file and re-open, it will print 'hello world' to the listener. You don't have any control over the environment in which your file may be opened, so you should be careful about making assumptions about available imports and so on. The script will execute in the global scope, as if typed in the listener.
update - packaging
The comments indicate that OP wants to put more complex scripts into the node. Generally maya will execute the string as if you'd run exec
on on the same string in Python. Exec works for multiline strings, but you have to format them correctly with newlines and tabs if you use tabs and spaces if not. You'll also need to escape your quotes correctly. For short scripts just use triple-single quotes to escape the whole thing:
script = '''
def example():
for n in range (100):
print ("hello world")
example()
'''
import maya.cmds as cmds
sn = cmds.scriptNode(beforeScript = script)
cmds.setAttr(sn + '.scriptType', 2)
cmds.setAttr(sn + '.sourceType', 1)
Often it's easier to just read a script from disk and store it like this:
def create_script_node_from_file(filename):
with open('filename', 'rt') as handle:
script_text = handle.read()
sn = cmds.scriptNode(beforeScript = script_text)
cmds.setAttr(sn + '.scriptType', 2) # gui open/close
cmds.setAttr(sn + '.sourceType', 1) # python
You probably don't want to do anything very intrusive or elaborate in a script node because people opening the file in the future may only want to inspect it and not get deep into whatever UI you are presenting. You may also make it harder to run batch processes on the file.

- 12,028
- 3
- 23
- 36
-
Yeah, tried... it works for one line commands... lets say i have a window ui, which i want to open with file. (it can be saved to scripts folder, but import command also not working with script node) Lets say i have following code saved in "test.py" in scripts directory. – Theprash Jul 03 '17 at 17:59
-
`import maya.cmds as cmds cmds.window(title = "New Window") cmds.columnLayout( "columnName01", adjustableColumn = True) cmds.button(label = "click me", parent = "columnName01", command = 'cmds.sphere()') cmds.columnLayout( "columnName02", columnAttach=('both', 5), rowSpacing=10, columnWidth=250) cmds.button(label = "make sphere") cmds.showWindow()` – Theprash Jul 03 '17 at 18:03
-
sorry i'm not able to preserve formatting here... but I'm just using example code from here -- http://www.bojandjulbic.com/code-snippets-creating-gui-using-python-maya/ (02.Layout) what if i want to create this window as soon as file opens.note: i want to save it in file. at least execution command. above code can be stored in scripts directory. – Theprash Jul 03 '17 at 18:08
-
@Theprash there is a alternative and that is to implement your gui as a panel thsiway it is persistent in the file. – joojaa Jul 04 '17 at 09:09
-
Wow, man worked perfectly!! thank you very much. There is not enough documentation available.. thank god you helped me out. Cheers!!! – Theprash Jul 04 '17 at 20:56