forked from dfelinto/addon-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_shape_alignment.py
391 lines (294 loc) · 11.9 KB
/
object_shape_alignment.py
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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# ########################################
# Shape Alignment Addon
#
# Organize the selected objects in specific shapes.
#
# Dalai Felinto
#
# dalaifelinto.com
# Rio de Janeiro, June 2014
#
# ########################################
import bpy
import math
from mathutils import Euler, Vector
bl_info = {
"name": "Shape Alignment",
"author": "Dalai Felinto (dfelinto)",
"version": (1,0),
"blender": (2, 7, 1),
"location": "Tool Panel",
"description": "",
"warning": "",
"wiki_url": "https://github.com/dfelinto/addon-samples",
"tracker_url": "",
"category": "Object"}
def get_angle(base_dir, ob, center):
angle = base_dir.angle_signed(ob.location.xy - center.xy, 0)
if angle < 0:
angle += math.pi * 2
return angle
def get_xy_corner(vector2d):
"""return a vector that points to the closest corner:
1,1; 1,-1; -1,-1, -1, 1
"""
diagonal = vector2d.length
half_side = 0.5 * math.sqrt(2 * (diagonal ** 2))
angle = vector2d.angle_signed(Vector((1,1)), 0)
if angle < 0:
angle += math.pi * 2
if angle <= math.radians(45) or \
angle > math.radians(360 - 45):
return Vector((half_side, half_side))
elif angle > math.radians(45) and \
angle <= math.radians(90 + 45):
return Vector((-half_side, half_side))
elif angle > math.radians(90 + 45) and \
angle <= math.radians(180 + 45):
return Vector((-half_side, -half_side))
else:
return Vector((half_side, -half_side))
def get_xy(vector2d):
"""return a vector that points to the closest direction:
1,0; 0,-1; -1,0; 0,1
"""
length = vector2d.length
angle = vector2d.angle_signed(Vector((1,0)), 0)
if angle < 0:
angle += math.pi * 2
if angle <= math.radians(45) or \
angle > math.radians(360 - 45):
return Vector((length, 0))
elif angle > math.radians(45) and \
angle <= math.radians(90 + 45):
return Vector((0, length))
elif angle > math.radians(90 + 45) and \
angle <= math.radians(180 + 45):
return Vector((-length, 0))
else:
return Vector((0, -length))
def shape_circle(context, orientation):
center = context.scene.cursor_location
active = context.active_object
zed = active.location[2]
base_dir = active.location.xy - center.xy
if orientation == 'XY':
zero_dir = get_xy(base_dir).resized(3)
else:
zero_dir = base_dir.xy.resized(3)
num_objects = len(context.selected_objects)
delta_angle = 2 * math.pi / num_objects
# sort objects based on angle to center
sorted_objects = sorted(context.selected_objects, key=lambda ob: get_angle(base_dir, ob, center))
for i in range(num_objects):
angle = delta_angle * i
euler = Euler((0, 0, -angle))
direction = Vector(zero_dir)
direction.rotate(euler)
sorted_objects[i].location = center + direction
sorted_objects[i].location[2] = zed
def shape_line(context, orientation):
center = context.scene.cursor_location
active = context.active_object
zed = active.location[2]
origin = active.location.xy
base_dir = active.location.xy - center.xy
length = base_dir.length
#ortho_dir = Vector(base_dir).rotate(Euler((0, 0, math.pi * 0.5)))
if orientation == 'XY':
line_dir = get_xy(base_dir).resized(3)
else:
line_dir = base_dir.xy.resized(3)
line_dir.normalize()
pre_objects = [ob for ob in context.selected_objects if ob != active and math.fabs(base_dir.angle_signed(ob.location.xy - origin, 0)) < math.pi * 0.5]
post_objects = [ob for ob in context.selected_objects if ob != active and ob not in pre_objects]
num_objects = len(context.selected_objects)
if len(post_objects) > 1:
step = length / len(post_objects)
else:
step = length
#XXX should use cross or dot instead
pre_objects = sorted(pre_objects, key=lambda ob: (ob.location.xy - origin).length)
post_objects = sorted(post_objects, key=lambda ob: (ob.location.xy - origin).length)
for i, ob in enumerate(pre_objects):
ob.location.xy = origin + (line_dir.xy * (i + 1) * step)
for i, ob in enumerate(post_objects):
ob.location.xy = origin - (line_dir.xy * (i + 1) * step)
def shape_square(context, orientation):
center = context.scene.cursor_location
active = context.active_object
zed = active.location[2]
base_dir = active.location.xy - center.xy
diagonal = base_dir.length
if orientation == 'XY':
zero_dir = get_xy_corner(base_dir).resized(3)
else:
zero_dir = base_dir.xy.resized(3)
num_objects = len(context.selected_objects)
num_side_objects = (num_objects // 4) - 1
ortho_angle = math.pi * 0.5
ortho_dir = Vector(zero_dir)
ortho_dir.rotate(Euler((0, 0, ortho_angle * 0.5)))
ortho_dir.normalize()
# sort objects based on angle to center
sorted_objects = sorted(context.selected_objects, key=lambda ob: get_angle(base_dir, ob, center))
corners = []
for i in range(0, num_objects, num_side_objects + 1):
corners.append(sorted_objects[i])
assert(len(corners) == 4)
sides = [ob for ob in sorted_objects if ob not in corners]
side_step = math.sqrt(2 * (diagonal ** 2)) / (num_side_objects + 1)
for i in range(4):
# corner
angle = ortho_angle * i
euler = Euler((0, 0, -angle))
direction = Vector(zero_dir)
direction.rotate(euler)
corners[i].location = center + direction
corners[i].location[2] = zed
side_dir = Vector(ortho_dir)
side_dir.rotate(euler)
for j in range(num_side_objects):
ob = sides[(i * num_side_objects) + j]
step = (j + 1) * side_step
ob.location = center + direction
ob.location.xy -= side_dir.xy * step
ob.location[2] = zed
class OBJECT_OT_ShapeAlignment(bpy.types.Operator):
"""Align and distribute selected objects into shapes"""
bl_idname = "object.shape_alignment"
bl_label = "Shape Alignment"
shape = bpy.props.EnumProperty(
name="Shape",
description="",
items=(
('LINE', "Line", ""),
('SQUARE', "Square", ""),
('CIRCLE', "Circle", ""),
),
default='LINE')
orientation = bpy.props.EnumProperty(
name="Orientation",
description="",
items=(
('XY', "XY", ""),
('NONE', "None", ""),
),
default='NONE')
show_help = bpy.props.BoolProperty(
name="Help",
description="",
default=False,
options={'HIDDEN', 'SKIP_SAVE'})
@classmethod
def poll(cls, context):
return context.active_object and context.active_object.select
def help_draw(self, _self, context):
layout = _self.layout
col = layout.column()
col.label(text="Before calling this operator pre-arrange your objects in the final shape you want")
col.label(text="This way the final order of the elements will match its original arrangement")
col.separator()
col.label(text="Circle", icon='MESH_CIRCLE')
col.label(text="Creates a circle around the 3d cursor using the active object as a reference for the radius")
col.label(text="It requires at least three selected objects")
col.separator()
col.label(text="Square", icon='MESH_PLANE')
col.label(text="Creates a square around the 3d cursor using the active object as one of the corners")
col.label(text="It requires multiple of four selected objects (4, 8, 12, ...)")
col.separator()
col.label(text="Line", icon='ZOOMOUT')
col.label(text="Creates a line that passes through the the active object and ends in the 3d cursor")
col.label(text="It requires at least two selected objects")
def execute(self, context):
if self.show_help:
context.window_manager.popup_menu(self.help_draw, title='Help', icon='QUESTION')
return {'CANCELLED'}
error_message = self.check_errors(len(context.selected_objects), self.shape)
if error_message:
self.report({'ERROR'}, error_message)
if self.shape == 'CIRCLE':
shape_circle(context, self.orientation)
elif self.shape == 'LINE':
shape_line(context, self.orientation)
elif self.shape == 'SQUARE':
shape_square(context, self.orientation)
return {'FINISHED'}
def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_props_dialog(self, width=200)
def check_errors(self, num_objects, shape):
if num_objects == 0:
return "No selected object"
if shape == 'SQUARE' and \
num_objects % 4 != 0:
return "Square shape requires selected objects to be a multiple of 4 (4, 8, 12, 16, ...)"
elif self.shape == 'LINE' and \
num_objects < 2:
return "Line shape requires at least 2 selected objects"
elif self.shape == 'CIRCLE' and \
num_objects < 3:
return "Circle shape requires at least 3 selected objects"
return None
def draw(self, context):
layout = self.layout
col = layout.column()
col.prop(self, "shape", text="")
col.prop(self, "orientation")
class VIEW3D_PT_tools_shape(bpy.types.Panel):
bl_category = "Tools"
bl_context = "objectmode"
bl_label = "Shape Alignment"
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
def draw(self, context):
layout = self.layout
layout.operator_context = 'EXEC_REGION_WIN'
scene = context.scene
orientation = 'XY' if scene.use_shape_align_orientation else 'NONE'
col = layout.column()
row = col.row()
ops = row.operator("object.shape_alignment", text="", icon='MESH_CIRCLE', emboss=False)
ops.shape = 'CIRCLE'
ops.orientation = orientation
ops = row.operator("object.shape_alignment", text="", icon='MESH_PLANE', emboss=False)
ops.shape = 'SQUARE'
ops.orientation = orientation
ops = row.operator("object.shape_alignment", text="", icon='ZOOMOUT', emboss=False)
ops.shape = 'LINE'
ops.orientation = orientation
row.operator("object.shape_alignment", text="", icon='QUESTION', emboss=False).show_help = True
row = col.row()
row.prop(scene, "use_shape_align_orientation")
def register():
bpy.utils.register_class(OBJECT_OT_ShapeAlignment)
bpy.utils.register_class(VIEW3D_PT_tools_shape)
bpy.types.Scene.use_shape_align_orientation = bpy.props.BoolProperty(
name="Align to XY",
default=False,
description="Align the shape to the scene axis")
def unregister():
del bpy.types.Scene.use_shape_align_orientation
bpy.utils.unregister_class(VIEW3D_PT_tools_shape)
bpy.utils.unregister_class(OBJECT_OT_ShapeAlignment)
if __name__ == "__main__":
register()
# test call
bpy.ops.object.shape_alignment()