fix issues.

- fix issue according to python linter
- do not write vt group if no changes. reduce the cost of virtools group class.
This commit is contained in:
yyc12345 2023-11-17 22:00:22 +08:00
parent 1a2dd08092
commit 1432c2990a
3 changed files with 15 additions and 6 deletions

View File

@ -236,7 +236,7 @@ def _import_virtools_meshes(
# return # return
yield face 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(): for vtmtl in vtmesh.get_material_slots():
if vtmtl: if vtmtl:
yield material_cret_map.get(vtmtl, None) yield material_cret_map.get(vtmtl, None)

View File

@ -36,7 +36,7 @@ class BallanceElementType(enum.IntEnum):
_g_ElementCount: int = len(BallanceElementType) _g_ElementCount: int = len(BallanceElementType)
_g_ElementNameIdMap: dict[str, int] = dict((entry.name, entry.value) for entry in 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: 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) 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 Get Ballance element name by its ID
@ -250,10 +250,10 @@ class BallanceElementsHelper():
return mesh return mesh
# if no existing one, create new one # 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: if new_mesh_name is None:
raise UTIL_functions.BBPException('invalid element id') 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) _load_element(new_mesh, element_id)
self.__mElementMap[element_id] = new_mesh self.__mElementMap[element_id] = new_mesh

View File

@ -28,12 +28,14 @@ class VirtoolsGroupsHelper():
""" """
__mSingletonMutex: typing.ClassVar[bool] = False __mSingletonMutex: typing.ClassVar[bool] = False
__mIsValid: bool __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 __mAssocObj: bpy.types.Object
__mGroupsSet: set[str] __mGroupsSet: set[str]
def __init__(self, assoc: bpy.types.Object): def __init__(self, assoc: bpy.types.Object):
self.__mGroupsSet = set() self.__mGroupsSet = set()
self.__mAssocObj = assoc self.__mAssocObj = assoc
self.__mNoChange = True
# check singleton # check singleton
if VirtoolsGroupsHelper.__mSingletonMutex: if VirtoolsGroupsHelper.__mSingletonMutex:
@ -56,8 +58,10 @@ class VirtoolsGroupsHelper():
def dispose(self) -> None: def dispose(self) -> None:
if self.is_valid(): if self.is_valid():
# if have changes,
# write to ballance elements property and reset validation # 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 self.__mIsValid = False
VirtoolsGroupsHelper.__mSingletonMutex = False VirtoolsGroupsHelper.__mSingletonMutex = False
@ -67,18 +71,22 @@ class VirtoolsGroupsHelper():
def add_group(self, gname: str) -> None: def add_group(self, gname: str) -> None:
self.__check_valid() self.__check_valid()
self.__mNoChange = False
self.__mGroupsSet.add(gname) self.__mGroupsSet.add(gname)
def add_groups(self, gnames: typing.Iterable[str]) -> None: def add_groups(self, gnames: typing.Iterable[str]) -> None:
self.__check_valid() self.__check_valid()
self.__mNoChange = False
self.__mGroupsSet.update(gnames) self.__mGroupsSet.update(gnames)
def remove_group(self, gname: str) -> None: def remove_group(self, gname: str) -> None:
self.__check_valid() self.__check_valid()
self.__mNoChange = False
self.__mGroupsSet.discard(gname) self.__mGroupsSet.discard(gname)
def remove_groups(self, gnames: typing.Iterable[str]) -> None: def remove_groups(self, gnames: typing.Iterable[str]) -> None:
self.__check_valid() self.__check_valid()
self.__mNoChange = False
for gname in gnames: for gname in gnames:
self.__mGroupsSet.discard(gname) self.__mGroupsSet.discard(gname)
@ -103,6 +111,7 @@ class VirtoolsGroupsHelper():
def clear_all_groups(self): def clear_all_groups(self):
self.__check_valid() self.__check_valid()
self.__mNoChange = False
self.__mGroupsSet.clear() self.__mGroupsSet.clear()
def __write_to_virtools_groups(self) -> None: def __write_to_virtools_groups(self) -> None: