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

    Access Python Tag Globals from outside

    Cinema 4D SDK
    python
    3
    10
    1.1k
    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.
    • indexofrefractionI
      indexofrefraction
      last edited by indexofrefraction

      Hi,

      is it possible to access a python tags global from outside?

      ie. process all Tags of the same kind,
      and set a global variable in their python code?

      or trigger a function in their code?

      best, index

      ps.
      I know i could set some userdata, but it would be more elegant that way.
      the information doesn't have to be saved

      1 Reply Last reply Reply Quote 0
      • ferdinandF
        ferdinand
        last edited by

        Hi,

        no, that is mostly not possible. Python code is interpreted as a module and since you have no access to the module objects Cinema does create internally, you have also no access to their attributes (i.e. variables and functions). What you can do, is:

        • Run any python code yourself. This will give you access to the functions, but mutable values of that module, e.g. the global variables, will obviously not be bound to / the same values as the ones in the respective scripting tag.
        • Realise your global variables as user data attached to the tag. This will allow you to access them from the outside.
        • Realise your global variables as a fake module. If your global variables are too complex to be serialised in a sensible way into a BaseContainer, i.e. user data, then you could store them in any Python object of your choice and inject that object into Python's module dictionary. Accessing your global variables is then just a matter of importing them like you would import any module. Note that some people will consider this to be a hack and/or bad style.

        Cheers,
        zipit

        MAXON SDK Specialist
        developers.maxon.net

        1 Reply Last reply Reply Quote 1
        • indexofrefractionI
          indexofrefraction
          last edited by indexofrefraction

          Hi zipit,

          tx, i kinda expected that...
          but couldn't i just send a Message?

          mytag.Message(id, data)

          i dont need the data,
          i just need to pass some custom id like 999...

          then i catch it in the tags message()
          the tag will know what to do

          1 Reply Last reply Reply Quote 0
          • ferdinandF
            ferdinand
            last edited by

            Hi,

            sure, you can also send messages to your scripting node. But that would be a different question/approach than the one you did ask for 😉

            Cheers,
            zipit

            MAXON SDK Specialist
            developers.maxon.net

            1 Reply Last reply Reply Quote 1
            • indexofrefractionI
              indexofrefraction
              last edited by indexofrefraction

              Yes, sorry...

              these are fixed values to be set in the tag space
              the tag can do it itself, triggered by a message
              that is more elegant anyway 🙂

              is there a safe number space we can use for MSG_ ids ?
              to not collide with cinema4d internal messages
              (i dont need the data)

              1 Reply Last reply Reply Quote 0
              • ferdinandF
                ferdinand
                last edited by

                Hi,

                you will have to / should register a plugin id here in the forum to avoid message id collisions.

                Cheers,
                zipit

                MAXON SDK Specialist
                developers.maxon.net

                1 Reply Last reply Reply Quote 0
                • indexofrefractionI
                  indexofrefraction
                  last edited by indexofrefraction

                  No sure if I understand that right...
                  I use (on purpose) just a standard python tags, not python plugins.

                  I meant the id here : myTag.Message(id)
                  some of these are reserved like c4d.MSG_DESCRIPTION_CHECKUPDATE

                  i wonder if there are value ranges that are safe to use

                  how would i use plugin ID in this context?

                  1 Reply Last reply Reply Quote 0
                  • ferdinandF
                    ferdinand
                    last edited by ferdinand

                    Hi,

                    plugin ids are just a namespace curated by MAXON which is used for far more than just identifying plugins. You would use the plugin id here as the message id. This would ensure that neither Cinema nor anyone else is trying to convey some other meaning with that particular message id you are using.

                    Cheers,
                    zipit

                    MAXON SDK Specialist
                    developers.maxon.net

                    1 Reply Last reply Reply Quote 0
                    • M
                      m_adam
                      last edited by

                      Hi @indexofrefraction, I think @zipit already almost everything but just to clarify.

                      Each Python element (aka a Python Script Tag, a Python Generator) have Python Scope not bound to another this will be way too risky and with some challenges regarding synchronization internally, so they are completely split and each scope is independent without the luck to communicate from one to another.

                      Message is a good way to go if you just want to communicate from one entity to another. As @zipit a message is just an integer register in our Maxon "Namespace" so we reserve this ID so we are sure that this ID is only about this particular Message and this integer value is not used to represent anything else than this Message. This way when an object received this particular integer value, we can ensure this is about this particular Message and can assume the data structure passed.

                      So if you want to use your own Message it's advertised to register this ID as you register a plugin ID this way you ensure that nobody will use this ID in their plugins for something else.

                      Finally, note there is the MSG_BASECONTAINER which allows sending a basecontainer directly, this can be quiet useful because a BaseContainer can have an ID (So you have a kind of sub-message ID, this can be useful within communication to know the current status of the communication). And in this BaseContainer you can also store the original sender and multiple values.

                      Find in the attached scene, 2 Python Generator that asks a Python Tag to do a different math operation, and set their respective name according to the result received from the Python Tag. (Just enable them, the main function will be triggered and then message functions)
                      testMessage.c4d

                      Cheers,
                      Maxime.

                      MAXON SDK Specialist

                      Development Blog, MAXON Registered Developer

                      1 Reply Last reply Reply Quote 1
                      • indexofrefractionI
                        indexofrefraction
                        last edited by indexofrefraction

                        Thanks zipit and Maxime,

                        I use the MSG_BASECONTAINER solution now,
                        thanks for the example !

                        one question here:
                        you always return True from the message() function
                        is that necessary?

                        the docs here
                        https://developers.maxon.net/docs/py/2023_2/manuals/introduction/python_tag.html?highlight=python plugin
                        just says :
                        Return type: bool
                        Returns: Depends on the message type.

                        ...

                        and a bit off topic :
                        is there a message, when a python tag or generator gets enabled / disabled by the (generator) switch in the object manager?
                        can i catch that?

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