View Full Version : Global Variables in Python
rabidPraxis 12-20-2008, 01:04 AM Question:
Ok, I know using global variables is a one way street to trouble, but I have a use for them, yet I can't get them to work with modules imported into maya. Usually when calling code in the <maya console> if I were to say:
global _blah
_blah = "some global state"
and then I were to access that variable at a later time, it would still be there. Now, if I do the same global call within an imported module, the global state seems to only be within the imported module and not maya itself.
Does anyone know how to inject some global variables into maya, from within an imported module?
I hope this makes sense..
Thanks,
kw
|
|
bloggs
12-20-2008, 06:03 AM
dont you have to declare the variable before you make it global.
so
_blah = "this is my blah"
global _blah
just a guess.
RyanT
12-22-2008, 02:40 AM
Python does not have a way to specify a variable in a module that is global to all namespaces. You can define it as global but it will only be global in that module. Which you dont define a variable as a global by typing global you just define a variable in the global scope of that module outside of all other scopes. Generaly the method for getting a module or variable into a namespace you want is to import it like so:
import mymodule
print mymodule.varA
You can use mymodule.varA in any way you like. Treat it just like the variable. You can also say this:
import mymodule as mymod
print mymod.varA
Now that SHOULD work for your case and I have my doubts that it wont. It sounds like you are coming from the MEL side which has no such thing as namespaces to protect your code. With Python you can create a module and know without a shadow of a doubt that the variables within are not going to be overwritten or override other variables because of the namespace you use. With that said you can also import modules into a namespace completely or just import a specific variable into a specific namespace. Like so:
# import the entire module into your namespace
from mymodule import *
# import a variable from a module into your namespace
from mymodule import varA, varB
Can you explain what you are trying to do? Again I think I could explain a way to use the first method if I had example code. I also have more about importing on my site. Just do a search for import.
rabidPraxis
12-22-2008, 03:40 AM
Ryan: Thanks for the reply and yes, I have come to Python with extensive MEL experience. Although I have a good deal of OOP experience, I'm still trying to wrap my brain around the pythonian way of doing things. I thought the best way to tackle my problem was to store common properties in the application (<console>) namespace.
I realize now that this is impossible, and rightly so. Namespaces, although seemingly arduous for quick simple tasks, really are a lifesaver when many modules are in play. I found that assigning properties to my imported module works great because it acts as a built in singleton that can be passed to any script and there is no chance of unnessecary instanciation.
Thanks again for the replies guys!!
K
CGTalk Moderation
12-22-2008, 03:40 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
vBulletin v3.0.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.