This repository has been archived by the owner on Nov 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_ops.py
281 lines (232 loc) · 10 KB
/
array_ops.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
import bpy
from bpy.types import Operator, Panel
from bpy.props import FloatVectorProperty, FloatProperty, IntProperty
import math
from mathutils import Vector
"""
TODO:
O Write angle and count to top
"""
class HSU_CircularArrayOperator(Operator):
bl_idname = "object.circular_array"
bl_label = "Circular Array"
bl_description = "Create a circular array of selected objects around an empty"
bl_options = {'UNDO'}
# Properties
instance_count: IntProperty(name="Count", default=3)
empty_size: FloatProperty(name="Size", default=1.0, min=0)
empty_rotation: FloatVectorProperty(name="Rotation", default=(0.0, 0.0, 0.0), subtype="EULER")
axis_keys = {'X': (1, 0, 0), 'Y': (0, 1, 0), 'Z': (0, 0, 1)}
rotation_axis = (0, 0, 0)
base_location = Vector((0, 0, 0))
old_mouse_region_x = 0
modifiers = []
empty = None
@classmethod
def poll(cls, context):
return (context.active_object is not None or len(context.selected_objects) > 1)
def __init__(self):
self.modifiers = []
self.empty = None
#print("Start")
def __del__(self):
#print("End")
pass
"""
def draw(self, context):
layout = self.layout
layout.prop(self, "instance_count")
layout.prop(self, "empty_rotation")
layout.prop(self, "empty_size")
"""
def modal(self, context, event):
if event.type == 'MOUSEMOVE':
self.empty_rotation = tuple([math.radians(event.mouse_region_x*(0.1 if event.shift else 0.5)*x) for x in self.rotation_axis])
#print(f"moved: {self.empty_rotation}")
elif event.type == 'WHEELUPMOUSE':
self.instance_count += 1
if self.instance_count <= 0:
self.instance_count = 1
self.empty_rotation = tuple([math.radians(360/self.instance_count*x) for x in self.rotation_axis])
elif event.type == 'WHEELDOWNMOUSE':
self.instance_count -= 1
if self.instance_count <= 0:
self.instance_count = 1
self.empty_rotation = tuple([math.radians(360/self.instance_count*x) for x in self.rotation_axis])
elif event.type in {'X', 'Y', 'Z'}:
self.rotation_axis = self.axis_keys[event.type]
elif event.type in {'ESC', 'RIGHTMOUSE'}: # Cancel
return {'CANCELLED'}
elif event.type == 'LEFTMOUSE': # Confirm
self.execute(context)
return {'FINISHED'}
self.execute(context)
return {'RUNNING_MODAL'}
def invoke(self, context, event):
wm = context.window_manager
# self.rotation_axis = context.region_data.view_rotation.axis*Vector((0, 0, -1))
wm.modal_handler_add(self)
return {'RUNNING_MODAL'}
def update(self, context):
if not self.empty:
return
self.empty.location = self.base_location
self.empty.empty_display_size = self.empty_size
self.empty.rotation_euler = self.empty_rotation
for modifier in self.modifiers:
modifier.count = self.instance_count
def execute(self, context):
# Get active object and selected objects
active_obj = context.active_object
selected_objs = context.selected_objects
if active_obj in selected_objs:
selected_objs.remove(active_obj)
self.base_location = active_obj.location
if len(selected_objs) == 0 and active_obj:
selected_objs.append(active_obj)
try:
if self.modifiers:
self.update(context)
return{'FINISHED'}
except ReferenceError:
# continue
pass
# Create an empty at the active object's location
self.empty = bpy.data.objects.new(f"{active_obj.name}-CIRC_ARR", None)
self.empty.location = self.base_location
self.empty.empty_display_size = self.empty_size
self.empty.rotation_euler = self.empty_rotation
context.collection.objects.link(self.empty)
# Parent the empty to the active object
self.empty.parent = active_obj
cursor_location = context.scene.cursor.location
context.scene.cursor.location = active_obj.location
# Loop through selected objects and add array modifier
for obj in selected_objs:
# Add array modifier
array_modifier = obj.modifiers.new(name="Array", type='ARRAY')
array_modifier.count = self.instance_count
array_modifier.use_relative_offset = False
array_modifier.use_object_offset = True
array_modifier.offset_object = self.empty
self.modifiers.append(array_modifier)
# Make selected objects children of the active object
if obj is not active_obj:
obj.parent = active_obj
obj.matrix_parent_inverse = active_obj.matrix_world.inverted()
# Set object origin
bpy.context.view_layer.objects.active = obj
bpy.ops.object.origin_set(type='ORIGIN_CURSOR', center='MEDIAN')
context.scene.cursor.location = cursor_location
return {'FINISHED'}
class HSU_LinearArrayOperator(Operator):
bl_idname = "object.linear_array"
bl_label = "Linear Array"
bl_description = "Create a linear array of selected objects to an empty"
bl_options = {'REGISTER', 'UNDO'}
# Properties
instance_count: IntProperty(name="Count", default=3)
empty_location: FloatVectorProperty(name="Location", default=(0.0, 0.0, 0.0), subtype="XYZ")
empty_size: FloatProperty(name="Size", default=1.0, min=0)
axis_keys = {'X': (1, 0, 0), 'Y': (0, 1, 0), 'Z': (0, 0, 1)}
location_axis = (0, 0, 0)
base_location = Vector((0, 0, 0))
old_mouse_region_x = 0
modifiers = []
empty = None
@classmethod
def poll(cls, context):
return (context.active_object is not None or len(context.selected_objects) > 1)
def __init__(self):
self.modifiers = []
self.empty = None
#print("Start")
def __del__(self):
self.base_location = Vector((0, 0, 0))
self.empty_location = Vector((0, 0, 0))
#print("End")
"""
def draw(self, context):
layout = self.layout
layout.prop(self, "instance_count")
layout.prop(self, "empty_location")
layout.prop(self, "empty_size")
"""
def modal(self, context, event):
if event.type == 'MOUSEMOVE':
diff = self.old_mouse_region_x-event.mouse_region_x
self.empty_location = Vector(tuple([-diff*(0.1 if event.shift else 0.5)*x for x in self.location_axis]))
#print(f"{self.base_location} {self.empty_location} + {self.old_mouse_region_x} + {event.mouse_region_x} to {self.location_axis}")
elif event.type == 'WHEELUPMOUSE':
self.instance_count += 1
if self.instance_count <= 0:
self.instance_count = 1
elif event.type == 'WHEELDOWNMOUSE':
self.instance_count -= 1
if self.instance_count <= 0:
self.instance_count = 1
elif event.type in {'X', 'Y', 'Z'}:
self.location_axis = self.axis_keys[event.type]
elif event.type in {'ESC', 'RIGHTMOUSE'}: # Cancel
return {'CANCELLED'}
elif event.type == 'LEFTMOUSE': # Confirm
self.execute(context)
return {'FINISHED'}
self.execute(context)
return {'RUNNING_MODAL'}
def invoke(self, context, event):
wm = context.window_manager
# self.rotation_axis = context.region_data.view_rotation.axis*Vector((0, 0, -1))
wm.modal_handler_add(self)
self.old_mouse_region_x = event.mouse_region_x
self.base_location = context.active_object.location
return {'RUNNING_MODAL'}
def update(self, context):
if not self.empty:
return
self.empty.location = self.base_location+self.empty_location
self.empty.empty_display_size = self.empty_size
for modifier in self.modifiers:
modifier.count = self.instance_count
def execute(self, context):
active_obj = context.active_object
selected_objs = context.selected_objects
if active_obj is None:
active_obj = selected_objs[0]
if active_obj not in selected_objs:
selected_objs.add(active_obj)
self.base_location = active_obj.location
try:
if self.modifiers:
self.update(context)
return{'FINISHED'}
except ReferenceError:
# continue
pass
# Create an empty at the active object's location
self.empty = bpy.data.objects.new(f"{active_obj.name}-LIN_ARR", None)
self.empty.location = self.base_location+self.empty_location
self.empty.empty_display_size = self.empty_size
context.collection.objects.link(self.empty)
# Parent the empty to the active object
self.empty.parent = active_obj
cursor_location = context.scene.cursor.location
context.scene.cursor.location = active_obj.location
# Loop through selected objects and add array modifier
for obj in selected_objs:
# Add array modifier
array_modifier = obj.modifiers.new(name="Array", type='ARRAY')
array_modifier.count = self.instance_count
array_modifier.use_relative_offset = False
array_modifier.use_object_offset = True
array_modifier.offset_object = self.empty
self.modifiers.append(array_modifier)
# Make selected objects children of the active object
if obj is not active_obj:
obj.parent = active_obj
obj.matrix_parent_inverse = active_obj.matrix_world.inverted()
# Set object origin
bpy.context.view_layer.objects.active = obj
bpy.ops.object.origin_set(type='ORIGIN_CURSOR', center='MEDIAN')
context.scene.cursor.location = cursor_location
return {'FINISHED'}