-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.py
242 lines (201 loc) · 6.23 KB
/
util.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
import bpy
from mathutils import Vector
def get_color_from_obj(obj, idx):
"""
This function exists because we cannot trust blender to have the vertex colors
to be aligned with the vertex loops. Possibly this happens when you import a
wrong model.
"""
if len(obj.data.vertex_colors[0].data) <= idx:
return (0, 0, 0)
else:
return obj.data.vertex_colors[0].data[idx].color
def is_pos_s(vecfx32):
return (
(vecfx32.x & 0x3F) == 0 and
(vecfx32.y & 0x3F) == 0 and
(vecfx32.z & 0x3F) == 0
)
def is_pos_diff(diff):
# 512 is 0.125 in FX32
return (
abs(diff.x) < 512 and
abs(diff.y) < 512 and
abs(diff.z) < 512
)
def calculate_pos_scale(max_coord):
m = float_to_fx32(max_coord)
pos_scale = 0
while m >= 0x8000:
pos_scale += 1
m >>= 1
return pos_scale
def get_object_max_min(obj):
matrix = obj.matrix_world
bounds = [matrix @ Vector(v) for v in obj.bound_box]
return {
'min': bounds[0],
'max': bounds[6]
}
def get_all_max_min():
min_p = Vector([float('inf'), float('inf'), float('inf')])
max_p = Vector([-float('inf'), -float('inf'), -float('inf')])
for obj in bpy.context.view_layer.objects:
if obj.type != 'MESH':
continue
max_min = get_object_max_min(obj)
# Max
max_p.x = max(max_p.x, max_min['max'].x)
max_p.x = max(max_p.x, max_min['min'].x)
max_p.y = max(max_p.y, max_min['max'].y)
max_p.y = max(max_p.y, max_min['min'].y)
max_p.z = max(max_p.z, max_min['max'].z)
max_p.z = max(max_p.z, max_min['min'].z)
# Min
min_p.x = min(min_p.x, max_min['min'].x)
min_p.x = min(min_p.x, max_min['max'].x)
min_p.y = min(min_p.y, max_min['min'].y)
min_p.y = min(min_p.y, max_min['max'].y)
min_p.z = min(min_p.z, max_min['min'].z)
min_p.z = min(min_p.z, max_min['max'].z)
return {
'min': min_p,
'max': max_p
}
def get_global_mat_index(obj, index):
if len(obj.material_slots) <= index:
# If an object doesn't have (enough) material slots, the polygon
# with the requested index shouldn't be converted.
return -1
if obj.material_slots[index].material is None:
# Material doesn't have any material in the slot.
return -1
name = obj.material_slots[index].material.name
return bpy.data.materials.find(name)
def lin2s(x):
"""
Le color correction function. From some guy on blender stackexchange.
http://entropymine.com/imageworsener/srgbformula/
"""
if x <= 0.0031308:
y = x * 12.92
elif 0.0031308 < x <= 1:
y = 1.055 * x ** (1 / 2.4) - 0.055
return y
def float_to_fx32(value):
return int(round(value * 4096))
def fx32_to_float(value):
return float(value) / 4096
def float_to_fx10(value):
return max(min(int(round(value * 512)), 511), -512)
def fx10_to_float(value):
return float(value) / 512
def vector_to_vecfx10(vector):
return Vecfx10([
float_to_fx10(vector.x),
float_to_fx10(vector.y),
float_to_fx10(vector.z),
])
class Vecfx10():
def __init__(self, vector=[0, 0, 0]):
self.x = vector[0]
self.y = vector[1]
self.z = vector[2]
def to_vector(self):
return Vector([
fx10_to_float(self.x),
fx10_to_float(self.y),
fx10_to_float(self.z),
])
def __eq__(self, other):
if other is None:
return False
return (
self.x == other.x
and self.y == other.y
and self.z == other.z
)
class VecFx32(object):
def __init__(self, vector=[0, 0, 0]):
self.x = vector[0]
self.y = vector[1]
self.z = vector[2]
def from_floats(self, floats):
return VecFx32([
float_to_fx32(floats[0]),
float_to_fx32(floats[1]),
float_to_fx32(floats[2])
])
def from_vector(self, vector):
return VecFx32([
float_to_fx32(vector.x),
float_to_fx32(vector.y),
float_to_fx32(vector.z)
])
def to_vector(self):
return Vector([
fx32_to_float(self.x),
fx32_to_float(self.y),
fx32_to_float(self.z),
])
def __str__(self):
return str(self.x), str(self.y), str(self.z)
def __sub__(self, other):
if isinstance(other, self.__class__):
return VecFx32([
self.x - other.x,
self.y - other.y,
self.z - other.z
])
elif isinstance(other, int):
return VecFx32([
self.x - other,
self.y - other,
self.z - other
])
else:
raise TypeError(
"unsupported operand type(s) for -: '{}' and '{}'"
).format(self.__class__, type(other))
def __rshift__(self, other):
if isinstance(other, self.__class__):
return VecFx32([
self.x >> other.x,
self.y >> other.y,
self.z >> other.z
])
elif isinstance(other, int):
return VecFx32([
self.x >> other,
self.y >> other,
self.z >> other
])
else:
raise TypeError(
"unsupported operand type(s) for >>: '{}' and '{}'"
).format(self.__class__, type(other))
def __lt__(self, other):
if isinstance(other, self.__class__):
return (
self.x < other.x and
self.y < other.y and
self.z < other.z
)
elif isinstance(other, int):
return (
self.x < other and
self.y < other and
self.z < other
)
else:
raise TypeError(
"unsupported operand type(s) for <: '{}' and '{}'"
).format(self.__class__, type(other))
def __eq__(self, other):
if other is None:
return False
return (
self.x == other.x
and self.y == other.y
and self.z == other.z
)