Skip to content

Commit

Permalink
Make bounds enum properties less verbose.
Browse files Browse the repository at this point in the history
Every time we need to make a place to store a region's bounds, we have
to write quite a bit of boilerplate code to stash the bounds type into
the collision modifier. This removes the need to do that by making a
helper function that generates those helper functions for us. Generally,
we would use `functools.partial` to do this, but Blender requires
function objects for the `EnumProperty`'s getter and setter. Partial
objects raise a `TypeError`.
  • Loading branch information
Hoikas committed Jun 22, 2024
1 parent c6b5422 commit a833fa8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 53 deletions.
31 changes: 19 additions & 12 deletions korman/nodes/node_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
from PyHSPlasma import *
from typing import *

from .. import bounds_helpers
from .node_core import *
from ..properties.modifiers.physics import bounds_types
from .. import idprops

class PlasmaClickableNode(idprops.IDPropObjectMixin, PlasmaNodeBase, bpy.types.Node):
Expand All @@ -38,10 +38,13 @@ class PlasmaClickableNode(idprops.IDPropObjectMixin, PlasmaNodeBase, bpy.types.N
description="Mesh object that is clickable",
type=bpy.types.Object,
poll=idprops.poll_mesh_objects)
bounds = EnumProperty(name="Bounds",
description="Clickable's bounds (NOTE: only used if your clickable is not a collider)",
items=bounds_types,
default="hull")

bounds = bounds_helpers.enum_property(
"bounds", store_on_collider=False,
name="Bounds",
description="Clickable's bounds (NOTE: only used if your clickable is not a collider)",
default="hull"
)

input_sockets: dict[str, Any] = {
"region": {
Expand Down Expand Up @@ -162,10 +165,12 @@ class PlasmaClickableRegionNode(idprops.IDPropObjectMixin, PlasmaNodeBase, bpy.t
description="Object that defines the region mesh",
type=bpy.types.Object,
poll=idprops.poll_mesh_objects)
bounds = EnumProperty(name="Bounds",
description="Physical object's bounds (NOTE: only used if your clickable is not a collider)",
items=bounds_types,
default="hull")
bounds = bounds_helpers.enum_property(
"bounds", store_on_collider=False,
name="Bounds",
description="Physical object's bounds (NOTE: only used if your clickable is not a collider)",
default="hull"
)

output_sockets = {
"satisfies": {
Expand Down Expand Up @@ -419,9 +424,11 @@ def _update_report_on(self, context):
description="Object that defines the region mesh",
type=bpy.types.Object,
poll=idprops.poll_mesh_objects)
bounds = EnumProperty(name="Bounds",
description="Physical object's bounds",
items=bounds_types)
bounds = bounds_helpers.enum_property(
"bounds", store_on_collider=False,
name="Bounds",
description="Physical object's bounds"
)

# Detector Properties
report_on = EnumProperty(name="Triggerers",
Expand Down
26 changes: 10 additions & 16 deletions korman/nodes/node_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from typing import *
from PyHSPlasma import *

from .. import bounds_helpers
from .node_core import *
from ..properties.modifiers.physics import bounds_types, bounds_type_index, bounds_type_str
from .. import idprops

class PlasmaExcludeRegionNode(idprops.IDPropObjectMixin, PlasmaNodeBase, bpy.types.Node):
Expand All @@ -33,25 +33,19 @@ class PlasmaExcludeRegionNode(idprops.IDPropObjectMixin, PlasmaNodeBase, bpy.typ
# ohey, this can be a Python attribute
pl_attrib = {"ptAttribExcludeRegion"}

def _get_bounds(self):
if self.region_object is not None:
return bounds_type_index(self.region_object.plasma_modifiers.collision.bounds)
return bounds_type_index("hull")
def _set_bounds(self, value):
if self.region_object is not None:
self.region_object.plasma_modifiers.collision.bounds = bounds_type_str(value)

region_object = PointerProperty(name="Region",
description="Region object's name",
type=bpy.types.Object,
poll=idprops.poll_mesh_objects)
bounds = EnumProperty(name="Bounds",
description="Region bounds",
items=bounds_types,
get=_get_bounds,
set=_set_bounds)
block_cameras = BoolProperty(name="Block Cameras",
description="The region blocks cameras when it has been cleared")
bounds = bounds_helpers.enum_property(
"bounds",
name="Bounds",
description="Region bounds"
)
block_cameras = BoolProperty(
name="Block Cameras",
description="The region blocks cameras when it has been cleared"
)

input_sockets:dict[str, dict[str, Any]] = {
"safe_point": {
Expand Down
18 changes: 2 additions & 16 deletions korman/properties/modifiers/physics.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,10 @@
from PyHSPlasma import *

from .base import PlasmaModifierProperties
from ...bounds_helpers import _bounds_types
from ...exporter import ExportError
from ... import idprops

# These are the kinds of physical bounds Plasma can work with.
# This sequence is acceptable in any EnumProperty
bounds_types = (
("box", "Bounding Box", "Use a perfect bounding box"),
("sphere", "Bounding Sphere", "Use a perfect bounding sphere"),
("hull", "Convex Hull", "Use a convex set encompasing all vertices"),
("trimesh", "Triangle Mesh", "Use the exact triangle mesh (SLOW!)")
)

# These are the collision sound surface types
surface_types = (
# Danger: do not reorder this one.
Expand All @@ -55,12 +47,6 @@
("subworld", "Separate World", "Causes all objects to be placed in a separate physics simulation"),
)

def bounds_type_index(key):
return list(zip(*bounds_types))[0].index(key)

def bounds_type_str(idx):
return bounds_types[idx][0]

def _set_phys_prop(prop, sim, phys, value=True):
"""Sets properties on plGenericPhysical and plSimulationInterface (seeing as how they are duped)"""
sim.setProperty(prop, value)
Expand All @@ -78,7 +64,7 @@ class PlasmaCollider(PlasmaModifierProperties):

bounds = EnumProperty(name="Bounds Type",
description="",
items=bounds_types,
items=_bounds_types,
default="hull")

avatar_blocker = BoolProperty(name="Blocks Avatars",
Expand Down
22 changes: 13 additions & 9 deletions korman/properties/modifiers/region.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
from ...helpers import bmesh_from_object
from ... import idprops

from ... import bounds_helpers
from .base import PlasmaModifierProperties, PlasmaModifierLogicWiz
from ..prop_camera import PlasmaCameraProperties
from .physics import bounds_types

footstep_surface_ids = {
"dirt": 0,
Expand Down Expand Up @@ -136,14 +136,18 @@ class PlasmaFootstepRegion(PlasmaModifierProperties, PlasmaModifierLogicWiz):
bl_description = "Footstep Region"
bl_object_types = {"MESH"}

surface = EnumProperty(name="Surface",
description="What kind of surface are we walking on?",
items=footstep_surfaces,
default="stone")
bounds = EnumProperty(name="Region Bounds",
description="Physical object's bounds",
items=bounds_types,
default="hull")
surface = EnumProperty(
name="Surface",
description="What kind of surface are we walking on?",
items=footstep_surfaces,
default="stone"
)
bounds = bounds_helpers.enum_property(
"bounds", store_on_collider=False,
name="Region Bounds",
description="Physical object's bounds",
default="hull"
)

def logicwiz(self, bo, tree):
nodes = tree.nodes
Expand Down

0 comments on commit a833fa8

Please sign in to comment.