Skip to content

Commit

Permalink
Suppress exceptions for missing group objects #125
Browse files Browse the repository at this point in the history
  • Loading branch information
UuuNyaa committed Dec 18, 2023
1 parent 0559750 commit 4605fcc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion mmd_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
27 changes: 15 additions & 12 deletions mmd_tools/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -144,15 +144,17 @@ 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:
yield child
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):
Expand All @@ -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)),
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 4605fcc

Please sign in to comment.