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?

    Thanks.



  • On 23/07/2015 at 01:58, xxxxxxxx wrote:

    Hello,

    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.

    Best wishes,
    Sebastian



  • 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!

    @NiklasR
    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?

    @S_Bach
    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:

    Hello,

    you can set and get parameters either using SetParameter() and GetParameter() or simply using brackets []. Something like this

      
    op[12345] = "teststring"  
      
    # something  
      
    print(op[12345])  
    

    Be sure to use an ID that is not already used; to be sure you could use a new Plugin ID. To easily store multiple values you could save them in a BaseContainer and just save that BaseContainer.

      
    myData = c4d.BaseContainer()  
    myData[1] = "something"  
    myData[2] = "something else"  
      
    op[12346] = myData  
      
    # something  
      
    for data in op[123456]:  
         print(data)  
    

    best wishes,
    Sebastian



  • On 24/07/2015 at 10:47, xxxxxxxx wrote:

    Thank you very much!


Log in to reply