Skip to content

Commit c3eec50

Browse files
committed
S26 Release
Add new example for the Asset frameworks Add new example for Geometry handling
1 parent aea195b commit c3eec50

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+4963
-168
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
enum
2-
{
3-
// End of symbol definition
4-
_DUMMY_ELEMENT_
5-
};
1+
enum
2+
{
3+
// End of symbol definition
4+
_DUMMY_ELEMENT_
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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()

scripts/01_foundations/readme.md

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Maxon API:
1616

1717
## Examples
1818

19+
### load_python_script
20+
21+
Handle the scripts managed by the Script Manager programmatically.
22+
1923
### reg_plugin_info
2024

2125
Stores plugin information.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
Copyright: MAXON Computer GmbH
3+
Author: Maxime Adam, Ferdinand Hoppe
4+
5+
Description:
6+
- Showcases the usage of the custom data type `c4d.Gradient`.
7+
- Creates a material with a gradient shader in the color channel.
8+
9+
Class/method highlighted:
10+
- c4d.BaseMaterial
11+
- c4d.BaseShader
12+
- c4d.Gradient
13+
- c4d.BaseContainer
14+
15+
"""
16+
17+
import c4d
18+
19+
def main():
20+
# Create a new material and a new gradient shader.
21+
material = c4d.BaseMaterial(c4d.Mmaterial)
22+
shader = c4d.BaseShader(c4d.Xgradient)
23+
24+
# Get a copy of the c4d.Gradient data referenced by the gradient shader
25+
# and a copy of the knot data stored in the c4d.Gradient data. The knot
26+
# data is expressed as a c4d.BaseCoontainer.
27+
gradient = shader[c4d.SLA_GRADIENT_GRADIENT]
28+
knotData = gradient.GetData(c4d.GRADIENT_KNOT)
29+
30+
# Iterate over all knots in the knot data container and set their
31+
# interpolations to linear. Each knot in the knot data container is itself
32+
# a c4d.BaseContainer, storing the data for a single knot.
33+
for _, knot in knotData:
34+
knot[c4d.GRADIENTKNOT_INTERPOLATION] = c4d.GRADIENT_INTERPOLATION_NONE
35+
36+
# Items can also be retrieved by their index from a BaseContainer, here
37+
# the first index, i.e., second item, in the container. In this case the
38+
# position of the knot is written and then the knot container is being
39+
# written back into the index its has been retrieved from.
40+
knot = knotData.GetIndexData(1)
41+
42+
knot[c4d.GRADIENTKNOT_POSITION] = 0.5
43+
knotData.SetIndexData(1, knot)
44+
45+
# After these modifications the knots must be written back into the
46+
# c4d.Gradient data, because GetData() returned a copy above. This also
47+
# does apply to the shader and its gradient for the same reason.
48+
gradient.SetData(c4d.GRADIENT_KNOT, knotData)
49+
shader[c4d.SLA_GRADIENT_GRADIENT] = gradient
50+
51+
# Aside from assigning the shader to the correct parameter of the
52+
# material, it is also important to call BaseList2D.InsertShader(), as
53+
# otherwise the setup will not work.
54+
material.InsertShader(shader)
55+
material[c4d.MATERIAL_COLOR_SHADER] = shader
56+
57+
# Finally, the material is being inserted into the active document and an
58+
# update event is being pushed event to Cinema 4D, so that its GUI can
59+
# catch up to our changes. 'doc' is a predefined module attribute in
60+
# script manger scripts which points to the active document. It is
61+
# equivalent to c4d.documents.GetActiveDocument().
62+
doc.InsertMaterial(material)
63+
c4d.EventAdd()
64+
65+
if __name__=='__main__':
66+
main()

scripts/02_data_algorithms/readme.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ Maxon API:
3232
Creates a Luka Noise into a BaseBitmap.
3333
Displays this BaseBitmap into the Picture Viewer.
3434

35+
### datatype_gradient
36+
37+
Showcases the usage of the custom data type c4d.Gradient.
38+
3539
### icondata_basic
3640

37-
3841
Displays the icon of the selected object into the Picture Viewer.
3942
Before R21 it's possible to call BaseList2D.GetIcon which will returns a dictionary representing an IconData.
4043

0 commit comments

Comments
 (0)