From 4605fcc96389a00e64961a0bd3f860acf972a751 Mon Sep 17 00:00:00 2001 From: UuuNyaa Date: Sun, 17 Dec 2023 23:09:23 -0800 Subject: [PATCH] Suppress exceptions for missing group objects #125 --- mmd_tools/__init__.py | 2 +- mmd_tools/core/model.py | 27 +++++++++++++++------------ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/mmd_tools/__init__.py b/mmd_tools/__init__.py index dd2de076..221b9b3f 100644 --- a/mmd_tools/__init__.py +++ b/mmd_tools/__init__.py @@ -3,7 +3,7 @@ bl_info = { "name": "mmd_tools", "author": "sugiany", - "version": (2, 10, 1), + "version": (2, 10, 2), "blender": (2, 93, 0), "location": "View3D > Sidebar > MMD Tools Panel", "description": "Utility tools for MMD model editing. (UuuNyaa's forked version)", diff --git a/mmd_tools/core/model.py b/mmd_tools/core/model.py index 30d5d7a5..584ae9f3 100644 --- a/mmd_tools/core/model.py +++ b/mmd_tools/core/model.py @@ -3,7 +3,7 @@ import itertools import logging import time -from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, List, Optional, Set, Union +from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, Iterator, List, Optional, Set, Union import bpy import idprop @@ -144,7 +144,7 @@ def find_bone_order_mesh_object(root_object: bpy.types.Object) -> Optional[bpy.t return next(filter(lambda o: o.type == 'MESH' and 'mmd_bone_order_override' in o.modifiers, armature_object.children), None) @staticmethod - def all_children(obj: bpy.types.Object) -> Iterable[bpy.types.Object]: + def all_children(obj: bpy.types.Object) -> Iterator[bpy.types.Object]: child: bpy.types.Object obj.children_recursive for child in obj.children: @@ -152,7 +152,9 @@ def all_children(obj: bpy.types.Object) -> Iterable[bpy.types.Object]: yield from FnModel.all_children(child) @staticmethod - def filtered_children(condition_function: Callable[[bpy.types.Object], bool], obj: bpy.types.Object) -> Iterable[bpy.types.Object]: + def filtered_children(condition_function: Callable[[bpy.types.Object], bool], obj: Optional[bpy.types.Object]) -> Iterator[bpy.types.Object]: + if obj is None: + return child: bpy.types.Object for child in obj.children: if condition_function(child): @@ -161,11 +163,11 @@ def filtered_children(condition_function: Callable[[bpy.types.Object], bool], ob yield from FnModel.filtered_children(condition_function, child) @staticmethod - def child_meshes(obj: bpy.types.Object) -> Iterable[bpy.types.Object]: + def child_meshes(obj: bpy.types.Object) -> Iterator[bpy.types.Object]: return FnModel.filtered_children(lambda x: x.type == 'MESH' and x.mmd_type == 'NONE', obj) @staticmethod - def iterate_rigid_body_objects(root_object: bpy.types.Object) -> Iterable[bpy.types.Object]: + def iterate_rigid_body_objects(root_object: bpy.types.Object) -> Iterator[bpy.types.Object]: if root_object.mmd_root.is_built: return itertools.chain( FnModel.filtered_children(FnModel.is_rigid_body_object, FnModel.find_armature(root_object)), @@ -174,19 +176,20 @@ def iterate_rigid_body_objects(root_object: bpy.types.Object) -> Iterable[bpy.ty return FnModel.filtered_children(FnModel.is_rigid_body_object, FnModel.find_rigid_group(root_object)) @staticmethod - def iterate_joint_objects(root_object: bpy.types.Object) -> Iterable[bpy.types.Object]: + def iterate_joint_objects(root_object: bpy.types.Object) -> Iterator[bpy.types.Object]: return FnModel.filtered_children(FnModel.is_joint_object, FnModel.find_joint_group(root_object)) @staticmethod - def iterate_temporary_objects(root_object: bpy.types.Object, rigid_track_only: bool = False) -> Iterable[bpy.types.Object]: - rigid_group_object = FnModel.find_rigid_group(root_object) - rigid_body_objects = [] if rigid_group_object is None else FnModel.filtered_children(FnModel.is_temporary_object, rigid_group_object) + def iterate_temporary_objects(root_object: bpy.types.Object, rigid_track_only: bool = False) -> Iterator[bpy.types.Object]: + rigid_body_objects = FnModel.filtered_children(FnModel.is_temporary_object, FnModel.find_rigid_group(root_object)) if rigid_track_only: return rigid_body_objects temporary_group_object = FnModel.find_temporary_group(root_object) - return rigid_body_objects if temporary_group_object is None else itertools.chain(rigid_body_objects, FnModel.filtered_children(FnModel.is_temporary_object, temporary_group_object)) + if temporary_group_object is None: + return rigid_body_objects + return itertools.chain(rigid_body_objects, FnModel.filtered_children(FnModel.is_temporary_object, temporary_group_object)) @staticmethod def is_root_object(obj: bpy.types.Object): @@ -788,7 +791,7 @@ def create_ik_constraint(self, bone, ik_target): ik_const.subtarget = ik_target_name return ik_const - def allObjects(self, obj: Optional[bpy.types.Object] = None) -> Iterable[bpy.types.Object]: + def allObjects(self, obj: Optional[bpy.types.Object] = None) -> Iterator[bpy.types.Object]: if obj is None: obj: bpy.types.Object = self.__root yield obj @@ -856,7 +859,7 @@ def meshes(self): return [] return FnModel.child_meshes(arm) - def attachMeshes(self, meshes: Iterable[bpy.types.Object], add_armature_modifier: bool = True): + def attachMeshes(self, meshes: Iterator[bpy.types.Object], add_armature_modifier: bool = True): FnModel.attach_meshes(self.rootObject(), meshes, add_armature_modifier) def firstMesh(self):