-
Notifications
You must be signed in to change notification settings - Fork 0
/
mini_bim.py
339 lines (261 loc) · 9.15 KB
/
mini_bim.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
bl_info = {
"name": "Experimental BIM",
"author": "Luca Strano <[email protected]>",
"version": (1, 0),
"blender": (3, 1, 0),
"category": "",
"location": "",
"description": "",
}
import bpy
import mysql.connector
import bmesh
from collections import Counter
mydb = mysql.connector.connect(
host = "localhost",
user = "root",
)
mydb.autocommit = False
mycursor = mydb.cursor()
def add_mesh(self, context):
items = []
items.append(("All", "All", ""))
for obj in bpy.data.objects:
if obj.type=='MESH':
items.append((obj.name, obj.name, ""))
return items
def add_vertex_group(self, context):
items = []
scene = context.scene
mytool = scene.my_tool
items.append(("All", "All", ""))
for obj in bpy.data.objects:
if mytool.mesh_list == obj.name:
for vgroup in bpy.data.objects[obj.name].vertex_groups:
items.append((vgroup.name, vgroup.name, ""))
break
return items
class MyProperties(bpy.types.PropertyGroup):
mesh_list : bpy.props.EnumProperty(
name = "",
description = "Select a Mesh",
items = add_mesh
)
vertex_group_list : bpy.props.EnumProperty(
name = "",
description = "Select a vertex group",
items = add_vertex_group
)
min_vertex : bpy.props.IntProperty(name = "", min = 0)
max_vertex : bpy.props.IntProperty(name = "", min = 0)
min_face : bpy.props.IntProperty(name = "", min = 0)
max_face : bpy.props.IntProperty(name = "", min = 0)
min_area: bpy.props.FloatProperty(name = "", min = 0.0)
max_area: bpy.props.FloatProperty(name = "", min = 0.0)
check : bpy.props.BoolProperty(name = "", default = False)
def create_database():
database = 'blender'
mycursor.execute("CREATE DATABASE IF NOT EXISTS %s DEFAULT CHARACTER SET 'utf8'" % database)
mydb.database = database
def create_table():
tableExist = False
mycursor.execute("SHOW TABLES")
for x in mycursor:
if "vertex_group" in x:
tableExist = True
break
if tableExist == True:
mycursor.execute("DROP TABLE vertex_group")
mycursor.execute("CREATE TABLE vertex_group(id INT AUTO_INCREMENT PRIMARY KEY, name_vertex_group VARCHAR(255), mesh VARCHAR(255), num_vertex_vg INT, num_face_vg INT, area_vg FLOAT)")
def count_vertex(obj, num_groups):
vg_num_vertices = [0] * num_groups
vertices = obj.data.vertices
for vertex in vertices:
for vgroup in vertex.groups:
vg_num_vertices[vgroup.group] += 1
return vg_num_vertices
def count_face_area(obj, num_groups):
group_faces = [0] * num_groups
area = [0] * num_groups
vertices = obj.data.vertices
polygons = obj.data.polygons
for face in polygons:
size = len(face.vertices)
verts = (vertices[i] for i in face.vertices)
cnt = Counter(g.group for v in verts for g in v.groups)
for i, c, in cnt.items():
if c == size:
group_faces[i]+=1
area[i]+=face.area
return group_faces, area
def insert_database(obj, num_groups, group_vertices, group_faces, area):
for i in range(num_groups):
sql = "INSERT INTO vertex_group (name_vertex_group, mesh, num_vertex_vg, num_face_vg, area_vg) VALUES (%s, %s, %s, %s, %s)"
val = (obj.vertex_groups[i].name, obj.name, group_vertices[i], group_faces[i], area[i])
mycursor.execute(sql, val)
def deselect_vertex():
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.reveal()
bpy.ops.mesh.select_all(action="DESELECT")
def reset_color_vertex(obj):
mesh = obj.data
if obj.type == "MESH":
for vc in mesh.vertex_colors:
mesh.vertex_colors.remove(vc)
mesh.vertex_colors.new()
def restore_scene():
deselect_vertex()
for obj in bpy.data.objects:
reset_color_vertex(obj)
def process_query():
select_mesh = bpy.context.scene.my_tool.mesh_list
select_vertex_group = bpy.context.scene.my_tool.vertex_group_list
min_vertex = bpy.context.scene.my_tool.min_vertex
max_vertex = bpy.context.scene.my_tool.max_vertex
min_face = bpy.context.scene.my_tool.min_face
max_face = bpy.context.scene.my_tool.max_face
min_area = bpy.context.scene.my_tool.min_area
max_area = bpy.context.scene.my_tool.max_area
if select_mesh=="All" and select_vertex_group=="All":
sql = "SELECT * FROM vertex_group WHERE num_vertex_vg>=%s AND num_vertex_vg<=%s AND num_face_vg>=%s AND num_face_vg<=%s AND area_vg>=%s AND area_vg<=%s"
val = (min_vertex, max_vertex, min_face, max_face, min_area, max_area)
if select_mesh!="All" and select_vertex_group == "All":
sql = "SELECT * FROM vertex_group WHERE mesh = %s AND num_vertex_vg>=%s AND num_vertex_vg<=%s AND num_face_vg>=%s AND num_face_vg<=%s AND area_vg>=%s AND area_vg<=%s"
val = (select_mesh, min_vertex, max_vertex, min_face, max_face, min_area, max_area)
if select_mesh!="All" and select_vertex_group!="All":
sql = "SELECT * FROM vertex_group WHERE name_vertex_group = %s AND mesh = %s"
val = (select_vertex_group, select_mesh,)
return sql, val
def query_database():
sql, val = process_query()
mycursor.execute(sql, val)
myresult = mycursor.fetchall()
return myresult
def select_vertex(obj, myObj):
bpy.context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode="OBJECT")
vertices = obj.data.vertices
for vertex in vertices:
for vgroup in vertex.groups:
if vgroup.group in myObj[obj]:
vertices[vertex.index].select = True
def color_vertex(obj, myObj):
bpy.ops.object.mode_set(mode = "EDIT")
bm = bmesh.from_edit_mesh(obj.data)
vgroup_colors = {}
for index_vg in myObj[obj]:
vgroup_colors.update({index_vg: (1, 0, 0, 1)})
target_vertex_groups = vgroup_colors.keys()
vcolor_layers = {}
for index_vg in myObj[obj]:
vcolor_layers.update({index_vg: bm.loops.layers.color['Col']})
dl = bm.verts.layers.deform.verify()
for vertex in bm.verts:
if len(vertex[dl]):
in_vgroups = [vgroup_index for vgroup_index, weight in vertex[dl].items() if (weight)]
in_target_vgroups = set(target_vertex_groups).intersection(in_vgroups)
for face_corner in vertex.link_loops:
for vgroup_idx in in_target_vgroups:
face_corner[vcolor_layers[vgroup_idx]] = vgroup_colors[vgroup_idx]
def hide_unselected_vertices():
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action="SELECT")
bpy.ops.object.mode_set(mode = "EDIT")
if bpy.context.scene.my_tool.check == True:
bpy.ops.mesh.hide(unselected=True)
class export(bpy.types.Operator):
"""Preleva dati dalla scena e caricali in un database"""
bl_idname = "mesh.export"
bl_label = "Export data"
def execute(self, context):
create_database()
create_table()
myObjects = bpy.data.objects
for obj in myObjects:
if obj.type != 'MESH':
continue
num_groups = len(obj.vertex_groups)
group_vertices = count_vertex(obj, num_groups)
group_faces, area = count_face_area(obj, num_groups)
insert_database(obj, num_groups, group_vertices, group_faces, area)
mydb.commit()
return {"FINISHED"}
class query(bpy.types.Operator):
"""Fai una query"""
bl_idname = "mesh.query_database"
bl_label = "Query"
def execute(self, context):
restore_scene()
myresult = query_database()
myObj = {}
for obj in bpy.data.objects:
myObj[obj] = []
for result in myresult:
name_vertex_group = result[1]
name_mesh = result[2]
selected_mesh = bpy.data.objects[name_mesh]
selected_vg = bpy.data.objects[name_mesh].vertex_groups[name_vertex_group].index
myObj[selected_mesh].append(selected_vg)
for obj in myObj:
if(len(myObj[obj])!=0):
select_vertex(obj, myObj)
color_vertex(obj, myObj)
hide_unselected_vertices()
return {"FINISHED"}
class restore(bpy.types.Operator):
"""Ripristina tutta la scena"""
bl_idname = "mesh.restore_data"
bl_label = "Restore"
def execute(self, context):
restore_scene()
return {"FINISHED"}
class VIEW3D_PT_query(bpy.types.Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "Experimental BIM"
bl_label = "Query"
def draw(self, context):
layout = self.layout
scene = context.scene
mytool = scene.my_tool
select_vertex_group = bpy.context.scene.my_tool.vertex_group_list
row = layout.row()
row.operator('mesh.export')
layout.separator()
row = layout.row()
row.label(text="Search: ")
row = layout.row(align=True)
row.label(text = "Mesh")
row.prop(mytool, "mesh_list")
row = layout.row(align=True)
row.label(text = "Vertex Group")
row.prop(mytool, "vertex_group_list")
if select_vertex_group == "All":
row = layout.row(align=True)
row.label(text = "Numero vertici")
row.prop(mytool, "min_vertex", text = "MIN")
row.prop(mytool, "max_vertex", text = "MAX")
row = layout.row(align=True)
row.label(text = "Numero facce")
row.prop(mytool, "min_face", text = "MIN")
row.prop(mytool, "max_face", text = "MAX")
row = layout.row(align=True)
row.label(text = "Area")
row.prop(mytool, "min_area", text = "MIN")
row.prop(mytool, "max_area", text = "MAX")
row = layout.row()
row.prop(mytool, "check", text="Show only selected vertex group")
layout.separator()
row = layout.row()
row.operator('mesh.query_database')
row = layout.row()
row.operator('mesh.restore_data')
classes = [MyProperties, export, query, restore, VIEW3D_PT_query]
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Scene.my_tool = bpy.props.PointerProperty(type=MyProperties)
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)
del bpy.types.Scene.my_tool