Thread: Global Variables in Python2.6.1 for Maya -Help Needed!

Reply to Thread
Results 1 to 3 of 3
  1. #1 Global Variables in Python2.6.1 for Maya -Help Needed! 
    Join Date
    Mar 2011
    Location
    Shimla
    Posts
    16
    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:

    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,
    Last edited by semiprowolverine; August 15th, 2012 at 03:22 AM.
    Shashi Kant

    MyBlog:

    http://learningfluids.blogspot.in/
    http://evocatematte.blogspot.in/
    Reply With Quote  

  2. #2  
    Join Date
    Apr 2007
    Location
    India
    Posts
    185
    Blog Entries
    1
    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.

    Reply With Quote  

  3. #3  
    Join Date
    Mar 2011
    Location
    Shimla
    Posts
    16
    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 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:
    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.
    Shashi Kant

    MyBlog:

    http://learningfluids.blogspot.in/
    http://evocatematte.blogspot.in/
    Reply With Quote  

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. 3D Maya Generalist needed
    By Chris55 in forum The Job Lot
    Replies: 0
    Last Post: August 3rd, 2011, 12:33 AM
  2. Vue and Maya Artists Needed
    By sdgproductions in forum The Job Lot
    Replies: 0
    Last Post: April 18th, 2011, 12:02 PM
  3. Maya Instructor needed
    By Michaelkeith in forum The Job Lot
    Replies: 0
    Last Post: March 18th, 2010, 04:19 PM
  4. MAYA GENERALISTS NEEDED
    By liquidbuddha in forum The Job Lot
    Replies: 0
    Last Post: October 3rd, 2007, 04:54 PM
  5. Maya Generalist Needed
    By reelfxjobs in forum The Job Lot
    Replies: 0
    Last Post: September 12th, 2006, 11:30 AM
Tags for this Thread

View Tag Cloud

Bookmarks
Bookmarks
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts