Repository navigation
Expand file tree
/
Copy pathpy_tool_liquid_painter.pyp
More file actions
283 lines (217 loc) · 10.8 KB
/
Copy pathpy_tool_liquid_painter.pyp
File metadata and controls
283 lines (217 loc) · 10.8 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
"""Demonstrates how to implement a simple tool.
The tool combines a metaball and a sphere while illustrating the tool lifecycle, mouse input, viewport drawing, cursor information, and a subdialog.
To run this example, place the file in the Cinema 4D plugins folder and restart Cinema 4D.
Topics:
- Implementing a ToolData interaction lifecycle
- Combining metaball and sphere objects for painting
- Handling mouse input and viewport drawing
"""
__copyright__ = "Copyright 2026, MAXON Computer"
__author__ = "Maxime Adam"
__date__ = "25/08/2026"
__license__ = "Apache-2.0 license"
__version__ = "12.0.0"
import c4d
import os
# Be sure to use a unique ID obtained from developers.maxon.net
PLUGIN_ID = 1025247
# Values must match with the header file, usd by c4d.plugins.GeLoadString
IDS_PRIMITIVETOOL = 50000
class SettingsDialog(c4d.gui.SubDialog):
"""Dialog to display option in the ToolData, in this case the Sphere size."""
parameters = {}
def __init__(self, arg):
# Checks if the argument passed is a dictionary
if not isinstance(arg, dict):
raise TypeError("arg is not a dict.")
self.parameters = arg
def CreateLayout(self):
"""This Method is called automatically when Cinema 4D Create the Layout (display) of the GeDialog."""
value = getattr(self.parameters, "sphere_size", 15)
# Creates a Group to align 2 items
if self.GroupBegin(id=1000, flags=c4d.BFH_SCALEFIT, cols=2, rows=1):
self.GroupBorderSpace(10, 10, 10, 10)
# Creates a Static text and a number input
self.AddStaticText(id=1001, flags=c4d.BFH_MASK, initw=120,
name="Sphere Size", borderstyle=c4d.BORDER_NONE)
self.AddEditNumberArrows(id=1002, flags=c4d.BFH_MASK)
# Defines the default values
self.SetFloat(id=1002, value=value, min=0, max=20)
self.GroupEnd()
return True
def Command(self, messageId, bc):
"""This Method is called automatically when the user clicks on a gadget and/or changes its value this function will be called.
It is also called when a string menu item is selected.
Args:
messageId (int): The ID of the gadget that triggered the event.
bc (c4d.BaseContainer): The original message container
Returns:
False if there was an error, otherwise True.
"""
# When the user change the Gadget with the ID 1002 (the input number field)
if id == 1002:
# Stores the value in the parameter variable
self.parameters['sphere_size'] = self.GetFloat(1002)
return True
class LiquidTool(c4d.plugins.ToolData):
"""Inherit from ToolData to create your own tool"""
def __init__(self):
self.data = {'sphere_size': 15}
def GetState(self, doc):
"""Called by Cinema 4D to know if the tool can be used currently
Args:
doc (c4d.documents.BaseDocument): The current active document.
Returns:
True if the tool can be used, otherwise False.
"""
if doc.GetMode() == c4d.Mpaint:
return False
return c4d.CMD_ENABLED
def MouseInput(self, doc, data, bd, win, msg):
"""Called by Cinema 4D, when the user click on the viewport and the tool is active.
Mainly the place to do moue interaction from the viewport.
Args:
doc (c4d.documents.BaseDocument): The current active document.
data (c4d.BaseContainer): The tool settings container.
bd (c4d.BaseDraw): The BaseDraw object of the active editor view.
win (c4d.gui.EditorWindow): The EditorWindow object for the active editor view.
msg (c4d.BaseContainer): The original message container.
Returns:
False if a problem occurred during this function.
"""
# Retrieves which clicks is currently clicked
device = 0
if msg[c4d.BFM_INPUT_CHANNEL] == c4d.BFM_INPUT_MOUSELEFT:
device = c4d.KEY_MLEFT
elif msg[c4d.BFM_INPUT_CHANNEL] == c4d.BFM_INPUT_MOUSERIGHT:
device = c4d.KEY_MRIGHT
else:
return True
# Creates a MetaBall object
metaball = c4d.BaseObject(c4d.Ometaball)
if metaball is None:
raise MemoryError("Failed to create a Metaball.")
# Defines some settings and create a Phong Tag
metaball[c4d.METABALLOBJECT_SUBEDITOR] = 10
pTag = metaball.MakeTag(c4d.Tphong)
if pTag is None:
raise MemoryError("Failed to create a Phong Tag.")
# Adds an undo step
doc.AddUndo(c4d.UNDOTYPE_NEWOBJ, metaball)
# Inserts the object in the active document and set it as active one.
doc.InsertObject(metaball)
doc.SetActiveObject(metaball)
# Updates the Viewport (so the metaball is drawn)
c4d.DrawViews(c4d.DRAWFLAGS_ONLY_ACTIVE_VIEW |
c4d.DRAWFLAGS_NO_THREAD | c4d.DRAWFLAGS_NO_ANIMATION)
# Retrieves the X/Y screen position of the mouse.
mx = msg[c4d.BFM_INPUT_X]
my = msg[c4d.BFM_INPUT_Y]
# Start a Dragging session
win.MouseDragStart(button=device, mx=int(mx), my=int(
my), flags=c4d.MOUSEDRAGFLAGS_DONTHIDEMOUSE | c4d.MOUSEDRAGFLAGS_NOMOVE)
result, dx, dy, channel = win.MouseDrag()
# While the Mouse is still in the Dragging (clicked) state
while result == c4d.MOUSEDRAGRESULT_CONTINUE:
# If user doesnt move the mouse simply updates frag information
if dx == 0.0 and dy == 0.0:
result, dx, dy, channel = win.MouseDrag()
continue
# Offsets the original position with the delta of the dragging
mx += dx
my += dy
# Creates a Sphere
sphere = c4d.BaseObject(c4d.Osphere)
if sphere is None:
raise MemoryError("Failed to create a Sphere.")
# Defines the position of the sphere from Screen Space to World Space
sphere.SetAbsPos(bd.SW(c4d.Vector(mx, my, 500.0)))
# Defines the sphere radius
sphere[c4d.PRIM_SPHERE_RAD] = self.data["sphere_size"]
# Inserts it under the metaball
sphere.InsertUnder(metaball)
# Updates the Viewport (so the metaball with the newly created sphere is drawn)
c4d.DrawViews(c4d.DRAWFLAGS_ONLY_ACTIVE_VIEW |
c4d.DRAWFLAGS_NO_THREAD | c4d.DRAWFLAGS_NO_ANIMATION)
# Updates drag information
result, dx, dy, channel = win.MouseDrag()
# If the user press ESC while dragging, do an Undo (remove the Metaball Object)
if win.MouseDragEnd() == c4d.MOUSEDRAGRESULT_ESCAPE:
doc.DoUndo(True)
# Pushes an update event to Cinema 4D
c4d.EventAdd()
return True
def Draw(self, doc, data, bd, bh, bt, flags):
"""Called by Cinema 4D when the display is updated to display some visual element of your tool in the 3D view.
Args:
doc (c4d.documents.BaseDocument): The current active document.
data (c4d.BaseContainer.): The tool settings container.
bd (c4d.BaseDraw): The editor's view.
bh (c4d.plugins.BaseDrawHelp): The BaseDrawHelp editor's view.
bt (c4d.threading.BaseThread): The calling thread.
flags (TOOLDRAWFLAGS): The current drawing pass.
Returns:
The result of the drawing (most likely c4d.DRAWRESULT_OK)
"""
# Resets the drawing matrix to the world space matrix.
bd.SetMatrix_Matrix(None, c4d.Matrix())
# If the DrawPass is the Highlight one
if flags & c4d.TOOLDRAWFLAGS_HIGHLIGHT:
p = [c4d.Vector(), c4d.Vector(100, 0, 0), c4d.Vector(50, 100, 0)]
f = [c4d.Vector(1, 0, 0), c4d.Vector(1, 0, 0), c4d.Vector(1, 0, 0)]
# If the DrawPass asks the inverse Z (ignore it, for 2D drawing)
elif flags & c4d.TOOLDRAWFLAGS_INVERSE_Z:
p = [c4d.Vector(), c4d.Vector(100, 0, 0), c4d.Vector(50, -100, 0)]
f = [c4d.Vector(0, 0, 1), c4d.Vector(0, 0, 1), c4d.Vector(0, 0, 1)]
# In any other cases
else:
p = [c4d.Vector(), c4d.Vector(-100, 0, 0), c4d.Vector(-50, 100, 0)]
f = [c4d.Vector(0, 1, 0), c4d.Vector(0, 1, 0), c4d.Vector(0, 1, 0)]
# Draw the polygon
bd.DrawPolygon(p, f)
return c4d.TOOLDRAW_HANDLES | c4d.TOOLDRAW_AXIS
def GetCursorInfo(self, doc, data, bd, x, y, bc):
"""Called by Cinema 4D when the cursor is over editor window to get the state of the mouse pointer.
Args:
doc (c4d.documents.BaseDocument): The current active document.
data (c4d.BaseContainer): The tool settings container.
bd (c4d.BaseDraw): The editor's view.
x (float): The x coordinate of the mouse cursor relative to the top-left of the currently active editor view.
y (float): The y coordinate of the mouse cursor relative to the top-left of the currently active editor view.
bc (c4d.BaseContainer): The container to store the result in.
"""
# If the cursor has left a user area, simply return True
if bc.GetId() == c4d.BFM_CURSORINFO_REMOVE:
return True
# Sets the BubbleHelp string and cursor.
bc.SetString(c4d.RESULT_BUBBLEHELP, c4d.plugins.GeLoadString(IDS_PRIMITIVETOOL))
bc.SetInt32(c4d.RESULT_CURSOR, c4d.MOUSE_POINT_HAND)
return True
def AllocSubDialog(self, bc):
"""Called by Cinema 4D To allocate the Tool Dialog Option.
Args:
bc (c4d.BaseContainer): Currently not used.
Returns:
The allocated sub dialog.
"""
return SettingsDialog(getattr(self, "data", {'sphere_size': 15}))
if __name__ == "__main__":
# Retrieves the icon path
directory, _ = os.path.split(__file__)
fn = os.path.join(directory, "res", "liquid.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 tool 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
# has (see py_menu).
c4d.plugins.RegisterToolPlugin(id=PLUGIN_ID,
str="Py SDK - Liquid Painter",
info=c4d.PLUGINFLAG_HIDEPLUGINMENU,
icon=bmp,
help="This string is shown in the statusbar",
dat=LiquidTool())