Jelajahi Sumber

Add Invert Matrix Node

I don't think I want to support safe-invert matrix. better to throw an error and
make the user fix the matrix
also I have never once encountered a non-invertable matrix in my rigging scripts
so I don't actually expect the error to occur all that often
Joseph Brandenburg 6 bulan lalu
induk
melakukan
1a27015284
4 mengubah file dengan 44 tambahan dan 4 penghapusan
  1. 1 0
      __init__.py
  2. 29 1
      misc_nodes.py
  3. 14 0
      misc_nodes_ui.py
  4. 0 3
      utilities.py

+ 1 - 0
__init__.py

@@ -148,6 +148,7 @@ matrix_category = [
         NodeItem("UtilityMatrixFromXForm"),
         NodeItem("UtilityAxesFromMatrix"),
         NodeItem("UtilityMatrixTransform"),
+        NodeItem("UtilityMatrixInvert"),
         NodeItem("UtilityTransformationMatrix"),
         NodeItem("UtilitySetBoneMatrixTail"),
     ]

+ 29 - 1
misc_nodes.py

@@ -1,5 +1,6 @@
 from .node_container_common import *
 from .base_definitions import MantisNode, NodeSocket
+from .base_definitions import MantisSocketTemplate as SockTemplate
 from .xForm_containers import xFormArmature, xFormBone
 from .misc_nodes_socket_templates import *
 from math import pi, tau
@@ -49,6 +50,7 @@ def TellClasses():
              UtilityAxesFromMatrix,
              UtilityBoneMatrixHeadTailFlip,
              UtilityMatrixTransform,
+             UtilityMatrixInvert,
              UtilityTransformationMatrix,
              UtilityIntToString,
              UtilityArrayGet,
@@ -1254,7 +1256,6 @@ class UtilityGeometryOfXForm(MantisNode):
         prOrange(f"WARN: Cannot retrieve data from {self}, the connected xForm is not a mesh or curve.")
         return None
 
-
 class UtilityNameOfXForm(MantisNode):
     '''A node representing existing object data'''
     def __init__(self, signature, base_tree):
@@ -1533,7 +1534,34 @@ class UtilityMatrixTransform(MantisNode):
         self.prepared = True
         self.executed = True
 
+MatrixInvertSockets=[
+    Matrix1Template := SockTemplate(
+    name="Matrix 1", is_input=True,  bl_idname='MatrixSocket', ),
+    MatrixOutTemplate := SockTemplate(
+    name="Matrix", is_input=False,  bl_idname='MatrixSocket', ),
+]
 
+class UtilityMatrixInvert(MantisNode):
+    def __init__(self, signature, base_tree):
+        super().__init__(signature, base_tree, MatrixInvertSockets)
+        self.init_parameters()
+        self.node_type = "UTILITY"
+
+    def bPrepare(self, bContext = None,):
+        from mathutils import Vector
+        mat1 = self.evaluate_input("Matrix 1")
+        if mat1:
+            mat1copy = mat1.copy()
+            try:
+                self.parameters["Matrix"] = mat1copy.inverted()
+            except ValueError as e:
+                prRed(f"ERROR: {self}: The matrix cannot be inverted.")
+                prOrange(mat1)
+                raise e
+        else:
+            raise RuntimeError(wrapRed(f"Node {self} did not receive all matrix inputs... found input 1? {mat1 is not None}"))
+        self.prepared = True
+        self.executed = True
 
 class UtilityTransformationMatrix(MantisNode):
     def __init__(self, signature, base_tree):

+ 14 - 0
misc_nodes_ui.py

@@ -55,6 +55,7 @@ def TellClasses():
              UtilityAxesFromMatrix,
              UtilityBoneMatrixHeadTailFlip,
              UtilityMatrixTransform,
+             UtilityMatrixInvert,
              UtilityTransformationMatrix,
              UtilitySetBoneMatrixTail,
 
@@ -879,6 +880,19 @@ class UtilityMatrixTransform(Node, MantisUINode):
         self.outputs.new("MatrixSocket", "Out Matrix")
         self.initialized = True
 
+from .misc_nodes import MatrixInvertSockets
+class UtilityMatrixInvert(Node, MantisUINode):
+    """Inverts an invertable matrix, otherwise throws an error."""
+    bl_idname = "UtilityMatrixInvert"
+    bl_label = "Invert Matrix"
+    bl_icon = "NODE"
+    initialized : bpy.props.BoolProperty(default = False)
+    mantis_node_class_name=bl_idname
+    
+    def init(self, context):
+        self.init_sockets(MatrixInvertSockets)
+        self.initialized = True
+
 class UtilityMatrixSetLocation(Node, MantisUINode):
     """Sets a matrix's location."""
     bl_idname = "UtilityMatrixSetLocation"

+ 0 - 3
utilities.py

@@ -330,9 +330,6 @@ def init_schema_dependencies(schema, all_nc):
                 continue
             schema_dependency_handle_item(schema, all_nc, item,)
 
-        
-
-
 def check_and_add_root(n, roots, include_non_hierarchy=False):
     if (include_non_hierarchy * len(n.dependencies)) > 0:
         return