-
Notifications
You must be signed in to change notification settings - Fork 0
/
interior.py
319 lines (246 loc) · 10.9 KB
/
interior.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
bl_info = {
"name": "ExteriorDeselector",
"blender": (2, 90, 0),
"category": "Mesh",
}
import bpy
import bmesh
from math import *
from bpy.props import *
from bpy.types import Panel, Operator, PropertyGroup
from mathutils import Vector
class RaycasterList(PropertyGroup):
ob: PointerProperty(
name='Current Object',
type=bpy.types.Object)
ob_id: IntProperty(default=0)
used: BoolProperty(default=True)
class TMP_UL_Interior_List(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
row = layout.row(align=True)
row.prop(item.ob, 'name', text='', icon='META_CUBE', emboss=False)
if item.used:
icon = 'CHECKBOX_HLT'
else:
icon = 'CHECKBOX_DEHLT'
row.operator('tmp_seli.raycast_switch', text='', icon=icon).list_id = index
row.operator('tmp_seli.frame_selected', text='', icon="RESTRICT_SELECT_OFF").list_id = index
def invoke(self, context, event):
pass
def registerProps():
bpy.utils.register_class(RaycasterList)
bpy.utils.register_class(TMP_UL_Interior_List)
bpy.types.Scene.tmp_seli_ob_data = CollectionProperty(type=RaycasterList)
bpy.types.Scene.tmp_seli_ob_data_id = IntProperty(default=0)
def unregisterProps():
bpy.utils.unregister_class(RaycasterList)
bpy.utils.unregister_class(TMP_UL_Interior_List)
del bpy.types.Scene.tmp_seli_ob_data
del bpy.types.Scene.tmp_seli_ob_data_id
#scene.objects.active = current_mesh
# Blender's "select interior faces" doesn't actually select interior faces, it
# selects faces that share edges with 2 or more other faces. In my case this
# erroneously selects some faces that are visible but connect to multiple other
# faces. The solution is to select interior faces, then ignore any faces that
# the camera (positioned at 0,0,10) can see, and delete the remaining ones
# See http//blender.stackexchange.com/questions/57540/automated-way-to-make-select-interior-faces-ignore-select-faces-that-are-visib
def removeInteriorFaces():
ops = bpy.ops
scene = bpy.context.scene
mesh = bpy.ops.mesh
viewLayer = bpy.context.view_layer
current_mesh = bpy.context.active_object
mesh_data = current_mesh.data
# First do the built in selection
# mesh.select_interior_faces()
items = scene.tmp_seli_ob_data
cameraOrigins = list(map(lambda x: x.ob.location, items))
# Use the current selection
def localToWorld(vec):
t = vec.to_4d()
t.w = 1
return (current_mesh.matrix_world @ t).to_3d()
# And store all faces inside that selection
indices = []
while len(indices) == 0:
bm = bmesh.from_edit_mesh( mesh_data )
for index, face in enumerate( bm.faces ):
if face.select:
indices.append( ( index, localToWorld(face.calc_center_median_weighted()) ) )
# Deselect everything...
mesh.select_all()
# Get the object depsgraph
deps = bpy.context.evaluated_depsgraph_get()
# Switch to object mode to do scene raycasting (doesn't work in edit mode
# I don't think, got error "has no mesh data to be used for ray casting"
ops.object.mode_set( mode = 'OBJECT' )
outside = []
for index_data in indices:
for cameraOrigin in cameraOrigins:
index = index_data[ 0 ]
center = index_data[ 1 ]
direction = center - cameraOrigin;
direction.normalize()
# Cast a ray from the "camera" position to the face we think is interior
result, location, normal, faceIndex, object, matrix = scene.ray_cast( deps, cameraOrigin, direction )
# If the ray actually hit the face, as in the face index from the
# selection matches the face index from the raycast, then this face
# *is* visible to the camera, so don't remove it!
if faceIndex == index:
outside.append( faceIndex )
break # Since we already know its visible we don't have to keep checking it against the other raycasts
# Build a list of the "true" interior face indices, which is the original
# indices from Blender's built in "select interior faces", but without the
# faces we know the camera can see
invisible_interior_faces = [ data[ 0 ] for data in indices if data[ 0 ] not in outside ]
print( 'Removing ',len( invisible_interior_faces ),'invisible faces' )
# Select the faces (in object mode this is easy, strangely)...
if len( invisible_interior_faces ) > 0:
for index in invisible_interior_faces:
mesh_data.polygons[ index ].select = True
ops.object.mode_set( mode = 'EDIT' )
# Then delete them
if len( invisible_interior_faces ) > 0:
#mesh.delete( type = 'FACE' )
pass
class RaycastSwitch(Operator):
bl_idname = 'tmp_seli.raycast_switch'
bl_label = 'Add Item'
bl_description = 'Selected objects will be used as raycasting origins'
bl_options = {'INTERNAL'}
list_id: IntProperty(default=0)
def execute(self, context):
scn = context.scene
items = scn.tmp_seli_ob_data
if self.list_id < 0:
for item in items:
if self.list_id == -1:
item.used = True
else:
item.used = False
else:
item = items[self.list_id]
if item.used:
item.used = False
else:
item.used = True
return {'FINISHED'}
class FrameSelected(Operator):
bl_idname = 'tmp_seli.frame_selected'
bl_label = 'Frame Selected'
bl_description = 'Go to this object in the viewport'
bl_options = {'INTERNAL'}
list_id: IntProperty(default=0)
def execute(self, context):
scn = context.scene
items = scn.tmp_seli_ob_data
item = items[self.list_id]
bpy.ops.object.select_all(action='DESELECT')
item.ob.select = True
bpy.ops.view3d.view_selected()
return {'FINISHED'}
class SelectInternal(Operator):
"""Deselects external faces via raycasting points""" # Use this as a tooltip for menu items and buttons.
bl_idname = "tmp_seli.deselect_external" # Unique identifier for buttons and menu items to reference.
bl_label = "Deselect External Faces" # Display name in the interface.
bl_options = {'REGISTER', 'UNDO'} # Enable undo for the operator.
def execute(self, context): # execute() is called when running the operator.
bpy.ops.object.mode_set( mode = 'EDIT' )
removeInteriorFaces()
return {'FINISHED'} # Lets Blender know the operator finished successfully.
class SelectInteriorPanel(Panel):
""""""
bl_idname = "VIEW3D_PT_int"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_label = "Interior Faces"
bl_category = "TMPIM"
def draw(self, context):
scn = bpy.context.scene
layout = self.layout
col = layout.column(align=True)
col.label(text="Objects for raycasting origins: ")
col.template_list('TMP_UL_Interior_List', 'raycast_list', scn, 'tmp_seli_ob_data',
scn, 'tmp_seli_ob_data_id', rows=12, type='DEFAULT')
row = col.row(align=True)
row.operator('tmp_seli.raycast_switch', text='Select All').list_id = -1
row.operator('tmp_seli.raycast_switch', text='Deselect All').list_id = -2
col = col.column(align=True)
col.scale_y = 1.2
col.operator("tmp_seli.refresh_ob_data")
col.separator()
col.separator()
colB = col.column(align=True)
colB.scale_y = 2
colB.operator("tmp_seli.deselect_external")
col = col.column()
col.separator()
col.operator("tmp_seli.point_cloud")
def get_obs(obs):
return [ob for ob in obs if ob.type == 'EMPTY']
class RefreshObData(bpy.types.Operator):
bl_idname = 'tmp_seli.refresh_ob_data'
bl_label = 'Update Raycaster List'
bl_description = 'Updates the raycaster list'
def execute(self, context):
scn = context.scene
ob_list = get_obs(scn.objects)
ob_list.sort(key=lambda k: k.name)
scn.tmp_seli_ob_data.clear()
for ob_id, ob in enumerate(ob_list):
item = scn.tmp_seli_ob_data.add()
item.ob = ob
item.ob_id = ob_id
item.used = True
return {'FINISHED'}
class PointCloudOperator(bpy.types.Operator):
bl_idname = "tmp_seli.point_cloud"
bl_label = "Generate Point Cloud"
bl_options = {'REGISTER', 'UNDO'}
count: bpy.props.IntProperty(name="Count", description="Number of points to generate", default=20, min=1)
distance: bpy.props.FloatProperty(name="Distance", description="Distance from the object origin to build the point cloud", default=50, min=0)
def execute(self, context):
# Source object
bpy.ops.object.empty_add(type='PLAIN_AXES', align='WORLD', location=(0,0,0), scale=(1, 1, 1))
bpy.context.object.show_in_front = True
src_ob = bpy.context.object
gr = (sqrt(5) + 1) / 2
ga = (2 - gr) * (2*pi)
rad = self.distance
obs = []
for i in range(self.count):
lat = asin(-1 + 2*i / (self.count + 1))
lon = ga * i
x = rad * cos(lon)*cos(lat)
z = rad * sin(lon)*cos(lat)
y = rad * sin(lat)
copy = src_ob.copy()
copy.location = Vector((x, y, z))
obs.append(copy)
collection = bpy.context.collection.objects
for ob in obs:
collection.link(ob)
# Get rid of the source at the origin
collection.unlink(src_ob)
return {'FINISHED'}
# test call
#bpy.ops.object.modal_operator()
def register():
bpy.utils.register_class(PointCloudOperator)
bpy.utils.register_class(FrameSelected)
bpy.utils.register_class(RaycastSwitch)
bpy.utils.register_class(RefreshObData)
bpy.utils.register_class(SelectInternal)
bpy.utils.register_class(SelectInteriorPanel)
registerProps()
def unregister():
bpy.utils.unregister_class(PointCloudOperator)
bpy.utils.unregister_class(FrameSelected)
bpy.utils.unregister_class(RaycastSwitch)
bpy.utils.unregister_class(RefreshObData)
bpy.utils.unregister_class(SelectInternal)
bpy.utils.unregister_class(SelectInteriorPanel)
unregisterProps()
if __name__ == "__main__":
self = bpy.data.texts["Select Interior"].as_module()
self.register()