SWBF-msh-Blender-IO/addons/io_scene_swbf_msh/msh_scene.py

105 lines
3.4 KiB
Python
Raw Normal View History

2019-11-11 10:03:52 +00:00
""" Contains Scene object for representing a .msh file and the function to create one
from a Blender scene. """
from dataclasses import dataclass, field
2019-11-15 03:28:09 +00:00
from typing import List, Dict
2019-11-11 10:03:52 +00:00
from copy import copy
import bpy
from mathutils import Vector
2020-10-11 16:11:15 +00:00
from .msh_model import Model, Animation
2019-11-11 10:03:52 +00:00
from .msh_model_gather import gather_models
from .msh_model_utilities import sort_by_parent, has_multiple_root_models, reparent_model_roots, get_model_world_matrix
from .msh_model_triangle_strips import create_models_triangle_strips
2019-11-15 03:28:09 +00:00
from .msh_material import *
from .msh_material_gather import gather_materials
from .msh_material_utilities import remove_unused_materials
2019-11-11 10:03:52 +00:00
from .msh_utilities import *
2020-10-11 16:11:15 +00:00
from .msh_anim_gather import *
2019-11-11 10:03:52 +00:00
@dataclass
class SceneAABB:
""" Class representing an axis-aligned bounding box. """
AABB_INIT_MAX = -3.402823466e+38
AABB_INIT_MIN = 3.402823466e+38
max_: Vector = Vector((AABB_INIT_MAX, AABB_INIT_MAX, AABB_INIT_MAX))
min_: Vector = Vector((AABB_INIT_MIN, AABB_INIT_MIN, AABB_INIT_MIN))
def integrate_aabb(self, other):
""" Merge another AABB with this AABB. """
self.max_ = max_vec(self.max_, other.max_)
self.min_ = min_vec(self.min_, other.min_)
def integrate_position(self, position):
""" Integrate a position with the AABB, potentially expanding it. """
self.max_ = max_vec(self.max_, position)
self.min_ = min_vec(self.min_, position)
@dataclass
class Scene:
""" Class containing the scene data for a .msh """
name: str = "Scene"
2019-11-15 03:28:09 +00:00
materials: Dict[str, Material] = field(default_factory=dict)
2019-11-11 10:03:52 +00:00
models: List[Model] = field(default_factory=list)
2020-10-11 16:11:15 +00:00
anims: List[Animation] = field(default_factory=list)
2019-11-11 10:03:52 +00:00
2020-10-14 20:06:04 +00:00
def create_scene(generate_triangle_strips: bool, apply_modifiers: bool, export_target: str, skel_only: bool) -> Scene:
2019-11-11 10:03:52 +00:00
""" Create a msh Scene from the active Blender scene. """
scene = Scene()
scene.name = bpy.context.scene.name
2019-11-15 03:28:09 +00:00
scene.materials = gather_materials()
scene.models = gather_models(apply_modifiers=apply_modifiers, export_target=export_target, skeleton_only=skel_only)
2019-11-11 10:03:52 +00:00
scene.models = sort_by_parent(scene.models)
if generate_triangle_strips:
scene.models = create_models_triangle_strips(scene.models)
else:
for model in scene.models:
if model.geometry:
for segment in model.geometry:
segment.triangle_strips = segment.triangles
2019-11-11 10:03:52 +00:00
if has_multiple_root_models(scene.models):
scene.models = reparent_model_roots(scene.models)
2019-11-15 03:28:09 +00:00
scene.materials = remove_unused_materials(scene.materials, scene.models)
#creates a dummy basepose if no Action is selected
if "Armature" in bpy.context.scene.objects.keys():
scene.anims = [extract_anim(bpy.context.scene.objects["Armature"])]
2020-10-11 16:11:15 +00:00
2019-11-11 10:03:52 +00:00
return scene
2019-11-11 10:03:52 +00:00
def create_scene_aabb(scene: Scene) -> SceneAABB:
""" Create a SceneAABB for a Scene. """
global_aabb = SceneAABB()
for model in scene.models:
if model.geometry is None or model.hidden:
2019-11-11 10:03:52 +00:00
continue
model_world_matrix = get_model_world_matrix(model, scene.models)
model_aabb = SceneAABB()
for segment in model.geometry:
segment_aabb = SceneAABB()
for pos in segment.positions:
segment_aabb.integrate_position(model_world_matrix @ pos)
model_aabb.integrate_aabb(segment_aabb)
global_aabb.integrate_aabb(model_aabb)
return global_aabb