Unsolved 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:

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.

  1. 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.
  2. You could use the std lib function runpy to run the code found at a script path yourself. Basically, what I did in run_script here. Find here the matching thread.
  3. 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

MAXON SDK Specialist
developers.maxon.net