|
| 1 | +""" |
| 2 | +Copyright: MAXON Computer GmbH |
| 3 | +Author: Maxime Adam |
| 4 | +
|
| 5 | +Description: |
| 6 | + - Create a new temporary script. |
| 7 | + - Load an existing Python file into the Script Manager. |
| 8 | + - Retrieve the first script not active in the script manager and make it active. |
| 9 | +
|
| 10 | +Class/method highlighted: |
| 11 | + - c4d.CreateNewPythonScript() |
| 12 | + - c4d.LoadPythonScript() |
| 13 | + - c4d.GetScriptHead() |
| 14 | + - c4d.GetDynamicScriptID() |
| 15 | + - c4d.SetActiveScriptObject() |
| 16 | +""" |
| 17 | +import c4d |
| 18 | +import os |
| 19 | + |
| 20 | + |
| 21 | +def CreateNewPythonScript(): |
| 22 | + """ Create a new temporary script. |
| 23 | + """ |
| 24 | + # Create a new temporary script. The first argument is the script name. If an empty string is used as the script |
| 25 | + # name, Cinema 4D will generate automatically the name. The second argument being the content of the script. |
| 26 | + # The new script will be set as active automatically. |
| 27 | + newScript = c4d.CreateNewPythonScript("", r"print('Sometimes science is more art than science.')") |
| 28 | + |
| 29 | + # ScriptObject being BaseList2D the usual C4DAtom bracket operator can be used to access various properties from it. |
| 30 | + scriptName = newScript[c4d.PYTHONSCRIPT_SCRIPTNAME] |
| 31 | + |
| 32 | + # An empty PYTHONSCRIPT_SCRIPTPATH indicate that this script is not saved and will be deleted once Cinema 4D close. |
| 33 | + scriptPath = newScript[c4d.PYTHONSCRIPT_SCRIPTPATH] |
| 34 | + |
| 35 | + # PYTHONSCRIPT_TEXT returns the python script visible in hte Script Manager. |
| 36 | + scriptContent = newScript[c4d.PYTHONSCRIPT_TEXT] |
| 37 | + |
| 38 | + print(f"Added '{scriptName}', it's content in the Script Manager is:\n{scriptContent}") |
| 39 | + |
| 40 | + |
| 41 | +def LoadExistingPythonScript(): |
| 42 | + """Load an existing Python file into the Script Manager. |
| 43 | + """ |
| 44 | + scriptPathToLoad = os.path.join(os.path.dirname(__file__), "reg_plugin_info_r23.py") |
| 45 | + |
| 46 | + # Load the script in the script manager and set it as active automatically |
| 47 | + loadedScript = c4d.LoadPythonScript(scriptPathToLoad) |
| 48 | + |
| 49 | + # ScriptObject being BaseList2D the usual C4DAtom bracket operator can be used to access various properties from it. |
| 50 | + scriptName = loadedScript[c4d.PYTHONSCRIPT_SCRIPTNAME] |
| 51 | + |
| 52 | + # An empty PYTHONSCRIPT_SCRIPTPATHindicate that this script is not saved and will be deleted once Cinema 4D close. |
| 53 | + scriptPath = loadedScript[c4d.PYTHONSCRIPT_SCRIPTPATH] |
| 54 | + |
| 55 | + # PYTHONSCRIPT_TEXT returns the python script visible in hte Script Manager. |
| 56 | + scriptContent = loadedScript[c4d.PYTHONSCRIPT_TEXT] |
| 57 | + |
| 58 | + print(f"Load '{scriptName}', from '{scriptPath}' it's content in the Script Manager is:\n{scriptContent}") |
| 59 | + |
| 60 | + |
| 61 | +def SetActiveScript(): |
| 62 | + """Retrieve the first script not active in the script manager and make it active. |
| 63 | +
|
| 64 | + Scripts available in the script manager are BaseList2D stored into a specific GeListHead. |
| 65 | + """ |
| 66 | + def HierarchyIterator(obj): |
| 67 | + """A Python Generator to iterate over the Hierarchy. |
| 68 | + Args: |
| 69 | + obj: The starting object of the python generator (will be the first result) |
| 70 | + Returns: |
| 71 | + All objects under and next of the `obj` |
| 72 | + """ |
| 73 | + while obj: |
| 74 | + yield obj |
| 75 | + for opChild in HierarchyIterator(obj.GetDown()): |
| 76 | + yield opChild |
| 77 | + obj = obj.GetNext() |
| 78 | + |
| 79 | + # Retrieve the script list head. |
| 80 | + scriptHead = c4d.GetScriptHead() |
| 81 | + |
| 82 | + # Retrieve the first script not active in the script manager and make it active. |
| 83 | + # A script is active when it has its code displayed in the script manager. |
| 84 | + for scriptOp in HierarchyIterator(scriptHead.GetFirst()): |
| 85 | + # Go to the next script object if the script is already active in the script manager. |
| 86 | + if scriptOp.GetBit(c4d.BIT_ACTIVE): |
| 87 | + continue |
| 88 | + |
| 89 | + # Retrieve the unique plugin ID generated by Cinema 4D for this script. |
| 90 | + scriptId = c4d.GetDynamicScriptID(scriptOp) |
| 91 | + |
| 92 | + # Define this script as the new script to be active in the script manager. |
| 93 | + c4d.SetActiveScriptObject(scriptId) |
| 94 | + |
| 95 | + # ScriptObject being BaseList2D the usual C4DAtom bracket operator can be used to access various properties from it. |
| 96 | + scriptName = scriptOp[c4d.PYTHONSCRIPT_SCRIPTNAME] |
| 97 | + |
| 98 | + # An empty PYTHONSCRIPT_SCRIPTPATH indicate that this script is not saved and will be deleted once Cinema 4D close. |
| 99 | + scriptPath = scriptOp[c4d.PYTHONSCRIPT_SCRIPTPATH] |
| 100 | + |
| 101 | + # PYTHONSCRIPT_TEXT returns the python script visible in hte Script Manager. |
| 102 | + scriptContent = scriptOp[c4d.PYTHONSCRIPT_TEXT] |
| 103 | + |
| 104 | + print(f"Set as active '{scriptName}', saved in `{scriptPath}` with the next content in" |
| 105 | + f" the Script Manager is:\n{scriptContent}") |
| 106 | + |
| 107 | + break |
| 108 | + |
| 109 | + |
| 110 | +def main(): |
| 111 | + CreateNewPythonScript() |
| 112 | + LoadExistingPythonScript() |
| 113 | + SetActiveScript() |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + main() |
0 commit comments