Skip to content

Commit ecbd94b

Browse files
author
nkx
committed
update obbox export
1 parent 04fef68 commit ecbd94b

7 files changed

Lines changed: 37 additions & 16 deletions

File tree

io_mesh_w3d/bone_volume_export.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def export_bone_volume_data(context, filepath):
4141

4242
location, rotation, scale = mesh.matrix_world.decompose()
4343
extend = get_aa_box(mesh.data.vertices)
44+
center = get_aa_center(mesh.data.vertices)
4445
halfX = extend.x * scale.x * 0.5
4546
halfY = extend.y * scale.y * 0.5
4647
halfZ = extend.z * scale.z * 0.5
@@ -50,7 +51,7 @@ def export_bone_volume_data(context, filepath):
5051
box.set('HalfSizeY', format_str(halfY))
5152
box.set('HalfSizeZ', format_str(halfZ))
5253

53-
create_vector(location, box, 'Translation')
54+
create_vector(location + center, box, 'Translation')
5455
create_quaternion(rotation, box, 'Rotation')
5556

5657
write(root, filepath)

io_mesh_w3d/common/utils/box_export.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,25 @@
55
from io_mesh_w3d.common.structs.collision_box import *
66

77

8-
def retrieve_boxes(container_name):
8+
def retrieve_boxes(context, container_name):
99
boxes = []
1010

1111
for mesh_object in get_objects('MESH'):
1212
if mesh_object.data.object_type != 'BOX':
1313
continue
1414
name = container_name + '.' + mesh_object.name
15+
16+
if Vector(mesh_object.rotation_euler) != Vector((0,0,0)):
17+
context.warning(f'Rotation on the collision box is not supported. Resetting to 0!')
18+
mesh_object.rotation_euler = (0,0,0)
19+
20+
center = get_aa_center(mesh_object.data.vertices, mesh_object.matrix_local)
21+
extend = get_aa_box(mesh_object.data.vertices, mesh_object.matrix_local)
22+
1523
box = CollisionBox(
1624
name_=name,
17-
center=mesh_object.location)
18-
19-
box.extend = get_aa_box(mesh_object.data.vertices)
25+
center=center,
26+
extend=extend)
2027

2128
box.box_type = int(mesh_object.data.box_type)
2229

io_mesh_w3d/common/utils/helpers.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def create_node_no_repeative(nodes, type, name):
201201
return new_node
202202

203203

204-
def get_aa_box(vertices):
204+
def get_aa_box(vertices, matrix_local = Matrix.Identity(4)):
205205
minX = sys.float_info.max
206206
maxX = sys.float_info.min
207207

@@ -212,13 +212,23 @@ def get_aa_box(vertices):
212212
maxZ = sys.float_info.min
213213

214214
for vertex in vertices:
215-
minX = min(vertex.co.x, minX)
216-
maxX = max(vertex.co.x, maxX)
217215

218-
minY = min(vertex.co.y, minY)
219-
maxY = max(vertex.co.y, maxY)
216+
coord = Vector(vertex.co)
220217

221-
minZ = min(vertex.co.z, minZ)
222-
maxZ = max(vertex.co.z, maxZ)
218+
minX = min((matrix_local @ coord).x, minX)
219+
maxX = max((matrix_local @ coord).x, maxX)
220+
minY = min((matrix_local @ coord).y, minY)
221+
maxY = max((matrix_local @ coord).y, maxY)
222+
minZ = min((matrix_local @ coord).z, minZ)
223+
maxZ = max((matrix_local @ coord).z, maxZ)
223224

224225
return Vector((maxX - minX, maxY - minY, maxZ - minZ))
226+
227+
def get_aa_center(vertices, matrix_local = Matrix.Identity(4)):
228+
vertex_sum = Vector((0, 0, 0))
229+
vertex_count = len(vertices)
230+
for vertex in vertices:
231+
vertex_sum += matrix_local @ vertex.co
232+
233+
centroid_local = vertex_sum / vertex_count if vertex_count > 0 else Vector((0,0,0))
234+
return centroid_local

io_mesh_w3d/common/utils/hierarchy_export.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def retrieve_hierarchy(context, container_name):
3737
hierarchy.header.name = rig.data.name
3838
hierarchy.header.center_pos = rig.location
3939

40-
if rig.location != (0, 0, 0) or rig.scale != (1,1,1) or rig.rotation_euler != (0,0,0):
40+
if rig.location != Vector((0, 0, 0)) or rig.scale != Vector((1,1,1)) or Vector(rig.rotation_euler) != Vector((0,0,0)):
4141
context.warning(f'Reset translation on the armature. To move/zoom/rotate the armature, do it in edit mode!')
4242
rig.location = (0, 0, 0)
4343
rig.scale = (1,1,1)

io_mesh_w3d/common/utils/material_export.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from io_mesh_w3d.w3d.structs.mesh_structs.vertex_material import *
88
from io_mesh_w3d.common.structs.mesh_structs.shader_material import *
99
from io_mesh_w3d.custom_properties import *
10+
import os
1011

1112
DEFAULT_W3D = 'DefaultW3D.fx'
1213

@@ -49,7 +50,9 @@ def get_used_textures_global_tree():
4950
for node in material.node_tree.nodes:
5051
if node.type == 'TEX_IMAGE':
5152
if node.image and node.image.name != "IMG_NOT_FOUND":
52-
used_images.append((node.image.name, node.image.filepath))
53+
abs_path = bpy.path.abspath(node.image.filepath)
54+
if os.path.exists(abs_path):
55+
used_images.append((node.image.name, abs_path))
5356
return used_images
5457

5558
def retrieve_vertex_material(material, principled):

io_mesh_w3d/export_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def retrieve_data(context, export_settings):
5151
rig=rig,
5252
meshes=[],
5353
textures=[],
54-
collision_boxes=retrieve_boxes(container_name),
54+
collision_boxes=retrieve_boxes(context, container_name),
5555
dazzles=retrieve_dazzles(container_name),
5656
hierarchy=hierarchy,
5757
hlod=hlod)

io_mesh_w3d/geometry_export.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def export_geometry_data(context, filepath):
4242

4343
type = str(mesh.data.geometry_type).upper()
4444
location, _, scale = mesh.matrix_world.decompose()
45-
extend = get_aa_box(mesh.data.vertices)
45+
extend = get_aa_box(mesh.data.vertices, mesh.matrix_local)
4646
majorRadius = extend.x * scale.x * 0.5
4747
minorRadius = extend.y * scale.y * 0.5
4848
height = extend.z * scale.z

0 commit comments

Comments
 (0)