From 1432c2990a90bc61e045166ea7ea550912b4d850 Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Fri, 17 Nov 2023 22:00:22 +0800 Subject: [PATCH] fix issues. - fix issue according to python linter - do not write vt group if no changes. reduce the cost of virtools group class. --- bbp_ng/OP_IMPORT_virtools.py | 2 +- bbp_ng/PROP_ballance_element.py | 8 ++++---- bbp_ng/PROP_virtools_group.py | 11 ++++++++++- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/bbp_ng/OP_IMPORT_virtools.py b/bbp_ng/OP_IMPORT_virtools.py index 7f6f399..776c651 100644 --- a/bbp_ng/OP_IMPORT_virtools.py +++ b/bbp_ng/OP_IMPORT_virtools.py @@ -236,7 +236,7 @@ def _import_virtools_meshes( # return yield face - def mtl_iterator() -> typing.Iterator[bpy.types.Material]: + def mtl_iterator() -> typing.Iterator[bpy.types.Material | None]: for vtmtl in vtmesh.get_material_slots(): if vtmtl: yield material_cret_map.get(vtmtl, None) diff --git a/bbp_ng/PROP_ballance_element.py b/bbp_ng/PROP_ballance_element.py index 338e37c..1143690 100644 --- a/bbp_ng/PROP_ballance_element.py +++ b/bbp_ng/PROP_ballance_element.py @@ -36,7 +36,7 @@ class BallanceElementType(enum.IntEnum): _g_ElementCount: int = len(BallanceElementType) _g_ElementNameIdMap: dict[str, int] = dict((entry.name, entry.value) for entry in BallanceElementType) -_g_ElementIdNameMap: dict[str, int] = dict((entry.value, entry.name) for entry in BallanceElementType) +_g_ElementIdNameMap: dict[int, str] = dict((entry.value, entry.name) for entry in BallanceElementType) def get_ballance_element_id(name: str) -> int | None: """ @@ -47,7 +47,7 @@ def get_ballance_element_id(name: str) -> int | None: """ return _g_ElementNameIdMap.get(name, None) -def get_ballance_element_name(id: int) -> int | None: +def get_ballance_element_name(id: int) -> str | None: """ Get Ballance element name by its ID @@ -250,10 +250,10 @@ class BallanceElementsHelper(): return mesh # if no existing one, create new one - new_mesh_name: str | None = get_ballance_element_id(element_id) + new_mesh_name: str | None = get_ballance_element_name(element_id) if new_mesh_name is None: raise UTIL_functions.BBPException('invalid element id') - new_mesh: bpy.types.Mesh = bpy.data.meshes.new(get_ballance_element_name(element_id)) + new_mesh: bpy.types.Mesh = bpy.data.meshes.new(new_mesh_name) _load_element(new_mesh, element_id) self.__mElementMap[element_id] = new_mesh diff --git a/bbp_ng/PROP_virtools_group.py b/bbp_ng/PROP_virtools_group.py index 5178e07..3d7dbfb 100644 --- a/bbp_ng/PROP_virtools_group.py +++ b/bbp_ng/PROP_virtools_group.py @@ -28,12 +28,14 @@ class VirtoolsGroupsHelper(): """ __mSingletonMutex: typing.ClassVar[bool] = False __mIsValid: bool + __mNoChange: bool ##< A bool indicate whether any change happended during lifetime. If no change, skip the writing when exiting. __mAssocObj: bpy.types.Object __mGroupsSet: set[str] def __init__(self, assoc: bpy.types.Object): self.__mGroupsSet = set() self.__mAssocObj = assoc + self.__mNoChange = True # check singleton if VirtoolsGroupsHelper.__mSingletonMutex: @@ -56,8 +58,10 @@ class VirtoolsGroupsHelper(): def dispose(self) -> None: if self.is_valid(): + # if have changes, # write to ballance elements property and reset validation - self.__write_to_virtools_groups() + if not self.__mNoChange: + self.__write_to_virtools_groups() self.__mIsValid = False VirtoolsGroupsHelper.__mSingletonMutex = False @@ -67,18 +71,22 @@ class VirtoolsGroupsHelper(): def add_group(self, gname: str) -> None: self.__check_valid() + self.__mNoChange = False self.__mGroupsSet.add(gname) def add_groups(self, gnames: typing.Iterable[str]) -> None: self.__check_valid() + self.__mNoChange = False self.__mGroupsSet.update(gnames) def remove_group(self, gname: str) -> None: self.__check_valid() + self.__mNoChange = False self.__mGroupsSet.discard(gname) def remove_groups(self, gnames: typing.Iterable[str]) -> None: self.__check_valid() + self.__mNoChange = False for gname in gnames: self.__mGroupsSet.discard(gname) @@ -103,6 +111,7 @@ class VirtoolsGroupsHelper(): def clear_all_groups(self): self.__check_valid() + self.__mNoChange = False self.__mGroupsSet.clear() def __write_to_virtools_groups(self) -> None: