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
|
# gpl author: Mano-Wii
bl_info = {
"name": "Arrange on Curve",
"author": "Mano-Wii",
"version": (6, 3, 0),
"blender": (2, 7, 7),
"location": "3D View > Toolshelf > Create > Arrange on Curve",
"description": "Arrange objects along a curve",
"warning": "Select curve",
"wiki_url": "",
"category": "3D View"
}
# Note: scene properties are moved into __init__
# search for patterns advanced_objects and adv_obj
import bpy
import mathutils
from bpy.types import (
Operator,
Panel,
)
from bpy.props import (
EnumProperty,
FloatProperty,
IntProperty,
)
FLT_MIN = 0.004
class PanelDupliCurve(Panel):
bl_idname = "VIEW3D_PT_arranjar_numa_curva"
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
bl_context = "objectmode"
bl_category = "Create"
bl_label = "Arrange on Curve"
bl_options = {'DEFAULT_CLOSED'}
@classmethod
def poll(cls, context):
return context.object and context.mode == 'OBJECT' and context.object.type == 'CURVE'
def draw(self, context):
layout = self.layout
adv_obj = context.scene.advanced_objects
layout.prop(adv_obj, "arrange_c_use_selected")
if not adv_obj.arrange_c_use_selected:
layout.prop(adv_obj, "arrange_c_select_type", expand=True)
if adv_obj.arrange_c_select_type == 'O':
layout.column(align=True).prop_search(
adv_obj, "arrange_c_obj_arranjar",
bpy.data, "objects"
)
elif adv_obj.arrange_c_select_type == 'G':
layout.column(align=True).prop_search(
adv_obj, "arrange_c_obj_arranjar",
bpy.data, "collections"
)
if context.object.type == 'CURVE':
layout.operator("object.arranjar_numa_curva", text="Arrange Objects")
class DupliCurve(Operator):
bl_idname = "object.arranjar_numa_curva"
bl_label = "Arrange Objects along a Curve"
bl_description = "Arange chosen / selected objects along the Active Curve"
bl_options = {'REGISTER', 'UNDO'}
use_distance: EnumProperty(
name="Arrangement",
items=[
("D", "Distance", "Objects are arranged depending on the distance", 0),
("Q", "Quantity", "Objects are arranged depending on the quantity", 1),
("R", "Range", "Objects are arranged uniformly between the corners", 2)
]
)
distance: FloatProperty(
name="Distance",
description="Distance between Objects",
default=1.0,
min=FLT_MIN,
soft_min=0.1,
unit='LENGTH',
)
object_qt: IntProperty(
name="Quantity",
description="Object amount",
default=2,
min=0,
)
scale: FloatProperty(
name="Scale",
description="Object Scale",
default=1.0,
min=FLT_MIN,
unit='LENGTH',
)
Yaw: FloatProperty(
name="X",
description="Rotate around the X axis (Yaw)",
default=0.0,
unit='ROTATION'
)
Pitch: FloatProperty(
default=0.0,
description="Rotate around the Y axis (Pitch)",
name="Y",
unit='ROTATION'
)
Roll: FloatProperty(
default=0.0,
description="Rotate around the Z axis (Roll)",
name="Z",
unit='ROTATION'
)
max_angle: FloatProperty(
default=1.57079,
max=3.141592,
name="Angle",
unit='ROTATION'
)
offset: FloatProperty(
default=0.0,
name="Offset",
unit='LENGTH'
)
@classmethod
def poll(cls, context):
return context.mode == 'OBJECT'
def draw(self, context):
layout = self.layout
col = layout.column()
col.prop(self, "use_distance", text="")
col = layout.column(align=True)
if self.use_distance == "D":
col.prop(self, "distance")
elif self.use_distance == "Q":
col.prop(self, "object_qt")
else:
col.prop(self, "distance")
col.prop(self, "max_angle")
col.prop(self, "offset")
col = layout.column(align=True)
col.prop(self, "scale")
col.prop(self, "Yaw")
col.prop(self, "Pitch")
col.prop(self, "Roll")
def Glpoints(self, curve):
Gpoints = []
for i, spline in enumerate(curve.data.splines):
segments = len(spline.bezier_points)
if segments >= 2:
r = spline.resolution_u + 1
points = []
for j in range(segments):
bp1 = spline.bezier_points[j]
inext = (j + 1)
if inext == segments:
if not spline.use_cyclic_u:
break
inext = 0
bp2 = spline.bezier_points[inext]
if bp1.handle_right_type == bp2.handle_left_type == 'VECTOR':
_points = (bp1.co, bp2.co) if j == 0 else (bp2.co,)
else:
knot1 = bp1.co
handle1 = bp1.handle_right
handle2 = bp2.handle_left
knot2 = bp2.co
_points = mathutils.geometry.interpolate_bezier(knot1, handle1, handle2, knot2, r)
points.extend(_points)
Gpoints.append(tuple((curve.matrix_world * p for p in points)))
elif len(spline.points) >= 2:
l = [curve.matrix_world * p.co.xyz for p in spline.points]
if spline.use_cyclic_u:
l.append(l[0])
Gpoints.append(tuple(l))
if self.use_distance == "R":
max_angle = self.max_angle
tmp_Gpoints = []
sp = Gpoints[i]
sp2 = [sp[0], sp[1]]
lp = sp[1]
v1 = lp - sp[0]
for p in sp[2:]:
v2 = p - lp
try:
if (3.14158 - v1.angle(v2)) < max_angle:
tmp_Gpoints.append(tuple(sp2))
sp2 = [lp]
except Exception as e:
print("\n[Add Advanced Objects]\nOperator: "
"object.arranjar_numa_curva\nError: {}".format(e))
pass
sp2.append(p)
v1 = v2
lp = p
tmp_Gpoints.append(tuple(sp2))
Gpoints = Gpoints[:i] + tmp_Gpoints
lengths = []
if self.use_distance != "D":
for sp in Gpoints:
lp = sp[1]
leng = (lp - sp[0]).length
for p in sp[2:]:
leng += (p - lp).length
lp = p
lengths.append(leng)
return Gpoints, lengths
def execute(self, context):
if context.object.type != 'CURVE':
return {'CANCELLED'}
curve = context.active_object
Gpoints, lengs = self.Glpoints(curve)
adv_obj = context.scene.advanced_objects
if adv_obj.arrange_c_use_selected:
G_Objeto = context.selected_objects
G_Objeto.remove(curve)
if not G_Objeto:
return {'CANCELLED'}
elif adv_obj.arrange_c_select_type == 'O':
G_Objeto = bpy.data.objects[adv_obj.arrange_c_obj_arranjar],
elif adv_obj.arrange_c_select_type == 'G':
G_Objeto = bpy.data.collections[adv_obj.arrange_c_obj_arranjar].objects
yawMatrix = mathutils.Matrix.Rotation(self.Yaw, 4, 'X')
pitchMatrix = mathutils.Matrix.Rotation(self.Pitch, 4, 'Y')
rollMatrix = mathutils.Matrix.Rotation(self.Roll, 4, 'Z')
max_angle = self.max_angle # max_angle is called in Glpoints
if self.use_distance == "D":
dist = self.distance
for sp_points in Gpoints:
dx = 0.0 # Length of initial calculation of section
last_point = sp_points[0]
j = 0
for point in sp_points[1:]:
vetorx = point - last_point # Vector spline section
quat = mathutils.Vector.to_track_quat(vetorx, 'X', 'Z') # Tracking the selected objects
quat = quat.to_matrix().to_4x4()
v_len = vetorx.length
if v_len > 0.0:
dx += v_len # Defined length calculation equal total length of the spline section
v_norm = vetorx / v_len
while dx > dist:
object = G_Objeto[j % len(G_Objeto)]
j += 1
dx -= dist # Calculating the remaining length of the section
obj = object.copy()
context.scene.objects.link(obj)
obj.matrix_world = quat * yawMatrix * pitchMatrix * rollMatrix
# Placing in the correct position
obj.matrix_world.translation = point - v_norm * dx
obj.scale *= self.scale
last_point = point
elif self.use_distance == "Q":
object_qt = self.object_qt + 1
for i, sp_points in enumerate(Gpoints):
dx = 0.0 # Length of initial calculation of section
dist = lengs[i] / object_qt
last_point = sp_points[0]
j = 0
for point in sp_points[1:]:
vetorx = point - last_point # Vector spline section
# Tracking the selected objects
quat = mathutils.Vector.to_track_quat(vetorx, 'X', 'Z')
quat = quat.to_matrix().to_4x4()
v_len = vetorx.length
if v_len > 0.0:
# Defined length calculation equal total length of the spline section
dx += v_len
v_norm = vetorx / v_len
while dx > dist:
object = G_Objeto[j % len(G_Objeto)]
j += 1
dx -= dist # Calculating the remaining length of the section
obj = object.copy()
context.scene.objects.link(obj)
obj.matrix_world = quat * yawMatrix * pitchMatrix * rollMatrix
# Placing in the correct position
obj.matrix_world.translation = point - v_norm * dx
obj.scale *= self.scale
last_point = point
else:
dist = self.distance
offset2 = 2 * self.offset
for i, sp_points in enumerate(Gpoints):
leng = lengs[i] - offset2
rest = leng % dist
offset = offset2 + rest
leng -= rest
offset /= 2
last_point = sp_points[0]
dx = dist - offset # Length of initial calculation of section
j = 0
for point in sp_points[1:]:
vetorx = point - last_point # Vector spline section
# Tracking the selected objects
quat = mathutils.Vector.to_track_quat(vetorx, 'X', 'Z')
quat = quat.to_matrix().to_4x4()
v_len = vetorx.length
if v_len > 0.0:
dx += v_len
v_norm = vetorx / v_len
while dx >= dist and leng >= 0.0:
leng -= dist
dx -= dist # Calculating the remaining length of the section
object = G_Objeto[j % len(G_Objeto)]
j += 1
obj = object.copy()
context.scene.objects.link(obj)
obj.matrix_world = quat * yawMatrix * pitchMatrix * rollMatrix
# Placing in the correct position
obj.matrix_world.translation = point - v_norm * dx
obj.scale *= self.scale
last_point = point
return {"FINISHED"}
def register():
bpy.utils.register_class(PanelDupliCurve)
bpy.utils.register_class(DupliCurve)
def unregister():
bpy.utils.unregister_class(PanelDupliCurve)
bpy.utils.unregister_class(DupliCurve)
if __name__ == "__main__":
register()
|