Repository navigation
Expand file tree
/
Copy pathpy_obj_custom_icon.pyp
More file actions
128 lines (96 loc) · 4.93 KB
/
Copy pathpy_obj_custom_icon.pyp
File metadata and controls
128 lines (96 loc) · 4.93 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
"""Demonstrates how to implement a scene element that dynamically updates its icon.
The ObjectData plugin fills custom icon settings from the host node and returns a colorized icon using the Cinema 4D R21 custom icon API.
To run this example, place the file in the Cinema 4D plugins folder and restart Cinema 4D.
Topics:
- Providing custom icon settings for an ObjectData plugin
- Deriving icon colors from a scene element
"""
__copyright__ = "Copyright 2026, MAXON Computer"
__author__ = "Maxime Adam"
__date__ = "25/08/2026"
__license__ = "Apache-2.0 license"
__version__ = "21.0.0"
import os
import c4d
import maxon
PLUGIN_ID = 1053134
class CustomIconObjectData(c4d.plugins.ObjectData):
def Init(self, node, isCloneInit=False):
"""Called by Cinema 4D to initialize the instance.
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 on success, otherwise False.
"""
# Creates a BaseContainer to store all custom color mode
iconSpecialModes = c4d.BaseContainer()
# Additional Special Color Mode for light object
iconSpecialModes.SetString(0, "Custom Color Mode")
# Creates a BaseContainer to define the initial iconsetting of the object
iconSettings = c4d.BaseContainer()
# Defines the custom color mode used with the previously created
# BaseContainer already filled
iconSettings.SetContainer(0, iconSpecialModes)
# Sets icon settings container into the current Object instance data container
node.GetDataInstance().SetContainer(c4d.ID_ICONCHOOSER_SETTINGS, iconSettings)
# Default color mode: custom mode
node.GetDataInstance().SetInt32(c4d.ID_BASELIST_ICON_COLORIZE_MODE,
c4d.ID_BASELIST_ICON_COLORIZE_MODE_CUSTOM + 1)
return True
def GetVirtualObjects(self, node, hh):
"""This method is called automatically when Cinema 4D asks 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:
node (c4d.BaseObject): The Python generator.
hh (c4d.HierarchyHelp): The hierarchy helper.
Returns:
c4d.BaseObject: The represented object.
"""
return c4d.BaseObject(c4d.Onull)
def Message(self, node, msgId, data):
"""Called by Cinema 4D part to notify the object to a special event.
Args:
node (c4d.BaseObject): The instance of the ObjectData.
msgId (int): The message ID type.
data (Any): The message data.
Returns:
Any: Depends of the message type, most of the time True.
"""
if msgId == c4d.MSG_GETCUSTOMICON:
settings = c4d.CustomIconSettings()
# Take care due to the nature of python (settings._specialColors returns a copy)
# so it's important to first retrieve the BaseArray and then resize it
# then re-assign it.
arr = settings._specialColors
arr.Resize(1)
# Creates an RGB value for custom color mode
arr[0] = maxon.Color(1, 0, 0)
# Reassigns the BaseArray data to the CustomIconSettings
settings._specialColors = arr
# Fills the CustomIconSettings with the passed BaseContainer object
c4d.CustomIconSettings.FillCustomIconSettingsFromBaseList2D(
settings, node.GetDataInstance(), node.GetType(), True)
# Finally fills the icon Data settings with the CustomIconSettings
c4d.CustomIconSettings.GetCustomIcon(data, settings, True)
return True
if __name__ == "__main__":
# Retrieves the icon path
directory, _ = os.path.split(__file__)
fn = os.path.join(directory, "res", "customicon.tif")
# Creates a BaseBitmap
bmp = c4d.bitmaps.BaseBitmap()
if bmp is None:
raise MemoryError("Failed to create a BaseBitmap.")
# Init the BaseBitmap with the icon
if bmp.InitWith(fn)[0] != c4d.IMAGERESULT_OK:
raise MemoryError("Failed to initialize the BaseBitmap.")
# 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 - Custom Icon Object Data",
g=CustomIconObjectData,
description="py_custom_icon",
icon=bmp,
info=c4d.OBJECT_GENERATOR | c4d.TAG_ICONCHOOSER_PARENT_IGNORE |
c4d.PLUGINFLAG_HIDEPLUGINMENU)