-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
143 lines (122 loc) · 4.3 KB
/
utils.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
import bpy
import bmesh
import bpy_extras
import gpu
import gpu_extras
import bgl
import blf
import mathutils
# Object
def object_get_active():
obj = bpy.context.active_object
return obj
def object_get_selected():
objs = bpy.context.selected_objects
return objs
# Mesh
def mesh_create_bmesh_object_from_edit_object():
bm = bmesh.from_edit_mesh(bpy.context.edit_object.data)
return bm
def mesh_create_bmesh_objects_from_selected_objects():
objs = bpy.context.selected_objects
selected_objs = [obj for obj in objs if obj.select_get()]
bms = []
for obj in selected_objs:
bm = bmesh.from_edit_mesh(obj.data)
bms.append(bm)
return bms
def mesh_get_active_element():
obj = object_get_active()
if obj == None:
print('no active object')
return None
bm = bmesh.from_edit_mesh(obj.data)
elem = bm.select_history.active
return elem
def mesh_get_selected_vertices_from_selected_objects():
objs = [obj for obj in object_get_selected() if obj.mode == 'EDIT']
all_selected_verts = []
for obj in objs:
bm = bmesh.from_edit_mesh(obj.data)
elems = []
for vert in bm.verts:
if vert.select:
elems.append(vert)
all_selected_verts.extend(elems)
return all_selected_verts
def mesh_get_selected_edges_from_selected_objects(stay_in_edit_mode = False):
mode = bpy.context.object.mode
bpy.ops.object.mode_set(mode='EDIT')
all_selected_edges = []
objs = object_get_selected()
for obj in objs:
bm = bmesh.from_edit_mesh(obj.data)
elems = []
for edge in bm.edges:
if edge.select:
elems.append(edge)
all_selected_edges.extend(elems)
if not stay_in_edit_mode:
bpy.ops.object.mode_set(mode=mode)
return all_selected_edges
def mesh_get_selected_faces_from_selected_objects(stay_in_edit_mode = False):
mode = bpy.context.object.mode
bpy.ops.object.mode_set(mode='EDIT')
all_selected_faces = []
objs = object_get_selected()
for obj in objs:
bm = bmesh.from_edit_mesh(obj.data)
elems = []
for face in bm.faces:
if face.select:
elems.append(face)
all_selected_faces.extend(elems)
if not stay_in_edit_mode:
bpy.ops.object.mode_set(mode=mode)
return all_selected_faces
def mesh_get_normals_of_selected_edges():
normals = []
edges = mesh_create_bmesh_object_from_edit_object().edges
for edge in edges:
edge.normal_update()
normal = (edge.verts[0].normal + edge.verts[1].normal) / 2.0
normals.append(normal)
return normals
def mesh_get_selected_edges_coords():
objs = [obj for obj in object_get_selected() if obj.mode == 'EDIT']
coords = []
for obj in objs:
bm = bmesh.from_edit_mesh(obj.data)
for edge in bm.edges:
if edge.select:
vert1 = mathutils.Vector((edge.verts[0].co[0], edge.verts[0].co[1], edge.verts[0].co[2]))
vert2 = mathutils.Vector((edge.verts[1].co[0], edge.verts[1].co[1], edge.verts[1].co[2]))
vert1 = obj.matrix_world @ vert1
vert2 = obj.matrix_world @ vert2
coords.append((vert1, vert2))
return coords
def mesh_get_selected_verts_coords():
objs = [obj for obj in object_get_selected() if obj.mode == 'EDIT']
coords = []
for obj in objs:
bm = bmesh.from_edit_mesh(obj.data)
for vert in bm.verts:
if vert.select:
coord = mathutils.Vector((vert.co[0], vert.co[1], vert.co[2]))
coord = obj.matrix_world @ coord
coords.append(coord)
return coords
# Draw
def draw_text_3d(text, pos, size, color):
pos_2d = bpy_extras.view3d_utils.location_3d_to_region_2d(bpy.context.region, bpy.context.region_data, pos)
blf.position(0, pos_2d[0], pos_2d[1], 0)
blf.color(0, color[0], color[1], color[2], color[3])
blf.size(0, size, bpy.context.preferences.system.dpi)
blf.draw(0, f'{text}')
def draw_line_3d(start, end, width, color):
shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
bgl.glLineWidth(width)
shader.bind()
shader.uniform_float('color', color)
batch = gpu_extras.batch.batch_for_shader(shader, 'LINES', {"pos": [start, end]})
batch.draw(shader)