Romulus
Romulus

Reputation: 1260

Save Python variables with Maya scene

How can I save Python variables in a Maya scene?

For example I create a variable x in Maya's Script Editor and I save the scene. When I reopen Maya and reopen the saved scene, x variable doesn't exist anymore.

Is it possible?

Upvotes: 2

Views: 4585

Answers (2)

Rajiv Sharma
Rajiv Sharma

Reputation: 7162

Here is the simple example to saving a python variable inside maya file.

lets Save Variable X in Maya file and the value of X is 100.

and we dont want see this error:

# Error: NameError: name 'x' is not defined # 

To Save Variable in Maya file Execute this code before you save your Maya file.

import maya.cmds as cmds

var_node = cmds.scriptNode(scriptType=1, name='CustomVariable', beforeScript='python("x = 100")')
cmds.scriptNode( var_node, executeBefore=True )

open file again and execute:

print x

Upvotes: 0

joojaa
joojaa

Reputation: 4434

Yes, use a script node, use a optionVar or store the variable in a attribute.

In general

Scene persistence is supposed to be built with nodes. In fact everything in Maya is supposed to be a node or built out of a node tree. The bonus is if you make your computation a node then maya will automatically handle all the stuff for you. This is how render engines for example store the data. They register a special node and read the data from the node.

Needing to ask this question, "How can I save Python variables in a Maya scene?", is a indication that you indeed should have built a nodal solution. There are a few exceptions to this and those are related to general user GUI preferences that should be saved as optionVars.

Maya does not actually enforce that you do things sanely. You are free to do whatever you want, sane or not. So it is possible, tough a bit fishy, to use a scene save scriptjob to store the snapshot of your environment to a script node that auto runs on load. This is undesirable in the same way as using global variables is undesirable for code in general. It is also slightly unreliable, as a user can disable auto running of on load scriptNodes, for good reason.

About nodal solutions

Maya as a rule does not work the way most people intuitively expect at first glance. Just like the text you write in code is not what your computer generally executes, but rather the compiled code is what gets executed. The compiler has a bit of leeway on what i can do in this context and usually throws away some stuff. So in code its not really sane to think in terms of what the text looks like in code but rather what structures it builds.

In Maya the structures you should be building are nodes. There are two extra use cases outside this and those are:

  1. Exporters (Importers but due to their nature they are node bound because they target Maya)
  2. Graphical user interfaces that introspect Mayas current state or bring external info to the user

These two could only specialize in reading nodes. Mayas object auxiliary function nature can quite efficiently hide the fact from the user but this is essentially want your doing. Anything outside this scope does not benefit form using Maya. So whenever you use maya you want to capitalize on nodes. Think of it as if you are using a second language on top of your own programming language. This one is the actual language of Maya.

Let us start with a naive approach for randomizing point positions for a mesh (note i avoid the term object as Maya has no such concept):

import maya.cmds as cmds
import random

def randomize_points(scale):
    sel = cmds.polyListComponentConversion(ff=1, fe=1, fuv=1, fvf=1, tv=1)
    sel = cmds.ls(sel ,fl=1)
    for item in sel:
        cmds.move((random.random()-0.5)*scale,
                  (random.random()-0.5)*scale,
                  (random.random()-0.5)*scale, 
                  item, r=1)

It works, but has three deficiencies:

  1. You can not know what it looks like until you run it.

    You can undo tough so test and undo. Maybe youd need a seed variable for the random too?

  2. You need to build a GUI for this so the user can work it.
  3. There's is no way to specify the profile of the randomness.

    (This one is here so it easy for you to run the code i could use one of many pythons noise implementations. I will fix this nonetheless.)

The code is straightforward in sense id does what a user would do in the GUI. But what really happens? Where do the changes go? Simply put the answer is 2 fold they go either to the tweak array (most likely), or the objects position array. But there is a better way i can use Maya primitives to manipulate a similar behavior. Enter nodes.

First you need to go a bit node shopping, what nodes could actually provide a noise functionality? The thing is there does not seem to be many contenders here. There is despite this one node that's basically roll your own deformation and that is the particle node. So this is how you'd attack the problem with a particle node, some noise and a few attributes:

... to be continued ...

Upvotes: 4

Related Questions