-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbulkbool.py
More file actions
286 lines (217 loc) · 8.92 KB
/
bulkbool.py
File metadata and controls
286 lines (217 loc) · 8.92 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
284
285
286
# ##### 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 #####
bl_info = {
"name": "Bulk Bool",
"author": "David B",
"version": (0, 0, 1),
"blender": (2, 80, 0),
"location": "View3D > Sidebar > Edit Tab",
"description": "Bulk Bool allows you to union lots of objects together, even if some are intersecting and others are not",
"doc_url": "https://github.com/AdvancedStyle/blender-bulkbool",
"category": "Object",
}
import bpy
from mathutils import Vector
from mathutils.bvhtree import BVHTree
import bmesh
import time
def BoundingsToMesh( obj, scene ):
name = obj.name + "_bounds"
meshData = bpy.data.meshes.new( name )
meshObj = bpy.data.objects.new( name, meshData )
meshObj.matrix_world = obj.matrix_world.copy()
meshObj.data.from_pydata( *BoundingsGeometry( obj ) )
scene.objects.link( meshObj )
return meshObj
#Create bounding geometry from an object
def BoundingsGeometry( obj ):
verts = [Vector(co) for co in obj.bound_box]
edges = []
faces = [ (0,1,2,3), (4,5,1,0), (7,6,5,4), (3,2,6,7), (6,2,1,5), (7,4,0,3) ]
return verts, edges, faces
#Translates bounding geometry in world coordinates
def BoundingsGeometryInWorld( obj ):
verts, edges, faces = BoundingsGeometry( obj )
return [obj.matrix_world @ v for v in verts], edges, faces
#Get vertices and polygons from an object in world coordinates
def MeshGeometryInWorld( obj ):
return [obj.matrix_world @ v.co for v in obj.data.vertices], [], [p.vertices for p in obj.data.polygons]
#Create a BVH tree from bounding (world co)
def BVHFromBoundings( obj ):
verts, edges, faces = BoundingsGeometryInWorld( obj )
return BVHTree.FromPolygons( verts, faces )
#Create a BVH tree from mesh (world co)
def BVHFromMesh( obj ):
verts, edges, faces = MeshGeometryInWorld( obj )
return BVHTree.FromPolygons( verts, faces )
#Create a BVH tree from bmesh (world co)
def BVHFromBMesh( obj ):
bm = bmesh.new()
bm.from_mesh( obj.data )
bm.transform( obj.matrix_world )
result = BVHTree.FromBMesh( bm )
del bm
return result
#Test if a bvh tree overlap an object
def IntersectBVHObj( bvh, obj, toBvh ):
objBvh = toBvh( obj )
result = bvh.overlap( objBvh )
del objBvh
return result
#Test if two objects overlap
def IntersectObjObj( obj, others, toBvh ):
objBvh = toBvh( obj )
result = [other for other in others if IntersectBVHObj( objBvh, other, toBvh )]
del objBvh
return result
#Test if two objects overlap using boundings method
def IntersectBoundings( obj, others ):
return IntersectObjObj( obj, others, BVHFromBoundings )
#Test if two objects overlap using mesh method
def IntersectMesh( obj, others ):
return IntersectObjObj( obj, others, BVHFromMesh )
#Test if two objects overlap using bmesh method
def IntersectBMesh( obj, others ):
return IntersectObjObj( obj, others, BVHFromBMesh )
#Select objects which overlap another one
def SelectIntersect( obj, scene, others, intersectBounding = False ):
result = IntersectBoundings( obj, others )
if intersectBounding == False:
#startTime = time.time()
#for i in range( 1000 ):
result = IntersectBMesh( obj, result )
#print( "elapsed", time.time() - startTime )
return result
class BULKBOOL_PT_panel(bpy.types.Panel):
bl_label = "Bulk Union"
bl_idname = "BULKBOOL_PT_panel"
bl_space_type = "VIEW_3D"
bl_region_type = 'UI'
bl_category = 'Edit'
def draw(self, context):
layout = self.layout
row = layout.row()
row.operator('object.bulkbool_auto_union')
class Bulk_Auto_Boolean:
def objects_prepare(self):
objects = bpy.context.selected_objects
print('Preparing Objects: '+str(len(objects)))
for ob in bpy.context.selected_objects:
#print(ob.type)
if ob.type != "MESH":
ob.select_set(False)
else:
ob.data = ob.data.copy()
def mesh_selection(self, ob, select_action):
obj = bpy.context.active_object
bpy.context.view_layer.objects.active = ob
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.reveal()
bpy.ops.mesh.select_all(action=select_action)
bpy.ops.object.mode_set(mode="OBJECT")
bpy.context.view_layer.objects.active = obj
def boolean_operation(self, obs):
joins_tracker = dict()
for ob in obs:
joins_tracker[ob.name] = len(ob.data.vertices)
while len(joins_tracker) > 1:
# Get our objects with
list = iter(sorted(joins_tracker.items(), key=lambda x: x[1]))
obj1_name = next(list)[0]
obj2_name = next(list)[0]
obj1 = bpy.context.scene.objects[obj1_name]
obj2 = bpy.context.scene.objects[obj2_name]
print(obj1_name)
print(obj2_name)
self.boolean_mod(obj1, obj2, self.mode)
del joins_tracker[obj2_name]
joins_tracker[obj1_name] = len(obj1.data.vertices)
obj1.select_set(True)
def boolean_mod(self, obj, ob, mode, ob_delete=True):
object_materials = []
for material in obj.data.materials:
object_materials.append(material.name)
# Handle Materials
for material in ob.data.materials:
if material.name not in object_materials:
# We need to add this material to the main object
obj.data.materials.append(material)
md = obj.modifiers.new("Auto Boolean", "BOOLEAN")
md.show_viewport = False
md.operation = mode
md.object = ob
override = {"object": obj}
bpy.ops.object.modifier_apply(override, modifier=md.name)
if ob_delete:
bpy.data.objects.remove(ob)
def get_touching_group(self):
all_objects = bpy.context.selected_objects.copy()
# Remove the untouching items
try_objects = []
for obj in all_objects:
if obj.name not in self.untouching_list:
try_objects.append(obj)
n=0
for obj in try_objects:
list = try_objects.copy()
# Remove itself from the list
del list[n]
result = SelectIntersect( obj, self.context.scene, list)
n += 1
if len(result) == 0:
# put this object in the no touching list
self.untouching_list.append(obj.name)
else:
store = [obj]
for r in result:
store.append(r)
return store
return False
def execute(self, context):
self.untouching_list = []
self.context = context
self.objects_prepare()
touching = self.get_touching_group()
while touching != False:
print('Found touching objects')
self.boolean_operation(touching)
touching = self.get_touching_group()
print('Done with touching do join');
#All touching stuff has been unioned, so now just join the rest instead of Union
# Make sure we have one of the objects set as active
bpy.context.view_layer.objects.active = bpy.context.selected_objects[0]
bpy.ops.object.join()
return {"FINISHED"}
def invoke(self, context, event):
#if len(context.selected_objects) < 2:
# self.report({"ERROR"}, "At least two objects must be selected")
# return {"CANCELLED"}
return self.execute(context)
class OBJECT_OT_bulkbool_Auto_Union(bpy.types.Operator, Bulk_Auto_Boolean):
bl_idname = "object.bulkbool_auto_union"
bl_label = "Union Selected Objects"
bl_description = "Combine selected objects"
mode = "UNION"
def register():
bpy.utils.register_class(BULKBOOL_PT_panel)
bpy.utils.register_class(OBJECT_OT_bulkbool_Auto_Union)
def unregister():
bpy.utils.unregister_class(BULKBOOL_PT_panel)
bpy.utils.unregister_class(OBJECT_OT_bulkbool_Auto_Union)
if __name__ == "__main__":
register()