How to access and modify the default gravity value of the scene using Python script in Cinema 4D?
-
Hi everyone,
I'm working on a Python script in Cinema 4D and I'm trying to toggle the default gravity of the scene between 0 and -981. I've tried several approaches, but I can't seem to find the correct method to access and modify this value.
Here's what I've attempted so far:
from typing import Optional import c4d doc: c4d.documents.BaseDocument # The active document op: Optional[c4d.BaseObject] # The active object, None if unselected def main() -> None: def object() -> Optional[c4d.BaseObject]: return doc.GetActiveObject() active_object = object() if not active_object: return current_gravity = active_object[c4d.PBDSCENE_DEFAULTGRAVITY] if current_gravity == 0: active_object[c4d.PBDSCENE_DEFAULTGRAVITY] = -981 else: active_object[c4d.PBDSCENE_DEFAULTGRAVITY] = 0 if __name__ == '__main__': main() c4d.EventAdd()
Unfortunately, this doesn't work. If anyone can point me to the correct way to access and modify this value, that would be great!
Thanks in advance for your help!
-
Hey @lakpo,
Welcome to the Maxon developers forum and its community, it is great to have you with us!
Getting Started
Before creating your next postings, we would recommend making yourself accustomed with our forum and support procedures. You did not do anything wrong, we point all new users to these rules.
- Forum Overview: Provides a broad overview of the fundamental structure and rules of this forum, such as the purpose of the different sub-forums or the fact that we will ban users who engage in hate speech or harassment.
- Support Procedures: Provides a more in detail overview of how we provide technical support for APIs here. This topic will tell you how to ask good questions and limits of our technical support.
- Forum Features: Provides an overview of the technical features of this forum, such as Markdown markup or file uploads.
It is strongly recommended to read the first two topics carefully, especially the section Support Procedures: Asking Questions.
About your First Question
The answer to your question depends on what you would consider the 'default gravity value of the scene'. There are basically two interpretations:
- You want to change the value for the default simulation scene as accessible via the Attribute Manager in document mode. You would here have to dig out the default simulation scene from its scene hook. That scene object will never be part of the normal object tree and you can therefor not access it with
BaseDocument.GetActiveObject
. We recently talked about this very subject here including some example code. - You want to reset the gravity value of a simulation scene, no matter if the default or a custom one, to its default value. You would have to access the description of the node, get the parameter you are interested in, and then read
DESC_DEFAULT
.
Cheers,
Ferdinand -
Hi Ferdinand,
I just wanted to thank you for your invaluable help! Thanks to your guidance, I was able to solve my issue with toggling the default gravity in Cinema 4D. Honestly, I didn't think it would be this complicated!
For anyone who might encounter a similar problem, here's the script I used, based on your code. It allows access to the global default simulation scene in the simulation hook and toggles the gravity value between 0 and -981:
import c4d doc: c4d.documents.BaseDocument # The currently active document. op: c4d.BaseObject | None # The primary selected object in the document. Can be None. c4d.SHsimulation: int = 1057220 # The ID of the simulation hook. def main() -> None: """Called by Cinema 4D when the script is executed.""" # Get the simulation hook of the active document simulationHook: c4d.BaseList2D | None = doc.FindSceneHook(c4d.SHsimulation) if not simulationHook: raise RuntimeError("Cannot find the simulation hook.") # Retrieve the object branch from the simulation hook branches: list[dict] = simulationHook.GetBranchInfo() data: dict | None = next(n for n in branches if n["id"] == c4d.Obase) # We're looking for the object branch. if not data: raise RuntimeError("Malformed simulation hook with missing object branch.") # Get the simulation scene simulationScene: c4d.BaseObject | None = data["head"].GetFirst() if not simulationScene: raise RuntimeError("Malformed simulation hook with missing object branch children.") # Toggle gravity between 0 and -981 if simulationScene[c4d.PBDSCENE_DEFAULTGRAVITY] == 0: simulationScene[c4d.PBDSCENE_DEFAULTGRAVITY] = -981 else: simulationScene[c4d.PBDSCENE_DEFAULTGRAVITY] = 0 c4d.EventAdd() if __name__ == '__main__': main()
This script toggles the default gravity of the simulation scene between 0 and -981 depending on its current value.
Thanks again for your help! I hope this can be useful to others who might face the same issue.