2019-11-11 10:03:52 +00:00
|
|
|
""" Gathers the Blender objects from the current scene and returns them as a list of
|
|
|
|
Model objects. """
|
|
|
|
|
|
|
|
import bpy
|
2019-11-15 09:40:47 +00:00
|
|
|
import math
|
2020-01-06 04:08:36 +00:00
|
|
|
from enum import Enum
|
2020-10-26 14:20:33 +00:00
|
|
|
from typing import List, Set, Dict, Tuple
|
2019-11-11 10:03:52 +00:00
|
|
|
from itertools import zip_longest
|
|
|
|
from .msh_model import *
|
|
|
|
from .msh_model_utilities import *
|
|
|
|
from .msh_utilities import *
|
2022-01-18 20:16:49 +00:00
|
|
|
from .msh_skeleton_utilities import *
|
2019-11-11 10:03:52 +00:00
|
|
|
|
|
|
|
SKIPPED_OBJECT_TYPES = {"LATTICE", "CAMERA", "LIGHT", "SPEAKER", "LIGHT_PROBE"}
|
|
|
|
MESH_OBJECT_TYPES = {"MESH", "CURVE", "SURFACE", "META", "FONT", "GPENCIL"}
|
2019-11-19 14:33:01 +00:00
|
|
|
MAX_MSH_VERTEX_COUNT = 32767
|
2019-11-11 10:03:52 +00:00
|
|
|
|
2020-11-30 01:10:16 +00:00
|
|
|
def gather_models(apply_modifiers: bool, export_target: str, skeleton_only: bool) -> Tuple[List[Model], bpy.types.Object]:
|
2019-11-11 10:03:52 +00:00
|
|
|
""" Gathers the Blender objects from the current scene and returns them as a list of
|
|
|
|
Model objects. """
|
|
|
|
|
2019-11-13 03:38:55 +00:00
|
|
|
depsgraph = bpy.context.evaluated_depsgraph_get()
|
2019-11-11 10:03:52 +00:00
|
|
|
parents = create_parents_set()
|
|
|
|
|
|
|
|
models_list: List[Model] = []
|
|
|
|
|
2022-09-29 19:18:58 +00:00
|
|
|
# Composite bones are bones which have geometry.
|
|
|
|
# If a child object has the same name, it will take said child's geometry.
|
2020-11-30 01:10:16 +00:00
|
|
|
|
2022-09-29 19:18:58 +00:00
|
|
|
# Pure bones are just bones and after all objects are explored the only
|
|
|
|
# entries remaining in this dict will be bones without geometry.
|
|
|
|
pure_bones_from_armature = {}
|
2019-11-11 10:03:52 +00:00
|
|
|
|
2022-09-29 19:18:58 +00:00
|
|
|
objects_to_export = select_objects(export_target)
|
2020-10-15 19:44:17 +00:00
|
|
|
|
2022-09-29 19:18:58 +00:00
|
|
|
for uneval_obj in objects_to_export:
|
|
|
|
if uneval_obj.type == "ARMATURE":
|
|
|
|
armature_found = uneval_obj.evaluated_get(depsgraph) if apply_modifiers else uneval_obj
|
|
|
|
pure_bones_from_armature = expand_armature(armature_found)
|
|
|
|
break
|
2019-11-21 10:23:52 +00:00
|
|
|
|
2022-09-29 19:18:58 +00:00
|
|
|
for uneval_obj in objects_to_export:
|
|
|
|
if uneval_obj.type == "ARMATURE" or (uneval_obj.type in SKIPPED_OBJECT_TYPES and uneval_obj.name not in parents):
|
2020-11-23 15:15:22 +00:00
|
|
|
continue
|
2020-10-16 00:40:27 +00:00
|
|
|
|
2022-09-29 19:18:58 +00:00
|
|
|
obj = uneval_obj.evaluated_get(depsgraph) if apply_modifiers else uneval_obj
|
2020-10-20 03:18:08 +00:00
|
|
|
|
2022-09-29 19:18:58 +00:00
|
|
|
check_for_bad_lod_suffix(obj)
|
2020-10-20 03:18:08 +00:00
|
|
|
|
2022-09-29 19:18:58 +00:00
|
|
|
# Test for a mesh object that is actually a BONE (shares name with bone_parent)
|
|
|
|
# If so, we inject geometry into the BONE while not modifying it's transform/name
|
|
|
|
if obj.parent_bone and obj.parent_bone in pure_bones_from_armature:
|
|
|
|
model = pure_bones_from_armature[obj.parent_bone]
|
|
|
|
# Since we found a composite bone, removed it from the dict of pure bones
|
|
|
|
pure_bones_from_armature.pop(obj.parent_bone)
|
|
|
|
else:
|
|
|
|
model = Model()
|
|
|
|
model.name = obj.name
|
|
|
|
model.model_type = get_model_type(obj, skeleton_only)
|
|
|
|
model.hidden = get_is_model_hidden(obj)
|
2020-11-29 23:32:17 +00:00
|
|
|
|
2022-09-29 19:18:58 +00:00
|
|
|
transform = obj.matrix_local
|
2020-11-30 01:10:16 +00:00
|
|
|
|
2022-09-29 19:18:58 +00:00
|
|
|
if obj.parent_bone:
|
|
|
|
model.parent = obj.parent_bone
|
2020-10-20 03:18:08 +00:00
|
|
|
|
2022-09-29 19:18:58 +00:00
|
|
|
# matrix_local, when called on an armature child also parented to a bone, appears to be broken.
|
|
|
|
# At the very least, the results contradict the docs...
|
|
|
|
armature_relative_transform = obj.parent.matrix_world.inverted() @ obj.matrix_world
|
|
|
|
transform = obj.parent.data.bones[obj.parent_bone].matrix_local.inverted() @ armature_relative_transform
|
|
|
|
|
|
|
|
else:
|
|
|
|
if obj.parent is not None:
|
|
|
|
if obj.parent.type == "ARMATURE":
|
|
|
|
model.parent = obj.parent.parent.name if obj.parent.parent else ""
|
|
|
|
transform = obj.parent.matrix_local @ transform
|
|
|
|
else:
|
|
|
|
model.parent = obj.parent.name
|
|
|
|
|
|
|
|
local_translation, local_rotation, _ = transform.decompose()
|
|
|
|
model.transform.rotation = convert_rotation_space(local_rotation)
|
|
|
|
model.transform.translation = convert_vector_space(local_translation)
|
2019-11-11 10:03:52 +00:00
|
|
|
|
2020-10-14 20:06:04 +00:00
|
|
|
if obj.type in MESH_OBJECT_TYPES:
|
2020-11-29 23:32:17 +00:00
|
|
|
|
2020-10-16 18:56:03 +00:00
|
|
|
mesh = obj.to_mesh()
|
2020-10-16 00:40:27 +00:00
|
|
|
model.geometry = create_mesh_geometry(mesh, obj.vertex_groups)
|
2020-11-30 01:27:46 +00:00
|
|
|
|
2019-11-11 10:03:52 +00:00
|
|
|
obj.to_mesh_clear()
|
|
|
|
|
2019-11-18 12:33:55 +00:00
|
|
|
_, _, world_scale = obj.matrix_world.decompose()
|
|
|
|
world_scale = convert_scale_space(world_scale)
|
|
|
|
scale_segments(world_scale, model.geometry)
|
2020-11-29 23:32:17 +00:00
|
|
|
|
2019-11-19 14:33:01 +00:00
|
|
|
for segment in model.geometry:
|
|
|
|
if len(segment.positions) > MAX_MSH_VERTEX_COUNT:
|
|
|
|
raise RuntimeError(f"Object '{obj.name}' has resulted in a .msh geometry segment that has "
|
|
|
|
f"more than {MAX_MSH_VERTEX_COUNT} vertices! Split the object's mesh up "
|
|
|
|
f"and try again!")
|
2020-11-29 23:32:17 +00:00
|
|
|
if obj.vertex_groups:
|
|
|
|
model.bone_map = [group.name for group in obj.vertex_groups]
|
|
|
|
|
2019-11-11 10:03:52 +00:00
|
|
|
|
2019-11-15 09:40:47 +00:00
|
|
|
if get_is_collision_primitive(obj):
|
|
|
|
model.collisionprimitive = get_collision_primitive(obj)
|
|
|
|
|
2020-10-14 20:06:04 +00:00
|
|
|
models_list.append(model)
|
|
|
|
|
2022-09-29 19:18:58 +00:00
|
|
|
# We removed all composite bones after looking through the objects,
|
|
|
|
# so the bones left are all pure and we add them all here.
|
|
|
|
models_list += pure_bones_from_armature.values()
|
2020-11-29 23:32:17 +00:00
|
|
|
|
2020-11-30 01:10:16 +00:00
|
|
|
return (models_list, armature_found)
|
2019-11-11 10:03:52 +00:00
|
|
|
|
|
|
|
|
2020-10-16 15:34:39 +00:00
|
|
|
|
2019-11-11 10:03:52 +00:00
|
|
|
def create_parents_set() -> Set[str]:
|
|
|
|
""" Creates a set with the names of the Blender objects from the current scene
|
|
|
|
that have at least one child. """
|
|
|
|
|
|
|
|
parents = set()
|
|
|
|
|
|
|
|
for obj in bpy.context.scene.objects:
|
|
|
|
if obj.parent is not None:
|
|
|
|
parents.add(obj.parent.name)
|
|
|
|
|
|
|
|
return parents
|
|
|
|
|
2020-10-16 00:40:27 +00:00
|
|
|
def create_mesh_geometry(mesh: bpy.types.Mesh, has_weights: bool) -> List[GeometrySegment]:
|
2019-11-11 10:03:52 +00:00
|
|
|
""" Creates a list of GeometrySegment objects from a Blender mesh.
|
|
|
|
Does NOT create triangle strips in the GeometrySegment however. """
|
|
|
|
|
2022-09-23 15:50:29 +00:00
|
|
|
# We have to do this for all meshes to account for sharp edges
|
|
|
|
mesh.calc_normals_split()
|
2019-11-15 07:02:04 +00:00
|
|
|
|
2019-11-11 10:03:52 +00:00
|
|
|
mesh.validate_material_indices()
|
|
|
|
mesh.calc_loop_triangles()
|
|
|
|
|
2019-11-15 07:02:04 +00:00
|
|
|
material_count = max(len(mesh.materials), 1)
|
2019-11-11 10:03:52 +00:00
|
|
|
|
2019-11-15 07:02:04 +00:00
|
|
|
segments: List[GeometrySegment] = [GeometrySegment() for i in range(material_count)]
|
2020-10-16 00:40:27 +00:00
|
|
|
vertex_cache = [dict() for i in range(material_count)]
|
2019-11-20 02:10:44 +00:00
|
|
|
vertex_remap: List[Dict[Tuple[int, int], int]] = [dict() for i in range(material_count)]
|
2019-11-15 07:02:04 +00:00
|
|
|
polygons: List[Set[int]] = [set() for i in range(material_count)]
|
2019-11-11 10:03:52 +00:00
|
|
|
|
|
|
|
if mesh.vertex_colors.active is not None:
|
|
|
|
for segment in segments:
|
|
|
|
segment.colors = []
|
|
|
|
|
2020-10-16 00:40:27 +00:00
|
|
|
if has_weights:
|
|
|
|
for segment in segments:
|
|
|
|
segment.weights = []
|
|
|
|
|
2019-11-11 10:03:52 +00:00
|
|
|
for segment, material in zip(segments, mesh.materials):
|
|
|
|
segment.material_name = material.name
|
|
|
|
|
2022-09-23 15:50:29 +00:00
|
|
|
def add_vertex(material_index: int, vertex_index: int, loop_index: int) -> int:
|
2019-11-11 10:03:52 +00:00
|
|
|
nonlocal segments, vertex_remap
|
|
|
|
|
2019-11-20 02:10:44 +00:00
|
|
|
vertex_cache_miss_index = -1
|
2019-11-11 10:03:52 +00:00
|
|
|
segment = segments[material_index]
|
2019-11-20 02:10:44 +00:00
|
|
|
cache = vertex_cache[material_index]
|
2019-11-11 10:03:52 +00:00
|
|
|
remap = vertex_remap[material_index]
|
|
|
|
|
2022-09-23 15:50:29 +00:00
|
|
|
# always use loop normals since we always calculate a custom split set
|
|
|
|
vertex_normal = Vector( mesh.loops[loop_index].normal )
|
2019-11-24 03:03:13 +00:00
|
|
|
|
|
|
|
def get_cache_vertex():
|
2019-11-20 02:10:44 +00:00
|
|
|
yield mesh.vertices[vertex_index].co.x
|
|
|
|
yield mesh.vertices[vertex_index].co.y
|
|
|
|
yield mesh.vertices[vertex_index].co.z
|
|
|
|
|
2019-11-24 03:03:13 +00:00
|
|
|
yield vertex_normal.x
|
|
|
|
yield vertex_normal.y
|
|
|
|
yield vertex_normal.z
|
2019-11-20 02:10:44 +00:00
|
|
|
|
|
|
|
if mesh.uv_layers.active is not None:
|
|
|
|
yield mesh.uv_layers.active.data[loop_index].uv.x
|
|
|
|
yield mesh.uv_layers.active.data[loop_index].uv.y
|
|
|
|
|
|
|
|
if segment.colors is not None:
|
2019-11-21 10:48:42 +00:00
|
|
|
for v in mesh.vertex_colors.active.data[loop_index].color:
|
2019-11-20 02:10:44 +00:00
|
|
|
yield v
|
|
|
|
|
2020-10-16 00:40:27 +00:00
|
|
|
if segment.weights is not None:
|
|
|
|
for v in mesh.vertices[vertex_index].groups:
|
|
|
|
yield v.group
|
|
|
|
yield v.weight
|
|
|
|
|
2019-11-24 03:03:13 +00:00
|
|
|
vertex_cache_entry = tuple(get_cache_vertex())
|
2019-11-20 02:10:44 +00:00
|
|
|
cached_vertex_index = cache.get(vertex_cache_entry, vertex_cache_miss_index)
|
|
|
|
|
|
|
|
if cached_vertex_index != vertex_cache_miss_index:
|
|
|
|
remap[(vertex_index, loop_index)] = cached_vertex_index
|
|
|
|
|
|
|
|
return cached_vertex_index
|
2019-11-11 10:03:52 +00:00
|
|
|
|
|
|
|
new_index: int = len(segment.positions)
|
2019-11-20 02:10:44 +00:00
|
|
|
cache[vertex_cache_entry] = new_index
|
2019-11-15 07:02:04 +00:00
|
|
|
remap[(vertex_index, loop_index)] = new_index
|
|
|
|
|
2019-11-15 07:41:31 +00:00
|
|
|
segment.positions.append(convert_vector_space(mesh.vertices[vertex_index].co))
|
2019-11-24 03:03:13 +00:00
|
|
|
segment.normals.append(convert_vector_space(vertex_normal))
|
2019-11-11 10:03:52 +00:00
|
|
|
|
|
|
|
if mesh.uv_layers.active is None:
|
|
|
|
segment.texcoords.append(Vector((0.0, 0.0)))
|
|
|
|
else:
|
2019-11-15 07:02:04 +00:00
|
|
|
segment.texcoords.append(mesh.uv_layers.active.data[loop_index].uv.copy())
|
2019-11-11 10:03:52 +00:00
|
|
|
|
|
|
|
if segment.colors is not None:
|
2019-11-21 10:48:42 +00:00
|
|
|
segment.colors.append(list(mesh.vertex_colors.active.data[loop_index].color))
|
2019-11-11 10:03:52 +00:00
|
|
|
|
2020-10-16 00:40:27 +00:00
|
|
|
if segment.weights is not None:
|
|
|
|
groups = mesh.vertices[vertex_index].groups
|
|
|
|
|
|
|
|
segment.weights.append([VertexWeight(v.weight, v.group) for v in groups])
|
|
|
|
|
2019-11-11 10:03:52 +00:00
|
|
|
return new_index
|
|
|
|
|
|
|
|
for tri in mesh.loop_triangles:
|
|
|
|
polygons[tri.material_index].add(tri.polygon_index)
|
2019-11-15 07:41:31 +00:00
|
|
|
segments[tri.material_index].triangles.append([
|
2022-09-23 15:50:29 +00:00
|
|
|
add_vertex(tri.material_index, tri.vertices[0], tri.loops[0]),
|
|
|
|
add_vertex(tri.material_index, tri.vertices[1], tri.loops[1]),
|
|
|
|
add_vertex(tri.material_index, tri.vertices[2], tri.loops[2])])
|
2019-11-11 10:03:52 +00:00
|
|
|
|
|
|
|
for segment, remap, polys in zip(segments, vertex_remap, polygons):
|
|
|
|
for poly_index in polys:
|
|
|
|
poly = mesh.polygons[poly_index]
|
|
|
|
|
2019-11-15 07:02:04 +00:00
|
|
|
segment.polygons.append([remap[(v, l)] for v, l in zip(poly.vertices, poly.loop_indices)])
|
2019-11-11 10:03:52 +00:00
|
|
|
|
|
|
|
return segments
|
|
|
|
|
2020-11-29 23:32:17 +00:00
|
|
|
def get_model_type(obj: bpy.types.Object, skel_only: bool) -> ModelType:
|
2019-11-11 10:03:52 +00:00
|
|
|
""" Get the ModelType for a Blender object. """
|
|
|
|
|
2020-11-29 23:32:17 +00:00
|
|
|
if obj.type in MESH_OBJECT_TYPES and not skel_only:
|
2020-10-16 00:40:27 +00:00
|
|
|
if obj.vertex_groups:
|
|
|
|
return ModelType.SKIN
|
|
|
|
else:
|
|
|
|
return ModelType.STATIC
|
2019-11-11 10:03:52 +00:00
|
|
|
|
|
|
|
return ModelType.NULL
|
|
|
|
|
|
|
|
def get_is_model_hidden(obj: bpy.types.Object) -> bool:
|
|
|
|
""" Gets if a Blender object should be marked as hidden in the .msh file. """
|
|
|
|
|
|
|
|
name = obj.name.lower()
|
|
|
|
|
2022-01-07 18:45:50 +00:00
|
|
|
if name.startswith("c_"):
|
|
|
|
return True
|
2019-11-11 10:03:52 +00:00
|
|
|
if name.startswith("sv_"):
|
|
|
|
return True
|
|
|
|
if name.startswith("p_"):
|
|
|
|
return True
|
|
|
|
if name.startswith("collision"):
|
|
|
|
return True
|
2019-11-21 10:23:52 +00:00
|
|
|
|
2019-11-11 10:03:52 +00:00
|
|
|
if obj.type not in MESH_OBJECT_TYPES:
|
|
|
|
return True
|
|
|
|
|
2019-11-21 10:23:52 +00:00
|
|
|
if name.endswith("_lod2"):
|
|
|
|
return True
|
|
|
|
if name.endswith("_lod3"):
|
|
|
|
return True
|
|
|
|
if name.endswith("_lowrez"):
|
|
|
|
return True
|
|
|
|
if name.endswith("_lowres"):
|
|
|
|
return True
|
|
|
|
|
2019-11-11 10:03:52 +00:00
|
|
|
return False
|
2019-11-15 07:41:31 +00:00
|
|
|
|
2019-11-15 09:40:47 +00:00
|
|
|
def get_is_collision_primitive(obj: bpy.types.Object) -> bool:
|
|
|
|
""" Gets if a Blender object represents a collision primitive. """
|
|
|
|
|
|
|
|
name = obj.name.lower()
|
|
|
|
|
|
|
|
return name.startswith("p_")
|
|
|
|
|
|
|
|
def get_collision_primitive(obj: bpy.types.Object) -> CollisionPrimitive:
|
|
|
|
""" Gets the CollisionPrimitive of an object or raises an error if
|
|
|
|
it can't. """
|
|
|
|
|
|
|
|
primitive = CollisionPrimitive()
|
|
|
|
primitive.shape = get_collision_primitive_shape(obj)
|
|
|
|
|
|
|
|
if primitive.shape == CollisionPrimitiveShape.SPHERE:
|
|
|
|
# Tolerate a 5% difference to account for icospheres with 2 subdivisions.
|
|
|
|
if not (math.isclose(obj.dimensions[0], obj.dimensions[1], rel_tol=0.05) and
|
|
|
|
math.isclose(obj.dimensions[0], obj.dimensions[2], rel_tol=0.05)):
|
|
|
|
raise RuntimeError(f"Object '{obj.name}' is being used as a sphere collision "
|
|
|
|
f"primitive but it's dimensions are not uniform!")
|
|
|
|
|
|
|
|
primitive.radius = max(obj.dimensions[0], obj.dimensions[1], obj.dimensions[2]) * 0.5
|
|
|
|
elif primitive.shape == CollisionPrimitiveShape.CYLINDER:
|
2022-09-27 11:56:25 +00:00
|
|
|
primitive.radius = max(obj.dimensions[0], obj.dimensions[1]) * 0.5
|
2019-11-18 12:33:55 +00:00
|
|
|
primitive.height = obj.dimensions[2]
|
2019-11-15 09:40:47 +00:00
|
|
|
elif primitive.shape == CollisionPrimitiveShape.BOX:
|
|
|
|
primitive.radius = obj.dimensions[0] * 0.5
|
|
|
|
primitive.height = obj.dimensions[2] * 0.5
|
|
|
|
primitive.length = obj.dimensions[1] * 0.5
|
|
|
|
|
|
|
|
return primitive
|
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2019-11-15 09:40:47 +00:00
|
|
|
def get_collision_primitive_shape(obj: bpy.types.Object) -> CollisionPrimitiveShape:
|
|
|
|
""" Gets the CollisionPrimitiveShape of an object or raises an error if
|
|
|
|
it can't. """
|
|
|
|
|
|
|
|
name = obj.name.lower()
|
|
|
|
|
|
|
|
if "sphere" in name or "sphr" in name or "spr" in name:
|
|
|
|
return CollisionPrimitiveShape.SPHERE
|
|
|
|
if "cylinder" in name or "cyln" in name or "cyl" in name:
|
|
|
|
return CollisionPrimitiveShape.CYLINDER
|
2019-11-18 14:41:46 +00:00
|
|
|
if "box" in name or "cube" in name or "cuboid" in name:
|
2019-11-15 09:40:47 +00:00
|
|
|
return CollisionPrimitiveShape.BOX
|
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
# arc170 fighter has examples of box colliders without proper naming
|
|
|
|
prim_type = obj.swbf_msh_coll_prim.prim_type
|
|
|
|
if prim_type in [item.value for item in CollisionPrimitiveShape]:
|
|
|
|
return CollisionPrimitiveShape(prim_type)
|
|
|
|
|
|
|
|
raise RuntimeError(f"Object '{obj.name}' has no primitive type specified in it's name!")
|
|
|
|
|
2019-11-15 09:40:47 +00:00
|
|
|
|
2019-11-21 10:23:52 +00:00
|
|
|
def check_for_bad_lod_suffix(obj: bpy.types.Object):
|
|
|
|
""" Checks if the object has an LOD suffix that is known to be ignored by """
|
|
|
|
|
|
|
|
name = obj.name.lower()
|
|
|
|
failure_message = f"Object '{obj.name}' has unknown LOD suffix at the end of it's name!"
|
|
|
|
|
|
|
|
if name.endswith("_lod1"):
|
|
|
|
raise RuntimeError(failure_message)
|
|
|
|
|
|
|
|
for i in range(4, 10):
|
|
|
|
if name.endswith(f"_lod{i}"):
|
|
|
|
raise RuntimeError(failure_message)
|
|
|
|
|
2020-01-06 04:08:36 +00:00
|
|
|
def select_objects(export_target: str) -> List[bpy.types.Object]:
|
|
|
|
""" Returns a list of objects to export. """
|
|
|
|
|
|
|
|
if export_target == "SCENE" or not export_target in {"SELECTED", "SELECTED_WITH_CHILDREN"}:
|
|
|
|
return list(bpy.context.scene.objects)
|
|
|
|
|
|
|
|
objects = list(bpy.context.selected_objects)
|
|
|
|
added = {obj.name for obj in objects}
|
|
|
|
|
|
|
|
if export_target == "SELECTED_WITH_CHILDREN":
|
|
|
|
children = []
|
|
|
|
|
|
|
|
def add_children(parent):
|
|
|
|
nonlocal children
|
|
|
|
nonlocal added
|
|
|
|
|
|
|
|
for obj in bpy.context.scene.objects:
|
|
|
|
if obj.parent == parent and obj.name not in added:
|
|
|
|
children.append(obj)
|
|
|
|
added.add(obj.name)
|
|
|
|
|
|
|
|
add_children(obj)
|
|
|
|
|
|
|
|
|
|
|
|
for obj in objects:
|
|
|
|
add_children(obj)
|
|
|
|
|
|
|
|
objects = objects + children
|
|
|
|
|
|
|
|
parents = []
|
|
|
|
|
|
|
|
for obj in objects:
|
|
|
|
parent = obj.parent
|
|
|
|
|
|
|
|
while parent is not None:
|
|
|
|
if parent.name not in added:
|
|
|
|
parents.append(parent)
|
|
|
|
added.add(parent.name)
|
|
|
|
|
|
|
|
parent = parent.parent
|
|
|
|
|
|
|
|
return objects + parents
|
|
|
|
|
2020-10-20 03:18:08 +00:00
|
|
|
|
|
|
|
|
2022-09-29 19:18:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def expand_armature(armature: bpy.types.Object) -> Dict[str, Model]:
|
2022-01-18 20:16:49 +00:00
|
|
|
|
|
|
|
proper_BONES = get_real_BONES(armature)
|
|
|
|
|
2022-09-29 19:18:58 +00:00
|
|
|
bones: Dict[str, Model] = {}
|
2020-10-16 00:40:27 +00:00
|
|
|
|
2022-01-13 20:52:30 +00:00
|
|
|
for bone in armature.data.bones:
|
2020-10-16 00:40:27 +00:00
|
|
|
model = Model()
|
|
|
|
|
|
|
|
transform = bone.matrix_local
|
|
|
|
|
|
|
|
if bone.parent:
|
|
|
|
transform = bone.parent.matrix_local.inverted() @ transform
|
|
|
|
model.parent = bone.parent.name
|
2022-01-13 20:52:30 +00:00
|
|
|
# If the bone has no parent_bone:
|
|
|
|
# set model parent to SKIN object if there is one
|
|
|
|
# set model parent to armature parent if there is one
|
2020-10-16 00:40:27 +00:00
|
|
|
else:
|
2022-09-28 15:27:52 +00:00
|
|
|
|
2022-09-29 19:18:58 +00:00
|
|
|
bone_world_matrix = get_bone_world_matrix(armature, bone.name)
|
2022-09-28 15:27:52 +00:00
|
|
|
parent_obj = None
|
|
|
|
|
2022-01-28 22:28:12 +00:00
|
|
|
for child_obj in armature.original.children:
|
2022-01-13 20:52:30 +00:00
|
|
|
if child_obj.vertex_groups and not get_is_model_hidden(child_obj) and not child_obj.parent_bone:
|
2022-09-28 15:27:52 +00:00
|
|
|
#model.parent = child_obj.name
|
|
|
|
parent_obj = child_obj
|
2022-01-13 20:52:30 +00:00
|
|
|
break
|
2022-09-28 15:27:52 +00:00
|
|
|
|
|
|
|
if parent_obj:
|
|
|
|
transform = parent_obj.matrix_world.inverted() @ bone_world_matrix
|
|
|
|
model.parent = parent_obj.name
|
|
|
|
elif not parent_obj and armature.parent:
|
|
|
|
transform = armature.parent.matrix_world.inverted() @ bone_world_matrix
|
2022-01-13 20:52:30 +00:00
|
|
|
model.parent = armature.parent.name
|
2022-09-28 15:27:52 +00:00
|
|
|
else:
|
|
|
|
transform = bone_world_matrix
|
|
|
|
model.parent = ""
|
|
|
|
|
2020-11-29 23:32:17 +00:00
|
|
|
|
2020-10-16 00:40:27 +00:00
|
|
|
|
|
|
|
local_translation, local_rotation, _ = transform.decompose()
|
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
model.model_type = ModelType.BONE if bone.name in proper_BONES else ModelType.NULL
|
2020-10-16 00:40:27 +00:00
|
|
|
model.name = bone.name
|
|
|
|
model.transform.rotation = convert_rotation_space(local_rotation)
|
|
|
|
model.transform.translation = convert_vector_space(local_translation)
|
|
|
|
|
2022-09-29 19:18:58 +00:00
|
|
|
bones[bone.name] = model
|
2020-10-16 00:40:27 +00:00
|
|
|
|
|
|
|
return bones
|