PDA

View Full Version : Global Variables in Python2.6.1 for Maya -Help Needed!



semiprowolverine
July 6th, 2012, 12:08 PM
After going through every possible resources I came to conclusion that I need someone to enlighten me on passing global variables from a function to others inside a Module in Python.


Actually I am writing a small UI in Autodesk Maya2010, here is a brief about the problem.


In the following code, I have got two Module Level Global Variables. I am assigning values to them inside a function. Now if I pass these variables directly(i.e. without converting the function call and assigning it as a string) then the code works fine, however since the Command Flag of the Button Function only allows a string or a function name hence I am stuck with this way of calling the function.
The weird thing about this is when I pass only one argument then it gets read in the called function, however with two or more arguments it is using only Module Level Global variables.


The result which I get is :


temp_var=None
a_Window=None

which I have got no idea about.


Can anyone point me in the right direction about how to deal with this issue and can someone tell me what exactly is happening when I use the string value as function call?

Here is the Sample Code:


import maya.cmds;
temp_var=None;
a_Window=None;
def im_PrimaryFunc():
imSecondary_func();


def imSecondary_func():
global a_Window;
global temp_var;
a_Window=maya.cmds.window(title="Something");
a_layout=maya.cmds.columnLayout(adj=1,rs=10);
temp_var=maya.cmds.createNode("polySphere");
func_call="a_calledFunc(a_Window,temp_var)";
maya.cmds.button(label="call_aFunc",align="center",command=func_call);
maya.cmds.showWindow(a_Window);


def a_calledFunc(arg00,arg01):
print(arg00);
print(arg01);
im_PrimaryFunc();



Thanks,

niceguysan
August 12th, 2012, 03:47 AM
you are doing it wrong, to have aglobal variable all you have to do is to declare them outside the block so that they are available to all functions, if you put them inside a function, their scope stays inside the function so they are never global.

what you can do is have a variable declared outside and the function that declares your variable can return them in form of list and you set

myvar1=variableFunc()[0] # which will give you a variable with some value passed to it inside the function

this still means the variable inside the function declared is not global however you can access the data this way...from the function.

semiprowolverine
August 15th, 2012, 04:04 AM
Hello San,
As I mentioned earlier this script when saved and then imported as a module was not able to print out the desired object names, I apologize if I was not clear at first instant. The script works fine if one will execute this in the Maya script editor, however it does not print out the window and the polygon objects on the screen when imported as module.
Okay, I figured out later that this was due to the fact that temp_var and a_window variables were not getting updated in the global namespace, as you mentioned that their scope remains local to that function.

A weird case though was, when I called a function with a single argument it got printed ok.


Anyways, with all that said and done, Achayan (http://www.kurianos.com/wordpress/) introduced me to the wonderful function wrapper modules called functools ,it has a nice function called partial.
here is a working sample of the code:


import maya.cmds
from functools import partial
def im_PrimaryFunc():
imSecondary_func()


def imSecondary_func():
a_Window=maya.cmds.window(title="Something")
a_layout=maya.cmds.columnLayout(adj=1,rs=10)
temp_var=maya.cmds.createNode("polySphere")
maya.cmds.button(label="call_aFunc",align="center",command = partial(a_calledFunc,temp_var, a_Window))
maya.cmds.showWindow(a_Window)


def a_calledFunc(arg00,arg01, part):
print(arg00)
print(arg01)


im_PrimaryFunc()



This way of creating a module makes the process way more safer by avoiding hassle of managing global variables in the program.