-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path__init__.py
More file actions
206 lines (176 loc) · 8.37 KB
/
__init__.py
File metadata and controls
206 lines (176 loc) · 8.37 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
bl_info = {
"name": "Array Splitter",
"description": "Split an objects with array modifiers to individual objects",
"author": "Shahzod Boyxonov",
"version": (1, 0, 1),
"blender": (2, 80, 0),
"location": "CTRL+T > Split Array Modifiers",
"category": "Object"
}
import bpy
def select_obj(obj,context):
bpy.ops.object.select_all(action = 'DESELECT')
obj.select_set(True)
context.view_layer.objects.active = obj
def copy_transform(dst,mat):
dst.location = mat.to_translation()
dst.rotation_euler = mat.to_euler()
dst.scale = mat.to_scale()
def calc_curve_length(curve,context):
select_obj(curve,context)
bpy.ops.object.duplicate(linked = False)
curve = context.object
while len(curve.data.splines)>1:
curve.data.splines.remove(curve.data.splines[1])
bpy.ops.object.transform_apply(scale = True)
bpy.ops.object.convert(target = 'MESH',keep_original = False)
curve_length = 0
for edge in context.object.data.edges:
v1 = context.object.data.vertices[edge.vertices[0]].co
v2 = context.object.data.vertices[edge.vertices[1]].co
curve_length += (v1 - v2).length
active_data = context.object.data
bpy.data.objects.remove(context.object)
bpy.data.meshes.remove(active_data)
return curve_length
def remove_array_mods(obj,mod_show_list):
i = 0
for mod in obj.modifiers:
mod.show_viewport = mod_show_list[i]
i+=1
if mod.type != 'ARRAY': continue
obj.modifiers.remove(mod)
class ArraySplitter(bpy.types.Operator):
"""Split Array Modifier"""
bl_idname = "array_splitter.split"
bl_label = "Split Array Modifiers"
bl_options = {'REGISTER', 'UNDO'}
select : bpy.props.BoolProperty(
name="Select",
default=True)
linked : bpy.props.BoolProperty(
name="Linked",
default=True)
def execute(self, context):
scene = context.scene
vlayer = context.view_layer
active_obj = context.object
objs = [obj for obj in context.selected_objects]
offset_obj = bpy.data.objects.new('Array_Offset_Object',None)
context.collection.objects.link(offset_obj)
all_generated_objs = []
for obj in objs:
generated_objs = []
generated_objs.append(obj)
mod_show_list = []
for mod in obj.modifiers:
mod_show_list.append(mod.show_viewport)
mod.show_viewport = False
vlayer.update()
for mod in obj.modifiers:
if mod.type != 'ARRAY':
mod.show_viewport = True
vlayer.update()
continue
offset_obj.parent = None
offset_obj.location = 0,0,0
offset_obj.rotation_euler = 0,0,0
offset_obj.scale = 1,1,1
if mod.use_object_offset:
if mod.offset_object:
copy_transform(offset_obj, mod.offset_object.matrix_world)
select_obj(obj,context)
offset_obj.select_set(True)
bpy.ops.object.parent_set(keep_transform = True)
offset_obj.location = offset_obj.matrix_local.to_translation()
offset_obj.rotation_euler = offset_obj.matrix_local.to_euler()
offset_obj.scale = offset_obj.matrix_local.to_scale()
offset_obj.parent = None
if mod.use_constant_offset:
offset_obj.location[0] += mod.constant_offset_displace[0]
offset_obj.location[1] += mod.constant_offset_displace[1]
offset_obj.location[2] += mod.constant_offset_displace[2]
if mod.use_relative_offset:
offset_obj.location[0] += mod.relative_offset_displace[0] * (obj.dimensions[0]/obj.scale[0])
offset_obj.location[1] += mod.relative_offset_displace[1] * (obj.dimensions[1]/obj.scale[1])
offset_obj.location[2] += mod.relative_offset_displace[2] * (obj.dimensions[2]/obj.scale[2])
if mod.fit_type == 'FIXED_COUNT':
array_counter = 1
elif mod.fit_type == 'FIT_LENGTH':
length_counter = 0
tar_length = mod.fit_length
elif mod.fit_type == 'FIT_CURVE':
length_counter = 0
if mod.curve:
tar_length = calc_curve_length(mod.curve,context)
else:
tar_length = mod.fit_length
parent_obj = obj
select_obj(obj,context)
for obj2 in generated_objs:
obj2.select_set(True)
while True:
bpy.ops.object.duplicate(linked = self.linked)
copy_transform(context.object, offset_obj.matrix_local)
context.object.parent = parent_obj
parent_obj = context.object
remove_array_mods(parent_obj,mod_show_list)
if mod.fit_type == 'FIXED_COUNT':
array_counter += 1
generated_objs.extend(context.selected_objects)
if array_counter>=mod.count:break
elif mod.fit_type == 'FIT_LENGTH' or mod.fit_type == 'FIT_CURVE':
length_counter += offset_obj.location.length
if length_counter>=tar_length:
context.view_layer.objects.active = parent_obj.parent
bpy.data.objects.remove(parent_obj)
parent_obj = context.object
break
generated_objs.extend(context.selected_objects)
else:
break
if mod.start_cap:
select_obj(mod.start_cap,context)
bpy.ops.object.duplicate(linked = self.linked)
copy_transform(context.object, offset_obj.matrix_local.inverted_safe())
context.object.parent = obj
generated_objs.append(context.object)
if mod.end_cap:
select_obj(mod.end_cap,context)
bpy.ops.object.duplicate(linked = self.linked)
copy_transform(context.object, offset_obj.matrix_local)
context.object.parent = parent_obj
generated_objs.append(context.object)
mod.show_viewport = True
vlayer.update()
remove_array_mods(obj,mod_show_list)
if self.select:
all_generated_objs.extend(generated_objs)
for obj2 in generated_objs:
obj2.select_set(True)
if obj.parent:
obj.parent.select_set(True)
context.view_layer.objects.active = obj.parent
bpy.ops.object.parent_set(keep_transform = True)
else:
bpy.ops.object.parent_clear(type = 'CLEAR_KEEP_TRANSFORM')
bpy.data.objects.remove(offset_obj)
bpy.ops.object.select_all(action = 'DESELECT')
if self.select:
for obj in all_generated_objs:
obj.select_set(True)
else:
for obj in objs:
obj.select_set(True)
context.view_layer.objects.active = active_obj
return {'FINISHED'}
def menu_func(self,context):
self.layout.operator("array_splitter.split")
def register():
bpy.utils.register_class(ArraySplitter)
bpy.types.VIEW3D_MT_object_apply.append(menu_func)
def unregister():
bpy.utils.unregister_class(ArraySplitter)
bpy.types.VIEW3D_MT_object_apply.remove(menu_func)
if __name__ == "__main__":
register()