Globals in PythonTags

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

  • 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 = {}

  • 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()

    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?

    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())] = ....

  • 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

    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().

  • 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 :P

Log in to reply