-
Notifications
You must be signed in to change notification settings - Fork 5
/
box_creation.py
63 lines (43 loc) · 1.44 KB
/
box_creation.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
import bmesh
import bpy
from bpy_extras.object_utils import object_data_add
tmp_name = 'box_collider'
# vertex indizes defining the faces of the cube
face_order = [
(0, 1, 2, 3),
(4, 7, 6, 5),
(0, 4, 5, 1),
(1, 5, 6, 2),
(2, 6, 7, 3),
(4, 0, 3, 7),
]
def add_box_object(context, vertices):
"""Generate a new object from the given vertices"""
global tmp_name
verts = vertices
edges = []
faces = [[0, 1, 2, 3], [7, 6, 5, 4], [5, 6, 2, 1], [0, 3, 7, 4], [3, 2, 6, 7], [4, 5, 1, 0]]
mesh = bpy.data.meshes.new(name=tmp_name)
mesh.from_pydata(verts, edges, faces)
return object_data_add(context, mesh, operator=None, name=None)
def verts_faces_to_bbox_collider(self, context, verts_loc):
"""Create box collider for selected mesh area in edit mode"""
global tmp_name
# add new mesh
mesh = bpy.data.meshes.new(tmp_name)
bm = bmesh.new()
# create mesh vertices
for v_co in verts_loc:
bm.verts.new(v_co)
# connect vertices to faces
bm.verts.ensure_lookup_table()
for f_idx in face_order:
bm.faces.new([bm.verts[i] for i in f_idx])
# update bmesh to draw properly in viewport
bm.to_mesh(mesh)
mesh.update()
# create new object from mesh and link it to collection
new_collider = bpy.data.objects.new(tmp_name, mesh)
root_collection = context.scene.collection
root_collection.objects.link(new_collider)
return new_collider