Python Generator and Storing Variables [SOLVED]
On 22/07/2015 at 10:49, xxxxxxxx wrote:
I have this codebit in my python generator:
if not ('myvariable' in locals() or 'myvariable' in globals()) :
print "myvariable created"
myvariable = "hwllo world"
I should not be seeing "myvariable created" more than once, but I see it every time I move the mouse around (optimize cache and reset on frame 0 unchecked as this is a generator that needs to update every frame)
So I believe it's not possible to store any variables? it runs the whole generator from scratch every signle time?
Then how should I store my variables?
On 23/07/2015 at 01:58, xxxxxxxx wrote:
you could store persistent data in the Python generator object itself (using the "op" variable) or you could create and access userdata to make your variables also accessible to the user. You find an example on how to access userdata in a Python generator in the examples that are part of the Python SDK download.
On 23/07/2015 at 02:22, xxxxxxxx wrote:
If you want to create/assign a global variable, you have to specify that first. Keep in mind though that it will not be stored persistently in the file, but only until the object is destroyed in memory or the Python code is re-compiled.
myvariable = None def main() : global myvariable if myvariable is None: print "myvariable first assign" myvariable = "hello world"
Personally, I prefer to not use the global keyword and instead use a global dictionary or class that I can modify directly.
class G: myvariable = None def main() : if G.myvariable is None: print "myvariable first assign" G.myvariable = "hello world"
On 23/07/2015 at 10:55, xxxxxxxx wrote:
Thank you for the answers!
in your example, every time the code is run (every frame/update) it creates global myvariable over an over right? Then how would it store information?
canyou give me an example of storing the data in op? op.mystring = "string" does not seem to work for me.
To be more clear: the data I want to store is an array/list of points, that is the modified copy of the child object
On 23/07/2015 at 11:21, xxxxxxxx wrote:
Have you tested the code? Sure the code is run every frame/update, but the variable is not None after
the first run.
On 24/07/2015 at 02:33, xxxxxxxx wrote:
op = "teststring" # something print(op)
myData = c4d.BaseContainer() myData = "something" myData = "something else" op = myData # something for data in op: print(data)
On 24/07/2015 at 10:47, xxxxxxxx wrote:
Thank you very much!