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
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Globals in PythonTags

    Scheduled Pinned Locked Moved PYTHON Development
    5 Posts 0 Posters 726 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 23/10/2017 at 02:30, xxxxxxxx wrote:

      hi there...

      i'm a bit confused about the use of globals in python tags ....
      it seems to me that global variables are shared with all instances of the same (or all) python tags?
      is that correct?

      what is the best approach to have a individual "global" for each tag instance?

      import c4d
      gvar = {}
      def main() :
           print "this individual python tags global is", gvar
           # or is gvar the same for all tag instances???

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

        On 23/10/2017 at 02:48, xxxxxxxx wrote:

        What makes you think that the global scope is shared between all tags?
        Because that is actually not the case.

        # tag 1
        def main() :
          print gvar # NameError
        
        # tag 2
        gvar = {}
        
        1 Reply Last reply Reply Quote 0
        • H Offline
          Helper
          last edited by

          On 23/10/2017 at 05:29, xxxxxxxx wrote:

          tx and hm... i guess it is my "own module" setup...

          each tag the same:

          import mymodule
          def main()
               mymodule.main(doc,op)

          now globals initialized in mymodule are "shared" between the tags!

          this could get handed by using userdata on op, but that also would stick when saving the doc...
          or maybe by defining a class in mymodule and creating an own instance in the tag??
          or... is there an easier way?

          ps..
          an odd approach would be to use the object op.GetObject()
          as an identifier for a global hash in the module.
          but it seems that a stable/safe object id (GUID,UniqueIP) is also difficult

          in mymodule

          def main(doc, op) # op = python tag
               global pytagGlobal
               try: pytagGlobal
               except: pytagGlobal = {}
               pytagGlobal[str(op.GetObject().GetGUID())] = ....

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

            On 23/10/2017 at 09:22, xxxxxxxx wrote:

            Well, your module "mymodule" is shared between both tags. Your origonal
            claim/question was about the global scope of the tags, not your module's
            namespace/scope.

            Data you store in any plain Python object will not be available when you re-open
            the document. You need to save the data, for example, in a sub-container of your
            Python tag's data container (op.GetDataInstance()) or create an actual Python
            plugin and implement TagData.Write() and TagData.Read().

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

              On 23/10/2017 at 11:37, xxxxxxxx wrote:

              Thanks Niklas,
              and sorry if i wasnt clear, i had to sort it out myself first ....

              i want to work using a module and i need per tag variables staying alive until the document is closed.
              i dont want the variables to stay alive when saving, so userdata is not the right choice.

              i'm not 100% sure,
              but i guess the best way would be to define a class and not just functions in the module,
              and then to create an instance in the tag code, which will have its own variables then.

              sadly thats looks like a bit of work 😛

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