From bf49fb1284cdeeee36eb1006c4a395c62d4ed0ea Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Mon, 16 Aug 2021 06:13:43 -0400 Subject: [PATCH] Add Python attribute node creation helper. --- korman/nodes/node_python.py | 8 ++++++++ korman/properties/modifiers/base.py | 24 +++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/korman/nodes/node_python.py b/korman/nodes/node_python.py index 817d651..b924ae9 100644 --- a/korman/nodes/node_python.py +++ b/korman/nodes/node_python.py @@ -138,6 +138,14 @@ class PlasmaAttributeArguments(bpy.types.PropertyGroup): class PlasmaAttribute(bpy.types.PropertyGroup): + # This is thy lookup helper + type_LUT = { + bool: "ptAttribBoolean", + float: "ptAttribFloat", + int: "ptAttribInt", + str: "ptAttribString", + } + attribute_id = IntProperty() attribute_type = StringProperty() attribute_name = StringProperty() diff --git a/korman/properties/modifiers/base.py b/korman/properties/modifiers/base.py index 388e4e8..247d2df 100644 --- a/korman/properties/modifiers/base.py +++ b/korman/properties/modifiers/base.py @@ -17,7 +17,7 @@ import bpy from bpy.props import * import abc -from typing import Any, Dict, Generator +from typing import Any, Dict, Generator, Optional class PlasmaModifierProperties(bpy.types.PropertyGroup): @property @@ -140,6 +140,28 @@ class PlasmaModifierLogicWiz: pfm_node.update() return pfm_node + def _create_python_attribute(self, pfm_node, attribute_name: str, attribute_type: Optional[str] = None, **kwargs): + """Creates and links a Python Attribute Node to the Python File Node given by `pfm_node`. + This will automatically handle simple attribute types such as numbers and strings, however, + for object linkage, you should specify the optional `attribute_type` to ensure the proper + attribute type is found. For attribute nodes that require multiple values, the `value` may + be set to None and handled in your code.""" + from ...nodes.node_python import PlasmaAttribute, PlasmaAttribNodeBase + if attribute_type is None: + assert len(kwargs) == 1 and "value" in kwargs, \ + "In order to deduce the attribute_type, exactly one attribute value must be passed as a kw named `value`" + attribute_type = PlasmaAttribute.type_LUT.get(kwargs["value"].__class__) + node_cls = next((i for i in PlasmaAttribNodeBase.__subclasses__() if attribute_type in i.pl_attrib), None) + assert node_cls is not None, "'{}': Unable to find attribute node type for '{}' ('{}')".format( + self.id_data.name, attribute_name, attribute_type + ) + + node = pfm_node.id_data.nodes.new(node_cls.bl_idname) + node.link_output(pfm_node, "pfm", attribute_name) + for i, j in kwargs.items(): + setattr(node, i, j) + return node + @abc.abstractmethod def logicwiz(self, bo, tree): pass