Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware 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

    How to set up a global variable that all plugins can access

    Cinema 4D SDK
    python
    2
    4
    661
    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.
    • 1
      1279906127
      last edited by

      Hello everyone, I have a question to ask you. For example, I have a plugin A, where I have set a global variable open: bool = True. I want plugins BCDEFG… and many other plugins to be able to access the value of this global variable open. How can I do this?

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

        Hey @1279906127,

        Thank you for reaching out to us. Well you do that like you would do it for any other Python project: You put that variable into a module to which all your plugins have access. How that plays out depends on the context. When we have the plugins A, B, and C, we have two scenarios:

        • They are shipped separately to the customer, i.e., each plugin will live in its own folder (possibly in different root directories) on the machine of users. Here it will be impossible to have A, B, and C directly share a module shipped in these folders, as Cinema 4D does not expose plugin paths as module search paths.
          • Cinema offers some functionality to store data in its world container. See Plugin Data Functions, you could for example use SetWorldPluginData or WritePluginInfo to write data under an ID which all your plugins use. But that only works for constants.
          • You can follow our Python Libraries Manual and have each of your plugins install a shared library into one of the library paths (not a great idea).
          • You can technically just monkey patch a builtin module. E.g., just attach some data to sys or c4d. While this is a somewhat common practice, I would say this is even worse than the former point, and I would strongly advise against it.
          • Technically somewhat clean but complex would be to have your plugins communicate with each other with messages to align some atomic data (a bool for example), or to exchange their directories so that they can load each other.
          • In the end I would say this point is simply not meant to be. Two separate plugins are two separate plugins, they should not share a program state, this leads to all sorts of issues. When you want to share just some configuration, you can use the first point. Everything else is possible but a bad idea.
        • They are shipped as one entity to the customer, i.e., reside in one folder, e.g., three pyp files in one folder. Here you can just put a module (a py file) next to your plugins, which all of your plugins import. See mxutils.LocalImportPath for details.

        Technically you also could have each plugin simply inject itself into the module search paths, so that other plugins can see its modules, but that would be an even worse idea (please do not do that).

        Cheers,
        Ferdinand

        MAXON SDK Specialist
        developers.maxon.net

        1 Reply Last reply Reply Quote 0
        • 1
          1279906127
          last edited by

          Thank you very much for your answer! I already know how to do it, and your answer has given me some ideas.

          1 Reply Last reply Reply Quote 0
          • 1
            1279906127
            last edited by

            This post is deleted!
            1 Reply Last reply Reply Quote 0
            • First post
              Last post