SOLVED Python scripting tag editing object position and points

Hello,

In another post (https://plugincafe.maxon.net/topic/10624/14071_where-is-information-on-the-python-tag/2) I read that you may not modify the scene with a python tag. A few examples were: inserting, deleting objects and changing the hierarchy.

Is the same true for changing the positions of objects, or their points?
Or is this considered not modifying the scene, but modifying the respective objects?

Thanks for your help and time!
With kind regards,
Casimir Smets

Hello @csmets,

Thank you for reaching out to us. Changing parameter values in the data container of node, e.g., its position, is allowed. While the posting of Sebastian you cited was fairly extensive, it is missing a key piece of information: Why is there this rule?

These restrictions do exist in the context of threading safety. One is not permitted to change the scene graph outside from the main thread, as this can cause pointers the main thread is relying on to point to garbage and consequently crash Cinema 4D. So, the main thread assumes there is the object A, you remove it, and the main thread tries to access the memory block where previously the object A was and the fireworks begin.

You can read more about threading restrictions, i.e., what you are allowed to do outside of the main-thread and what not, in Python: Threading Information. There is in general no guarantee that any method or function is always running on the main thread, but there are functions/methods which never run on the main thread. An example would be TagData.Execute and its smaller brother, the Python Programming tag main function, they will never run on the main thread and you are therefore restricted to thread-safe operations there. The TagData.Message method and its sister, the Python Programming tag message function, will however run fairly often on the main thread. You can check if you are on the main thread or not with c4d.threading.GeIsMainThread() and c4d.threading.GeIsMainThreadAndNoDrawThread().

So, the absolute statement 'you may not modify the scene with a python tag' is not quite true, you are never allowed to do it from the main() function, but doing it in the message() function while on the main thread, is totally fine.

Regarding your initial question: Modifying the data container of a node outside of the main thread is generally okay, even though our documentation is a bit koi on that point. Modifying the position of the vertices of a PointObject is also okay, resizing it is not, as something on the main thread could be relying on the object having exactly X points. In general, Cinema 4D will not crash immediately when you disregard these rules, but there is a quite significant chance of data loss if you do. You should never ship such code.

If you want to have something which for example adds points to a spline and then adds noise on top of it, you should use a Python Generator object and clone the input spline. With the cloned object you can do whatever you want, since it is not part of the scene graph anymore, and after your modifications return it as the result of the Python generator object.

Cheers,
Ferdinand

Hi Ferdinand,

Thanks a lot for your in-depth answer!
I was assuming it was possible, but now I know for sure!

Cheers,
Casimir Smets