From fda37656c981e19c4ad43e1c9a2ee6561bc57c90 Mon Sep 17 00:00:00 2001 From: Salad Dais Date: Thu, 24 Jun 2021 05:26:53 +0000 Subject: [PATCH] Reduce boilerplate for mesh mangling addons Makes it less annoying to compose separate addons with different manglers --- addon_examples/local_mesh.py | 19 +++++++++++++++++++ addon_examples/mesh_mangler.py | 34 +++++++++------------------------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/addon_examples/local_mesh.py b/addon_examples/local_mesh.py index 2c2228a..fba2480 100644 --- a/addon_examples/local_mesh.py +++ b/addon_examples/local_mesh.py @@ -280,4 +280,23 @@ class MeshUploadInterceptingAddon(BaseAddon): cls._replace_local_mesh(session.main_region, asset_repo, mesh_list) +class BaseMeshManglerAddon(BaseAddon): + """Base class for addons that mangle uploaded or local mesh""" + MESH_MANGLERS: List[Callable[[MeshAsset], MeshAsset]] + + def handle_init(self, session_manager: SessionManager): + # Add our manglers into the list + MeshUploadInterceptingAddon.mesh_manglers.extend(self.MESH_MANGLERS) + # Tell the local mesh plugin that the mangler list changed, and to re-apply + MeshUploadInterceptingAddon.remangle_local_mesh(session_manager) + + def handle_unload(self, session_manager: SessionManager): + # Clean up our manglers before we go away + mangler_list = MeshUploadInterceptingAddon.mesh_manglers + for mangler in self.MESH_MANGLERS: + if mangler in mangler_list: + mangler_list.remove(mangler) + MeshUploadInterceptingAddon.remangle_local_mesh(session_manager) + + addons = [MeshUploadInterceptingAddon()] diff --git a/addon_examples/mesh_mangler.py b/addon_examples/mesh_mangler.py index 9f7e0b0..4435881 100644 --- a/addon_examples/mesh_mangler.py +++ b/addon_examples/mesh_mangler.py @@ -11,8 +11,6 @@ to add to give a mesh an arbitrary center of rotation / scaling. from hippolyzer.lib.base.mesh import MeshAsset from hippolyzer.lib.proxy.addons import AddonManager -from hippolyzer.lib.proxy.addon_utils import BaseAddon -from hippolyzer.lib.proxy.sessions import SessionManager import local_mesh AddonManager.hot_reload(local_mesh, require_addons_loaded=True) @@ -37,6 +35,9 @@ def reorient_mesh(orientation): # X=1, Y=2, Z=3 def _reorienter(mesh: MeshAsset): for material in mesh.iter_lod_materials(): + if "Position" not in material: + # Must be a NoGeometry LOD + continue # We don't need to use positions_(to/from)_domain here since we're just naively # flipping the axes around. material["Position"] = _reorient_coord_list(material["Position"], orientation) @@ -46,28 +47,11 @@ def reorient_mesh(orientation): return _reorienter -OUR_MANGLERS = [ - # Negate the X and Y axes on any mesh we upload or create temp - reorient_mesh((-1, -2, 3)), -] +class ExampleMeshManglerAddon(local_mesh.BaseMeshManglerAddon): + MESH_MANGLERS = [ + # Negate the X and Y axes on any mesh we upload or create temp + reorient_mesh((-1, -2, 3)), + ] -class MeshManglerExampleAddon(BaseAddon): - def handle_init(self, session_manager: SessionManager): - # Add our manglers into the list - local_mesh_addon = local_mesh.MeshUploadInterceptingAddon - local_mesh_addon.mesh_manglers.extend(OUR_MANGLERS) - # Tell the local mesh plugin that the mangler list changed, and to re-apply - local_mesh_addon.remangle_local_mesh(session_manager) - - def handle_unload(self, session_manager: SessionManager): - # Clean up our manglers before we go away - local_mesh_addon = local_mesh.MeshUploadInterceptingAddon - mangler_list = local_mesh_addon.mesh_manglers - for mangler in OUR_MANGLERS: - if mangler in mangler_list: - mangler_list.remove(mangler) - local_mesh_addon.remangle_local_mesh(session_manager) - - -addons = [MeshManglerExampleAddon()] +addons = [ExampleMeshManglerAddon()]