-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_attributes_from_blend.py
112 lines (79 loc) · 3.36 KB
/
get_attributes_from_blend.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
import bpy
import bmesh
import os
import json
def get_indices(mode=list(bpy.context.tool_settings.mesh_select_mode).index(True)):
obj = bpy.context.object
if obj.mode == "EDIT":
if mode == 2 or mode == "faces":
indices = [i.index for i in bmesh.from_edit_mesh(obj.data).faces if i.select]
elif mode == 1 or mode == "edges":
indices = [i.index for i in bmesh.from_edit_mesh(obj.data).edges if i.select]
else:
indices = [i.index for i in bmesh.from_edit_mesh(obj.data).verts if i.select]
else:
raise RuntimeError("Edit mode not active")
return indices
def get_height(object_name, dim="z"):
obj = bpy.data.objects[object_name]
if dim == "x" or dim == "X":
return obj.dimensions.x
elif dim == "y" or dim == "Y":
return obj.dimensions.y
else:
return obj.dimensions.z
def get_size(indices):
initial_mode = bpy.context.object.mode
bpy.ops.object.mode_set(mode="EDIT")
obj = bpy.context.object
me = obj.data
bm = bmesh.from_edit_mesh(me)
edges = [e for e in bm.edges]
length = 0
for index in indices:
length += edges[index].calc_length()
bpy.ops.object.mode_set(mode=initial_mode)
return length
def get_volume(object_name):
obj = bpy.data.objects[object_name]
bm = bmesh.new()
bm.from_object(obj, bpy.context.evaluated_depsgraph_get())
volume = bm.calc_volume()
return volume
def get_area(object_name):
obj = bpy.data.objects[object_name]
bm = bmesh.new()
bm.from_object(obj, bpy.context.evaluated_depsgraph_get())
area = sum(f.calc_area() for f in bm.faces)
return area
if __name__ == "__main__":
mode_indices = False
if mode_indices:
if bpy.context.object.mode == "EDIT":
print(get_indices())
quit()
with open(r"Data\indices.json", "r") as json_indices:
indices = json.load(json_indices)
with open(r"Data\project_tmp.json", "r") as json_project:
structure = json.load(json_project)
_, version, _ = bpy.app.version
dae_faces = structure["dae_faces"]
dae_faces_files = [os.path.join(dae_faces, file) for file in os.listdir(dae_faces) if file.endswith(".dae")]
for dae_faces_file in dae_faces_files:
name = "_".join(os.path.splitext(os.path.basename(dae_faces_file))[0].split("_")[:-1])
bpy.ops.wm.read_homefile(use_empty=True)
bpy.ops.wm.collada_import(filepath=dae_faces_file,
import_units=False)
with open(os.path.join(structure["labels"], f"{name}_attributes.json"), "r") as json_attributes:
attributes = json.load(json_attributes)
obj_name = bpy.data.objects[0].name
decimals = 2
bpy.context.view_layer.objects.active = bpy.data.objects[obj_name]
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
for key, value in indices.items():
attributes[key] = f"{get_size(value):.{decimals}f}"
attributes["Height"] = f"{get_height(obj_name):.{decimals}f}"
attributes["Area"] = f"{get_area(obj_name):.{decimals}f}"
attributes["Volume"] = f"{get_volume(obj_name):.{decimals+1}f}"
with open(os.path.join(structure["labels"], f"{name}_attributes.json"), "w") as json_attributes:
json.dump(attributes, json_attributes, indent=4)