-
Notifications
You must be signed in to change notification settings - Fork 22
/
op_object.py
119 lines (94 loc) · 3.83 KB
/
op_object.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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8-80 compliant>
# ----------------------------------------------------------------------------
# Object related functionality
from .op_gen import *
# ----------------------------------------------------------------------------
def get_object_bbox_coords(obj):
"""Returns two vectors which contain the minimum and maximum coordinates
for bounding box for object obj
"""
# Acknowledgement: Here is nice explanation of obj.bound_box and example
# for using Blender's bounding box data in python by zeffii:
# https://blender.stackexchange.com/questions/32283/what-are-all-values-in-bound-box
worldify = lambda p: obj.matrix_world @ mathutils.Vector(p[:])
coords = [worldify(p).to_tuple() for p in obj.bound_box[:]]
rotated = list(zip(*coords[::]))
min_bbox = [min(c) for c in rotated]
max_bbox = [max(c) for c in rotated]
return min_bbox, max_bbox
def get_global_bbox_coords():
"""Returns two vectors which contain the minimum and maximum coordinates
for bounding box for all mesh objects included in export
"""
all_mins=[];
all_maxs=[];
for obj in bpy.data.objects:
if obj.type != 'MESH':
continue
if not obj.shmg_include_in_export:
continue
obj_min_bbox, obj_max_bbox = get_object_bbox_coords(obj)
all_mins.append(obj_min_bbox)
all_maxs.append(obj_max_bbox)
rot_min = zip(*all_mins[::])
global_min_bbox = [min(c) for c in rot_min]
rot_max = zip(*all_maxs[::])
global_max_bbox = [max(c) for c in rot_max]
return global_min_bbox, global_max_bbox
def block_mesh_cell_count(bb_min, bb_max, gui):
"""Returns number of cells in Block Mesh and updates
block_mesh_* data in gui. bb_min and bb_max are
minimum and maximum bounding box coordinates.
"""
sl = gui.cell_side_length
for i in range(0, 3):
bb_min_with_margin = bb_min[i] - sl/2.0
bb_max_with_margin = bb_max[i] + sl/2.0
bm_min = math.floor(bb_min_with_margin / sl)
bm_max = math.ceil(bb_max_with_margin / sl)
gui.block_mesh_delta[i] = bm_max - bm_min
gui.block_mesh_min[i] = bm_min * sl
gui.block_mesh_max[i] = bm_max * sl
bm_count = gui.block_mesh_delta[0] * \
gui.block_mesh_delta[1] * \
gui.block_mesh_delta[2]
return bm_count
def get_surface_area(obj):
"""Returns surface area of mesh object obj"""
if obj.type != 'MESH':
return 0.0
bm = bmesh.new()
bm.from_mesh(obj.data)
bm.transform(obj.matrix_world) # Apply transformation
bmesh.ops.triangulate(bm, faces=bm.faces) # Triangulate
area = sum(f.calc_area() for f in bm.faces)
del bm
return area
def get_scaled_object_names():
"""Returns string of object names which apply object scaling"""
ob_names = ""
for ob in bpy.data.objects:
if ob.type != 'MESH':
continue
if not ob.shmg_include_in_export:
continue
if ob.scale != mathutils.Vector((1.0, 1.0, 1.0)):
ob_names += ob.name + " "
return ob_names