|
| 1 | +# This file is part of Korman. |
| 2 | +# |
| 3 | +# Korman is free software: you can redistribute it and/or modify |
| 4 | +# it under the terms of the GNU General Public License as published by |
| 5 | +# the Free Software Foundation, either version 3 of the License, or |
| 6 | +# (at your option) any later version. |
| 7 | +# |
| 8 | +# Korman is distributed in the hope that it will be useful, |
| 9 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +# GNU General Public License for more details. |
| 12 | +# |
| 13 | +# You should have received a copy of the GNU General Public License |
| 14 | +# along with Korman. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +from bpy.props import * |
| 19 | + |
| 20 | +from typing import * |
| 21 | +import warnings |
| 22 | + |
| 23 | +# Workaround for Blender memory management limitation, |
| 24 | +# don't change this to a literal in the code! |
| 25 | +_ENTIRE_ANIMATION = "(Entire Animation)" |
| 26 | + |
| 27 | +def _get_object_animation_names(self, object_attr: str) -> Sequence[Tuple[str, str, str]]: |
| 28 | + target_object = getattr(self, object_attr) |
| 29 | + if target_object is not None: |
| 30 | + items = [(anim.animation_name, anim.animation_name, "") |
| 31 | + for anim in target_object.plasma_modifiers.animation.subanimations] |
| 32 | + else: |
| 33 | + items = [(_ENTIRE_ANIMATION, _ENTIRE_ANIMATION, "")] |
| 34 | + |
| 35 | + # We always want "(Entire Animation)", if it exists, to be the first item. |
| 36 | + entire = items.index((_ENTIRE_ANIMATION, _ENTIRE_ANIMATION, "")) |
| 37 | + if entire not in (-1, 0): |
| 38 | + items.pop(entire) |
| 39 | + items.insert(0, (_ENTIRE_ANIMATION, _ENTIRE_ANIMATION, "")) |
| 40 | + |
| 41 | + return items |
| 42 | + |
| 43 | +def animation(object_attr: str, **kwargs) -> str: |
| 44 | + def get_items(self, context): |
| 45 | + return _get_object_animation_names(self, object_attr) |
| 46 | + |
| 47 | + return EnumProperty(items=get_items, **kwargs) |
| 48 | + |
| 49 | +# These are the kinds of physical bounds Plasma can work with. |
| 50 | +# This sequence is acceptable in any EnumProperty |
| 51 | +_bounds_types = ( |
| 52 | + ("box", "Bounding Box", "Use a perfect bounding box"), |
| 53 | + ("sphere", "Bounding Sphere", "Use a perfect bounding sphere"), |
| 54 | + ("hull", "Convex Hull", "Use a convex set encompasing all vertices"), |
| 55 | + ("trimesh", "Triangle Mesh", "Use the exact triangle mesh (SLOW!)") |
| 56 | +) |
| 57 | + |
| 58 | +def _bounds_type_index(key: str) -> int: |
| 59 | + return list(zip(*_bounds_types))[0].index(key) |
| 60 | + |
| 61 | +def _bounds_type_str(idx: int) -> str: |
| 62 | + return _bounds_types[idx][0] |
| 63 | + |
| 64 | +def _get_bounds(physics_attr: Optional[str]) -> Callable[[Any], int]: |
| 65 | + def getter(self) -> int: |
| 66 | + physics_object = getattr(self, physics_attr) if physics_attr is not None else self.id_data |
| 67 | + if physics_object is not None: |
| 68 | + return _bounds_type_index(physics_object.plasma_modifiers.collision.bounds) |
| 69 | + return _bounds_type_index("hull") |
| 70 | + return getter |
| 71 | + |
| 72 | +def _set_bounds(physics_attr: Optional[str]) -> Callable[[Any, int], None]: |
| 73 | + def setter(self, value: int): |
| 74 | + physics_object = getattr(self, physics_attr) if physics_attr is not None else self.id_data |
| 75 | + if physics_object is not None: |
| 76 | + physics_object.plasma_modifiers.collision.bounds = _bounds_type_str(value) |
| 77 | + return setter |
| 78 | + |
| 79 | +def bounds(physics_attr: Optional[str] = None, store_on_collider: bool = True, **kwargs) -> str: |
| 80 | + assert not {"items", "get", "set"} & kwargs.keys(), "You cannot use the `items`, `get`, or `set` keyword arguments" |
| 81 | + if store_on_collider: |
| 82 | + kwargs["get"] = _get_bounds(physics_attr) |
| 83 | + kwargs["set"] = _set_bounds(physics_attr) |
| 84 | + else: |
| 85 | + warnings.warn("Storing bounds properties outside of the collision modifier is deprecated.", category=DeprecationWarning) |
| 86 | + if "default" not in kwargs: |
| 87 | + kwargs["default"] = "hull" |
| 88 | + return EnumProperty( |
| 89 | + items=_bounds_types, |
| 90 | + **kwargs |
| 91 | + ) |
| 92 | + |
| 93 | +def upgrade_bounds(bl, bounds_attr: str) -> None: |
| 94 | + # Only perform this process if the property has a value. Otherwise, we'll |
| 95 | + # wind up blowing away the collision modifier's settings with nonsense. |
| 96 | + if not bl.is_property_set(bounds_attr): |
| 97 | + return |
| 98 | + |
| 99 | + # Before we unregister anything, grab a copy of what the collision modifier currently thinks. |
| 100 | + bounds_value_curr = getattr(bl, bounds_attr) |
| 101 | + |
| 102 | + # So, here's the deal. If someone has been playing with nodes and changed the bounds type, |
| 103 | + # Blender will think the property has been set, even if they wound up with the property |
| 104 | + # at the default value. I don't know that we can really trust the default in the property |
| 105 | + # definition to be the same as the old default (they shouldn't be different, but let's be safe). |
| 106 | + # So, let's apply rough justice. If the destination property thinks it's a triangle mesh, we |
| 107 | + # don't need to blow that away - it's a very specific non default setting. |
| 108 | + if bounds_value_curr == "trimesh": |
| 109 | + return |
| 110 | + |
| 111 | + # Unregister the new/correct proxy bounds property (with getter/setter) and re-register |
| 112 | + # the property without the proxy functions to get the old value. Reregister the new property |
| 113 | + # again and set it. |
| 114 | + cls = bl.__class__ |
| 115 | + prop_func, prop_def = getattr(cls, bounds_attr) |
| 116 | + RemoveProperty(cls, attr=bounds_attr) |
| 117 | + del prop_def["attr"] |
| 118 | + |
| 119 | + # Remove the things we don't want in a copy to prevent hosing the new property. |
| 120 | + old_prop_def = dict(prop_def) |
| 121 | + del old_prop_def["get"] |
| 122 | + del old_prop_def["set"] |
| 123 | + setattr(cls, bounds_attr, prop_func(**old_prop_def)) |
| 124 | + bounds_value_new = getattr(bl, bounds_attr) |
| 125 | + |
| 126 | + # Re-register new property. |
| 127 | + RemoveProperty(cls, attr=bounds_attr) |
| 128 | + setattr(cls, bounds_attr, prop_func(**prop_def)) |
| 129 | + |
| 130 | + # Only set the property if the value different to avoid thrashing and log spam. |
| 131 | + if bounds_value_curr != bounds_value_new: |
| 132 | + print(f"Stashing bounds property: [{bl.name}] ({cls.__name__}) {bounds_value_curr} -> {bounds_value_new}") # TEMP |
| 133 | + setattr(bl, bounds_attr, bounds_value_new) |
| 134 | + |
| 135 | +def _get_texture_animation_names(self, object_attr: str, material_attr: str, texture_attr: str) -> Sequence[Tuple[str, str, str]]: |
| 136 | + target_object = getattr(self, object_attr) |
| 137 | + material = getattr(self, material_attr) |
| 138 | + texture = getattr(self, texture_attr) |
| 139 | + |
| 140 | + if texture is not None: |
| 141 | + items = [(anim.animation_name, anim.animation_name, "") |
| 142 | + for anim in texture.plasma_layer.subanimations] |
| 143 | + elif material is not None or target_object is not None: |
| 144 | + if material is None: |
| 145 | + materials = (i.material for i in target_object.material_slots if i and i.material) |
| 146 | + else: |
| 147 | + materials = (material,) |
| 148 | + layer_props = (i.texture.plasma_layer for mat in materials for i in mat.texture_slots if i and i.texture) |
| 149 | + all_anims = frozenset((anim.animation_name for i in layer_props for anim in i.subanimations)) |
| 150 | + items = [(i, i, "") for i in all_anims] |
| 151 | + else: |
| 152 | + items = [(_ENTIRE_ANIMATION, _ENTIRE_ANIMATION, "")] |
| 153 | + |
| 154 | + # We always want "(Entire Animation)", if it exists, to be the first item. |
| 155 | + entire = items.index((_ENTIRE_ANIMATION, _ENTIRE_ANIMATION, "")) |
| 156 | + if entire not in (-1, 0): |
| 157 | + items.pop(entire) |
| 158 | + items.insert(0, (_ENTIRE_ANIMATION, _ENTIRE_ANIMATION, "")) |
| 159 | + |
| 160 | + return items |
| 161 | + |
| 162 | +def triprop_animation(object_attr: str, material_attr: str, texture_attr: str, **kwargs) -> str: |
| 163 | + def get_items(self, context): |
| 164 | + return _get_texture_animation_names(self, object_attr, material_attr, texture_attr) |
| 165 | + |
| 166 | + assert not {"items", "get", "set"} & kwargs.keys(), "You cannot use the `items`, `get`, or `set` keyword arguments" |
| 167 | + return EnumProperty(items=get_items, **kwargs) |
0 commit comments