Global Variable doesn't work on CoreMessage?
I have two main classes. One for
TreeViewand the other for
I have inserted
The event triggers well under the
The problem is global variable is not being updated under the
In the illustration below, the console always prints "images".
It should, however, be changed to whatever Tree Item is selected.
You can check the
CoreMessagefunction in the source code below:
Is there a way around this?
C4DS last edited by
Please use the Markdown to list your code, as mentioned here (link = https://plugincafe.maxon.net/topic/10953/how-to-post-questions). It's easier for anyone to look at your code in your topic, instead of following a link. Thanks.
zipit last edited by
cur_folderis not a
globalvariable, but a module level variable. The only place where you modify
TreeData.Select. You will need to test if that branch of the method is ever executed.
On a side note: Never use
globalvariables, because they are the devils work and module level variables are also a dangerous thing in a threaded environment (but you should be safe here, since everything should normally run on the main thread in your code).
Thanks for the response
I usually put markdowns on my code whenever I ask questions.
It's just there are dependencies on the code, so I didn't.
Anyway, here is the
def CoreMessage(self, id, msg): if id==PLUGIN_ID: #Fired by the main thread... print cur_folder return True return gui.GeDialog.CoreMessage(self, id, msg)
cur_folderis not a
Oh but I actually declared it before any Class or Functions.
The variable is at the top under the
RE: You will need to test if that branch of the method is ever executed.
Whenever I print the
class Tree Data > def Select, it works as expected (i.e. it prints a different variable whenever I click a folder)
It's on the
class GeDialog>def CoreMessagethat for some reason stuck at the initial variable declaration (i.e. "images")
Let me know if you need further clarification.
zipit last edited by zipit
I was just pointing out that
globalhas slightly different meaning in Python than in other languages.
globalis a keyword with which you can shadow a variable of an inner scope with an variable of an outer scope. Your variable is neither
globalnor really a variable, but an attribute bound to a module.
When you assign the name to
Select, you do this to a variable called
cur_valuein the scope of the method
Select, not to the attribute of the same name of the module.
SOME_ATTRIBUTE = "Bob is your uncle" class MyObject(): def do(self): """ """ SOME_ATTRIBUTE = "This won't work" def do_global(self): """ """ # Bad, really bad! Do not do this. global SOME_ATTRIBUTE SOME_ATTRIBUTE = "This will work" obj = MyObject() print SOME_ATTRIBUTE obj.do() print SOME_ATTRIBUTE obj.do_global() print SOME_ATTRIBUTE
Bob is your uncle Bob is your uncle This will work [Finished in 0.1s]
The correct way would either be to bind your
cur_folderto an object over which you have more control (a class or an instance of class) or just send
cur_folderalong with the message as message data.
Thanks for the warning.
Yep, the adding
class or an instance of classseems to work as expected.
I ended up using dict/list since I have to store the variable along with its designated path.
RE: just send cur_folder along with the message as message data
I'm not sure how to do this. I'll just probably create a separate thread for this one.
Thanks again for help.
m_adam last edited by
Regarding your last question, I think you should open a new topic, but the short answer there is no way to pass directly a variable with a MessageData, the best way is to use a scope accessible by both part and store this variable in this shared scope (can be global or any other).