Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Recent
    • Tags
    • Users
    • Login

    Python Generator and Storing Variables [SOLVED]

    Scheduled Pinned Locked Moved PYTHON Development
    7 Posts 0 Posters 535 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H Offline
      Helper
      last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • H Offline
        Helper
        last edited by

        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[URL-REMOVED].

        Best wishes,
        Sebastian


        [URL-REMOVED] @maxon: This section contained a non-resolving link which has been removed.

        1 Reply Last reply Reply Quote 0
        • H Offline
          Helper
          last edited by

          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"
          
          1 Reply Last reply Reply Quote 0
          • H Offline
            Helper
            last edited by

            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

            1 Reply Last reply Reply Quote 0
            • H Offline
              Helper
              last edited by

              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.

              1 Reply Last reply Reply Quote 0
              • H Offline
                Helper
                last edited by

                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[URL-REMOVED]. 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


                [URL-REMOVED] @maxon: This section contained a non-resolving link which has been removed.

                1 Reply Last reply Reply Quote 0
                • H Offline
                  Helper
                  last edited by

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

                  Thank you very much!

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post