Hi while previous answers give some hints I would like to provide more in-depth answers to these questions.
First, what is a global variable?
In Python, a global variable is a variable that is stored in the dictionary returned by "globals()".
The globals() function returns a dictionary that is local to the current Python Scope.
Note that a Python scope only lives for a given time and is not persistent.
This means it doesn't save its state or current data to a file so if you restart Cinema 4D, it starts with a fresh new Python Scope) but as long as the host object of the Python Scope is alive the data will be stored.
So few things to know about Python implementation within Cinema 4D.
Each BaseList2D that implements a Python implementation (Python Generator, Python Scripting Tag, Python Xpresso, Field Layer) has its own Python Scope.
That means if I have two Python Generator they don't share the same Python Scope. But they both have a distinct one.
Now in a case of a plugin, it's a bit tricky, if you use a global variable let's say in an ObjectData.
The global variable will be available for all instances (BaseObject) of this ObjectData. This is because a BaseObject is an instance of an ObjectData implementation.
And since there is only one implementation (your ObjectData that uses the global variable) they all use the same global variable.
Now when you refer to plugin ID, it's a kind of term abuse.
A plugin ID is a unique ID (aka a number who is unique into Cinema 4D context).
And can be retrieved from https://developers.maxon.net/forum/pid (you must be logged).
So a Plugin ID can be used to register a plugin.
When you write BaseObject(100000) Cinema 4D will look at the ObjectData that is registered at the ID 100000 and create a BaseObject that will implement this particular ObjectData.
But you can also use a PluginId (or aka a Unique ID) to register a data into a BaseContainer.
But what is a BaseContainer?
BaseDocument, Objects, tag and pretty much everything a user can find in C4D inherit from BaseList2D, and get a BaseContainer.
A BaseContainer is a kind of array (or a dictionary in python), where data are stored according to a given ID.
This BaseContainer is owned by the host BaseList2D (seems logical but it's an important point).
This way Cinema 4D has a generic way of storing and reading data. So Cinema 4D can automate few things such as saving automatically the data stored in this BaseContainer into a File (aka Cinema 4D File) and read them back when loading the Cinema 4D file.
So back to our topic, if we assign a particular value to a "Plugin ID/Unique ID" you can assume that nothing within in Cinema 4D will overwrite this particular value.
(Third-party developer can access this data however and may erase them, so that's why we recommend using a Unique ID when you store data in a BaseContainer, this way you are sure to not erase other people data and you're is ok).
Of course, this works because everyone plays the game if I can tell.
Now back to the initial questions.
What's the use case of a global variable in Python in Cinema 4D ecosystem?
Storing data across multiple frames. (Can also be done with a BaseContainer, but you have to convert to a DataType that BaseContaienr accept, so list, dict are not supported so you have to "bake" which is slow)
Data are only alive for the current lifetime of the host and in maximum the Cinema 4D session.
What's the use case of data stored in a BaseContainer in Cinema 4D?
Storing data across multiple frames. (See the previous point in some case it may be ineffective).
Storing persistent data over multiple Cinema 4D sessions (the data will be stored in the host, e.g. the Python Generator Object by itself, so if you open the file in Cinema 4D, the Python Generator will restore the data in the BaseContainer).
Exposing data to others (Maybe other Objects want to know the current value stored in a particular Python Generator Object).
Finally, BaseContainer should be primarily chosen but as stated in some condition it makes more sense to use a global variable.
And here is how to define a global variable within a Python Generator:
import c4d
# Checks if the test variables exist in the global dict, if not assign a value of 10
global test
if 'test' not in globals():
test = 10
def main():
global test
print test
return c4d.BaseObject(c4d.Ocube)
I hope it answers your questions. Additionally please read BaseContainer Manual.
Cheers,
Maxime.