How to set up a global variable that all plugins can access
-
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?
-
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
, andC
, 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
orWritePluginInfo
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
orc4d
. 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.
- Cinema offers some functionality to store data in its world container. See Plugin Data Functions, you could for example use
- 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 (apy
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 - 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.
-
Thank you very much for your answer! I already know how to do it, and your answer has given me some ideas.
-
This post is deleted!