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