1
+ """Demonstrates how to retrieve graphs from scene elements.
2
+
3
+ To run the script, paste it into the Script Manager of Cinema 4D and execute it.
4
+ """
5
+ import c4d
6
+ import maxon
7
+ import mxutils
8
+
9
+ __author__ = "Ferdinand Hoppe"
10
+ __copyright__ = "Copyright (C) 2024 Maxon Computer GmbH"
11
+ __version__ = "2025.0.0+"
12
+
13
+ doc : c4d .documents .BaseDocument # The active document
14
+
15
+ def main () -> None :
16
+ """Called when Cinema 4D runs this script.
17
+ """
18
+ # The most common use case for GetGraph is to create a new material and graph from scratch.
19
+ # The call below will result in a new material being created in the active document with the
20
+ # name "matA". Since we are not passing an explicit node space, the currently active material
21
+ # node space will be used (which usually is the Redshift node space for most users).
22
+ graphA : maxon .NodesGraphModelRef = maxon .GraphDescription .GetGraph (name = "ActiveNodeSpace" )
23
+ print (f"{ graphA = } " )
24
+
25
+ # This will create a material named "matB" that has a graph in the Redshift material node space,
26
+ # no matter what the currently active material node space is.
27
+ graphB : maxon .NodesGraphModelRef = maxon .GraphDescription .GetGraph (
28
+ name = "RedshiftSpace" , nodeSpaceId = maxon .NodeSpaceIdentifiers .RedshiftMaterial )
29
+ print (f"{ graphB = } " )
30
+
31
+ # But we can also use the function to create or get a graph from an existing material. The call
32
+ # below will either create a new Redshift graph for the first material in the document #material
33
+ # or return its existing Redshift graph. The CheckType call exists so that the script halts when
34
+ # there is no materials in a document. But since we just crated #matA and #matB, we will here
35
+ # just retrieve the graph from "matB" (because new materials inserted in front at not at the end).
36
+ # I.e., #graphB and #graphC are the same.
37
+ material : c4d .BaseMaterial = mxutils .CheckType (doc .GetFirstMaterial ())
38
+ graphC : maxon .NodesGraphModelRef = maxon .GraphDescription .GetGraph (
39
+ material , nodeSpaceId = maxon .NodeSpaceIdentifiers .RedshiftMaterial )
40
+ print (f"{ graphC = } " )
41
+ print (f"{ graphB == graphC = } " )
42
+
43
+ # We could also create a Standard Renderer graph, or a third party render engine graph such
44
+ # as Arnold or VRay for example.
45
+ graphStandard : maxon .NodesGraphModelRef = maxon .GraphDescription .GetGraph (
46
+ name = "StandardSpace" , nodeSpaceId = maxon .NodeSpaceIdentifiers .StandardMaterial )
47
+
48
+ try :
49
+ vrayGraph : maxon .NodesGraphModelRef = maxon .GraphDescription .GetGraph (
50
+ name = "VRaySpace" , nodeSpaceId = "com.chaos.class.vray_node_renderer_nodespace" )
51
+ except Exception as e :
52
+ print (e )
53
+
54
+ # We can also pass a document or an object for the first argument #element of GetGraph. If we
55
+ # pass a document, it will yield the scene nodes graph of that document, and if we pass an object,
56
+ # it will yield its capsule graph (if it has one).
57
+ sceneGraph : maxon .NodesGraphModelRef = maxon .GraphDescription .GetGraph (doc )
58
+ print (f"{ sceneGraph = } " )
59
+
60
+ # Finally, we can determine the contents of a newly created graph. We can either get an empty
61
+ # graph or the so-called default graph with a few default nodes as it would be created in
62
+ # Cinema 4D when the user creates a new graph for that space. #createEmpty=True is the default
63
+ # of a #GetGraph call.
64
+ emptyGraph : maxon .NodesGraphModelRef = maxon .GraphDescription .GetGraph (
65
+ name = "ActiveSpaceEmptyGraph" , createEmpty = True )
66
+ defaultGraph : maxon .NodesGraphModelRef = maxon .GraphDescription .GetGraph (
67
+ name = "ActiveSpaceDefaultGraph" , createEmpty = False )
68
+
69
+ # Finally, there is also GraphDescription.GetMaterialGraphs which simplifies iterating over all
70
+ # material graphs of a given node space in a document. This can simplify carrying out batch
71
+ # operations on material graphs for a specific render engine, while keeping other render engines
72
+ # untouched. Other than GetGraph, this function will not create new graphs when none exist.
73
+
74
+ # Print all Redshift renderer material graphs in a document. This will be at least one graph,
75
+ # because created explicitly one in this script.
76
+ graph : maxon .NodesGraphModelRef
77
+ for graph in maxon .GraphDescription .GetMaterialGraphs (doc , maxon .NodeSpaceIdentifiers .RedshiftMaterial ):
78
+ print (f"Redshift graph: { graph } " )
79
+
80
+ # Same for the Standard renderer material graphs. This will also be at least one graph, because
81
+ # created explicitly one in this script.
82
+ for graph in maxon .GraphDescription .GetMaterialGraphs (doc , maxon .NodeSpaceIdentifiers .StandardMaterial ):
83
+ print (f"Standard graph: { graph } " )
84
+
85
+ if __name__ == "__main__" :
86
+ main ()
0 commit comments