Script path instead of call command in Python
-
Hi there, I have a question about a few lines of Python code in Cinema 4D. I have a functional script that executes my saved script via a button. However, sometimes the saved script changes the Command ID, causing the button to fail.
Is there a way to specify the Command ID in my script or can someone rewrite it to execute based on the file path instead?
Thank you for any possible solutions.
from typing import Optional import c4d #Python Tags Object placed User Data Button def message(msg_type, data): if msg_type == c4d.MSG_NOTIFY_EVENT: event_data = data['event_data'] if event_data['msg_id'] == c4d.MSG_DESCRIPTION_COMMAND: desc_id = event_data['msg_data']['id'] if desc_id[1].id == 16: c4d.CallCommand(600000014) def Button(): c4d.CallCommand(600000014) def main(): obj = op.GetObject() bc = c4d.BaseContainer() obj.AddEventNotification(op, c4d.NOTIFY_EVENT_MESSAGE, 0, bc)
-
Hello @specnazspe,
Welcome to the Plugin Café forum and the Cinema 4D development 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 Guidelines, as they line out details about the Maxon SDK Group support procedures. Of special importance are:
- Support Procedures: Scope of Support: Lines out the things we will do and what we will not do.
- Support Procedures: Confidential Data: Most questions should be accompanied by code but code cannot always be shared publicly. This section explains how to share code confidentially with Maxon.
- Forum Structure and Features: Lines out how the forum works.
- Structure of a Question: Lines out how to ask a good technical question. It is not mandatory to follow this exactly, but you should follow the idea of keeping things short and mentioning your primary question in a clear manner.
About your First Question
Command IDs are assigned dynamically to a script and therefore can change. It seems also a bit overly complicated to have a tag that evaluates user data interactions on its host to call a command wrapping a script. Commands are based on IDs, so you cannot pass them a script path, but Python itself has one million and one way to have Python code execute other Python code.
- Since you want to use the content of your script just as a library, you should make it one. Place the logic of it in a library apth of your liking and import that logic both into your Script manager script and Python tag when needed.
- You could use the std lib function
runpy
to run the code found at a script path yourself. Basically, what I did inrun_script
here. Find here the matching thread. - You could also just manipulate
sys.path
from your tag to inject the path where your script lies, so you can then import the script like a module.
I would not recommend doing point 2 or 3 because they are both error prone. Just expose the commonly used functionality used both by the script and tag as a lib.
edit: As pointed out by @m_adam, since S26, you can also evaluate the dynamic ID of scripting objects from Python. Finde an example in load_python_script_r26.py. The bread and butter of this is
c4d.GetDynamicScriptID
.Cheers,
Ferdinand