Repository navigation
Expand file tree
/
Copy pathpy_obj_dynamic_parameters.pyp
More file actions
408 lines (311 loc) · 16.4 KB
/
Copy pathpy_obj_dynamic_parameters.pyp
File metadata and controls
408 lines (311 loc) · 16.4 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
"""Demonstrates how to implement a node (at the example of an object) that dynamically updates its parameters.
The generator builds parameters at runtime and links one of them to the angle of the first Phong tag. It highlights how dynamic descriptions, parameter translation, and enabling work together.
To run this example, place the file in the Cinema 4D plugins folder and restart Cinema 4D.
Topics:
- Building dynamic object descriptions at runtime
- Translating and enabling generated parameters
- Linking generated parameters to existing object data
"""
__copyright__ = "Copyright 2026, MAXON Computer"
__author__ = "Maxime Adam"
__date__ = "25/08/2026"
__license__ = "Apache-2.0 license"
__version__ = "18.0.0"
import copy
import random
import c4d
# Be sure to use a unique ID obtained from developers.maxon.net
PLUGIN_ID = 1037871
# Dynamic group and parameters IDs
OPYDYNAMICPARAMETERSOBJECT_DYNAMICGROUP = 1100
OPYDYNAMICPARAMETERSOBJECT_DYNAMICGROUP_FIRSTPARAMETER = OPYDYNAMICPARAMETERSOBJECT_DYNAMICGROUP + 1
# Dynamic Object plugin Example for the Dynamic Description additions in R18 Python API
# Parameter ID 1001: REAL parameter OPYDYNAMICPARAMETERSOBJECT_TRANSLATED_PHONG_ANGLE (refers the first attached Phong tag PHONGTAG_PHONG_ANGLE parameter, see TranslateDescID() below)
# Parameter ID 1002: LONG parameter OPYDYNAMICPARAMETERSOBJECT_PARAMETERSNUMBER for the number of parameters in the dynamic group
# Parameter ID 1000: Dynamic Group
# Parameters ID 1100+: Dynamic float parameters
class DynamicParametersObjectData(c4d.plugins.ObjectData):
def __init__(self):
self.parameters = [] # Dynamic parameters value
self.randomID = 0 # Random dynamic parameter ID to disable (see GetDEnabling() below)
def Init(self, node, isCloneInit=False):
"""Called by Cinema 4D on the initialization of the ObjectData, usually the place to define parameters value.
Args:
node (c4d.GeListNode): The instance of the ObjectData.
isCloneInit (bool): True if the object data is a copy of another one.
Returns:
bool: True if the initialization was successful, otherwise False, preventing Cinema 4D from creating the object.
"""
if not isCloneInit:
# Defines how many parameters the object will have
node[c4d.OPYDYNAMICPARAMETERSOBJECT_PARAMETERSNUMBER] = 10
return True
def Message(self, node, msgId, data):
"""Called by Cinema 4D to notify the object of a event.
Args:
node (c4d.BaseObject): The instance of the ObjectData.
msgId (int): The message ID type.
data (Any): The message data, the type depends on the message type.
Returns:
Any: Depends of the message type, most of the time True.
"""
# MSG_MENUPREPARE is received when called from the menu, to let some setup work.
# In the case of this message, the data passed is the BaseDocument the object is inserted
if msgId == c4d.MSG_MENUPREPARE:
# Creates a Phong Tag
pTag = c4d.BaseTag(c4d.Tphong)
if pTag is None:
raise MemoryError("Failed to create a phong tag.")
# Inserts the tag on the object
node.InsertTag(pTag)
return True
def CopyTo(self, dest, snode, dnode, flags, trn):
"""Called by Cinema 4D when the current object instance is copied.
The purpose is to copy the member variable from source variable to destination variable.
Args:
dest (c4d.plugins.NodeData): The new instance of the ObjectData where the data need to be copied.
snode (c4d.GeListNode): The source node data, i.e. the current node.
dnode (c4d.GeListNode): The new node data where the data need to be copied.
flags (COPYFLAGS): The copy flags.
trn (c4d.AliasTrans): An alias translator for the operation.
Returns:
True if the data was copied successfully, otherwise False.
"""
# Copies dynamic parameters value to the destination instance of ObjectData
dest.parameters = copy.copy(self.parameters)
return True
def Read(self, node, hf, level):
"""Called by Cinema 4D, when a file with an instance the ObjectData is opened.
The purpose is to read the data you stored in the Write method to restore your object parameter state.
Args:
node (c4d.GeListNode): The instance of the ObjectData.
hf (c4d.storage.HyperFile): The HyperFile to read from.
level (int): The plugin version number for your settings.
Returns:
bool: True if the data has been read successfully, otherwise False.
"""
# Reads the number of dynamic parameters
count = hf.ReadInt32()
# Reads the dynamic parameters value
for idx in range(count):
value = hf.ReadFloat32()
self.parameters.append(value)
return True
def Write(self, node, hf):
"""Called by Cinema 4D, when the document is saved in order to save custom parameters.
The purpose is to write data and read them back with the Read method.
Args:
node (c4d.GeListNode): The instance of the ObjectData.
hf (c4d.storage.HyperFile): The HyperFile to write into.
Returns:
bool: True if the data was written successfully, otherwise False.
"""
# Writes the number of dynamic parameters
count = len(self.parameters)
hf.WriteInt32(count)
# Writes the dynamic parameters value
for value in self.parameters:
hf.WriteFloat32(value)
return True
def GetDDescription(self, node, description, flags):
"""Called by Cinema 4D when the description (UI) is queried.
Args:
node (c4d.GeListNode): The instance of the ObjectData.
description (c4d.Description): The description to modify.
flags: return: The success status or the data to be returned.
Returns:
Union[Bool, tuple(bool, Any, DESCFLAGS_DESC)]: The success status or the data to be returned.
"""
data = node.GetDataInstance()
# Loads the parameters from the description resource before adding dynamic parameters.
if not description.LoadDescription(node.GetType()):
return False
# Get description single ID
singleID = description.GetSingleDescID()
# Declare dynamic group DescID
dynamicGroupID = c4d.DescID(c4d.DescLevel(
OPYDYNAMICPARAMETERSOBJECT_DYNAMICGROUP, c4d.DTYPE_GROUP, node.GetType()))
# Check if dynamic group needs to be added
addDynamicGroup = singleID is None
if not addDynamicGroup:
addDynamicGroup = dynamicGroupID.IsPartOf(singleID)[0]
# Adds dynamic group
if addDynamicGroup:
bc = c4d.GetCustomDataTypeDefault(c4d.DTYPE_GROUP)
bc.SetString(c4d.DESC_NAME, "Dynamic Group")
bc.SetInt32(c4d.DESC_COLUMNS, 1)
if not description.SetParameter(dynamicGroupID, bc, c4d.DescID(
c4d.DescLevel((c4d.ID_OBJECTPROPERTIES)))):
return False
# Declare REAL parameter container
bc = c4d.GetCustomDataTypeDefault(c4d.DTYPE_REAL)
bc.SetInt32(c4d.DESC_CUSTOMGUI, c4d.CUSTOMGUI_REALSLIDER)
bc.SetFloat(c4d.DESC_MIN, 0.0)
bc.SetFloat(c4d.DESC_MAX, 1.0)
bc.SetFloat(c4d.DESC_MINSLIDER, 0.0)
bc.SetFloat(c4d.DESC_MAXSLIDER, 1.0)
bc.SetFloat(c4d.DESC_STEP, 0.01)
bc.SetInt32(c4d.DESC_UNIT, c4d.DESC_UNIT_FLOAT)
bc.SetInt32(c4d.DESC_ANIMATE, c4d.DESC_ANIMATE_ON)
bc.SetBool(c4d.DESC_REMOVEABLE, False)
# Initialize/Update parameters value list if needed
parametersNum = data.GetInt32(c4d.OPYDYNAMICPARAMETERSOBJECT_PARAMETERSNUMBER)
parametersLen = len(self.parameters)
if parametersLen == 0:
self.parameters = [0.0] * parametersNum
elif parametersLen != parametersNum:
if parametersLen < parametersNum:
while parametersLen < parametersNum:
self.parameters.append(0.0)
parametersLen += 1
else:
while parametersLen > parametersNum:
self.parameters.pop()
parametersLen -= 1
# Adds dynamic REAL parameters
for idx in range(parametersNum):
descid = c4d.DescID(c4d.DescLevel(
OPYDYNAMICPARAMETERSOBJECT_DYNAMICGROUP_FIRSTPARAMETER + idx, c4d.DTYPE_REAL, node.GetType()))
addParameter = singleID is None
if not addParameter:
addParameter = descid.IsPartOf(singleID)[0]
if addParameter:
name = "Dynamic REAL " + str(idx + 1)
bc.SetString(c4d.DESC_NAME, name)
bc.SetString(c4d.DESC_SHORT_NAME, name)
if not description.SetParameter(descid, bc, dynamicGroupID):
break
# Calculate random ID in the dynamic parameters range
if parametersNum > 1:
self.randomID = random.randrange(OPYDYNAMICPARAMETERSOBJECT_DYNAMICGROUP_FIRSTPARAMETER,
OPYDYNAMICPARAMETERSOBJECT_DYNAMICGROUP_FIRSTPARAMETER + parametersNum - 1)
# After dynamic parameters have been added successfully, return True and
# c4d.DESCFLAGS_DESC_LOADED with the input flags
return True, flags | c4d.DESCFLAGS_DESC_LOADED
def SetDParameter(self, node, id, data, flags):
"""Called by Cinema 4D, when SetParameter is call from the node.
The main purpose is to store the data for member variable. It is necessary for parameters that are not simply stored in the node's container e.g. class members.
Args:
node (c4d.GeListNode): The instance of the ObjectData.
id (c4d.DescID): The parameter Id.
data (Any): the data, the user defines and we have to store.
flags (DESCFLAGS_SET): The input flags passed to define the operation.
Returns:
Union[Bool, tuple(bool, Any, DESCFLAGS_SET)]: The success status or the data to be returned.
"""
# Retrieves the parameter ID requested
paramID = id[0].id
# Retrieves the parameters count
parametersLen = len(self.parameters)
# Checks if passed parameter ID is a dynamic parameter
if OPYDYNAMICPARAMETERSOBJECT_DYNAMICGROUP_FIRSTPARAMETER <= paramID <= OPYDYNAMICPARAMETERSOBJECT_DYNAMICGROUP + parametersLen:
# Store the parameter data
self.parameters[paramID - OPYDYNAMICPARAMETERSOBJECT_DYNAMICGROUP_FIRSTPARAMETER] = data
return True, flags | c4d.DESCFLAGS_SET_PARAM_SET
return False
def GetDParameter(self, node, id, flags):
"""Called by Cinema 4D, when GetParameter is call from the node.
The main purpose is to return the data for member variable. It is necessary for parameters that are not simply stored in the node's container e.g. class members.
Args:
node (c4d.GeListNode): The instance of the ObjectData.
id (c4d.DescID): The parameter Id.
flags (DESCFLAGS_GET): The input flags passed to define the operation.
Returns:
Union[Bool, tuple(bool, Any, DESCFLAGS_GET)]: The success status or the data to be returned.
"""
# Retrieves the parameter ID requested
paramID = id[0].id
# Retrieves the parameters count
parametersLen = len(self.parameters)
# Checks passed parameter ID is a dynamic parameter
if OPYDYNAMICPARAMETERSOBJECT_DYNAMICGROUP_FIRSTPARAMETER <= paramID <= OPYDYNAMICPARAMETERSOBJECT_DYNAMICGROUP + parametersLen:
# Retrieves the parameter data
data = self.parameters[paramID - OPYDYNAMICPARAMETERSOBJECT_DYNAMICGROUP_FIRSTPARAMETER]
return True, data, flags | c4d.DESCFLAGS_GET_PARAM_GET
return False
def TranslateDescID(self, node, id):
"""Called by the Attribute Manager for every object and every description ID.
Gives a NodeData plugin the opportunity to route a description ID in the description of a node to another one.
Args:
node (c4d.GeListNode): The instance of the ObjectData.
id (c4d.DescID): The parameter Id.
Returns:
Union[Bool, tuple(bool, c4d.DescID, c4d.C4DAtom)]: The success status or the linked DescID
"""
# Retrieves the parameter ID requested
paramID = id[0].id
# OPYDYNAMICPARAMETERSOBJECT_TRANSLATED_PHONG_ANGLE references the first
# attached Phong tag PHONGTAG_PHONG_ANGLE parameter
if paramID == c4d.OPYDYNAMICPARAMETERSOBJECT_TRANSLATED_PHONG_ANGLE:
# Retrieves first Phong Tag
tag = node.GetTag(c4d.Tphong)
if tag is None:
return False
# Retrieves the Phong Tag description
flag = c4d.DESCFLAGS_DESC_NONE if c4d.GetC4DVersion() > 20000 else c4d.DESCFLAGS_DESC_0
desc = tag.GetDescription(flag)
if not desc:
return False
# Retrieve the complete DescID for the PHONGTAG_PHONG_ANGLE parameter
# Builds a DescID with only the parameter ID
descId = c4d.DescID(c4d.DescLevel(c4d.PHONGTAG_PHONG_ANGLE))
# Fills DescLever type and creator
completeId = desc.CheckDescID(descId, None)
return True, completeId, tag
return False
def GetDEnabling(self, node, id, t_data, flags, itemdesc):
"""Called by Cinema 4D to decide which parameters should be enabled or disabled (ghosted).
Args:
node (c4d.BaseObject): The instance of the ObjectData.
id (c4d.DescID): The Description ID of the parameter.
t_data (Any): The current data for the parameter.
flags: Not used
itemdesc (c4d.BaseContainer): The description, encoded to a container.
Returns:
True if the parameter should be enabled, otherwise False.
"""
# Retrieves the actual parameter queried
paramID = id[0].id
# Checks if parameter is the random ID to disable
if paramID == self.randomID:
return False
return True
def GetVirtualObjects(self, op, hh):
"""This method is called automatically when Cinema 4D ask for the cache of an object.
This is also the place where objects have to be marked as input object by touching them (destroy their cache in order to disable them in Viewport)
Args:
op (c4d.BaseObject): The Python Generator c4d.BaseObject.
hh (c4d.HierarchyHelp): Not implemented.
Returns:
c4d.SplineObject: The newly allocated object chain, or None if a memory error occurred.
"""
if op is None or hh is None:
raise RuntimeError("Failed to retrieve op or hh.")
returnObj = c4d.BaseObject(c4d.Osphere)
if returnObj is None:
raise MemoryError("Failed to create a sphere.")
phongTag = op.GetTag(c4d.Tphong)
if phongTag is not None:
phongTagCopy = phongTag.GetClone()
if phongTagCopy is None:
raise MemoryError("Failed to retrieve a clone of the phong tag.")
returnObj.InsertTag(phongTagCopy)
return returnObj
def GetBubbleHelp(self, node):
"""Called by Cinema 4D to create a contextual bubble help and status bar information for the node.
Args:
node (c4d.BaseObject): The instance of the ObjectData.
Returns:
str: The bubble help string.
"""
return "Dynamic Object Bubble Help"
if __name__ == "__main__":
# Registers the object plugin. PLUGINFLAG_HIDEPLUGINMENU hides the natural menu entry of this
# plugin. Omit this flag or replace it by 0 when you do not have custom menu handling as the SDK
# (see py_menu).
c4d.plugins.RegisterObjectPlugin(id=PLUGIN_ID,
str="Py SDK - Dynamic Parameters Object",
g=DynamicParametersObjectData,
description="opydynamicparametersobject",
icon=c4d.bitmaps.InitResourceBitmap(c4d.Onull),
info=c4d.OBJECT_GENERATOR | c4d.PLUGINFLAG_HIDEPLUGINMENU)