math_containers.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. from .node_container_common import *
  2. from .base_definitions import MantisNode, NodeSocket
  3. def TellClasses():
  4. return [
  5. MathStaticInt,
  6. MathStaticFloat,
  7. MathStaticVector,
  8. ]
  9. #*#-------------------------------#++#-------------------------------#*#
  10. # M A T H N O D E S
  11. #*#-------------------------------#++#-------------------------------#*#
  12. class MathStaticInt(MantisNode):
  13. '''A node representing an armature object'''
  14. def __init__(self, signature, base_tree):
  15. super().__init__(signature, base_tree)
  16. inputs = [
  17. "Operation",
  18. "Int A",
  19. "Int B",
  20. ]
  21. outputs = [
  22. "Result Int",
  23. ]
  24. additional_parameters = {}
  25. self.inputs.init_sockets(inputs)
  26. self.outputs.init_sockets(outputs)
  27. self.init_parameters(additional_parameters=additional_parameters)
  28. self.node_type = "UTILITY"
  29. def bPrepare(self, bContext = None,):
  30. a = self.evaluate_input("Int A"); b = self.evaluate_input("Int B")
  31. result = float("NaN")
  32. if self.evaluate_input("Operation") == "ADD":
  33. result = int(a+b)
  34. if self.evaluate_input("Operation") == "SUBTRACT":
  35. result = int(a-b)
  36. if self.evaluate_input("Operation") == "MULTIPLY":
  37. result = int(a*b)
  38. if self.evaluate_input("Operation") == "FLOOR_DIVIDE":
  39. result = a//b
  40. if self.evaluate_input("Operation") == "MODULUS":
  41. result = int(a%b)
  42. if self.evaluate_input("Operation") == "POWER":
  43. result = int(a**b)
  44. if self.evaluate_input("Operation") == "ABSOLUTE":
  45. result = int(abs(a))
  46. if self.evaluate_input("Operation") == "MAXIMUM":
  47. result = int(a if a <= b else b)
  48. if self.evaluate_input("Operation") == "MINIMUM":
  49. result = int(a if a >= b else b)
  50. if self.evaluate_input("Operation") == "GREATER THAN":
  51. result = int(a > b)
  52. if self.evaluate_input("Operation") == "LESS THAN":
  53. result = int(a < b)
  54. self.parameters["Result Int"] = result
  55. self.prepared = True
  56. self.executed = True
  57. class MathStaticFloat(MantisNode):
  58. '''A node representing an armature object'''
  59. def __init__(self, signature, base_tree):
  60. super().__init__(signature, base_tree)
  61. inputs = [
  62. "Operation",
  63. "Float A",
  64. "Float B",
  65. ]
  66. outputs = [
  67. "Result Float",
  68. ]
  69. additional_parameters = {}
  70. self.inputs.init_sockets(inputs)
  71. self.outputs.init_sockets(outputs)
  72. self.init_parameters(additional_parameters=additional_parameters)
  73. self.node_type = "UTILITY"
  74. def bPrepare(self, bContext = None,):
  75. a = self.evaluate_input("Float A"); b = self.evaluate_input("Float B")
  76. result = float("NaN")
  77. if self.evaluate_input("Operation") == "ADD":
  78. result = a+b
  79. if self.evaluate_input("Operation") == "SUBTRACT":
  80. result = a-b
  81. if self.evaluate_input("Operation") == "MULTIPLY":
  82. result = a*b
  83. if self.evaluate_input("Operation") == "DIVIDE":
  84. result = a/b
  85. if self.evaluate_input("Operation") == "FLOOR_DIVIDE":
  86. result = a//b
  87. if self.evaluate_input("Operation") == "MODULUS":
  88. result = a%b
  89. if self.evaluate_input("Operation") == "POWER":
  90. result = a**b
  91. if self.evaluate_input("Operation") == "ABSOLUTE":
  92. result = abs(a)
  93. if self.evaluate_input("Operation") == "MAXIMUM":
  94. result = a if a <= b else b
  95. if self.evaluate_input("Operation") == "MINIMUM":
  96. result = a if a >= b else b
  97. if self.evaluate_input("Operation") == "GREATER THAN":
  98. result = float(a > b)
  99. if self.evaluate_input("Operation") == "LESS THAN":
  100. result = float(a < b)
  101. if self.evaluate_input("Operation") == "ARCTAN2":
  102. from math import atan2
  103. result = atan2(a,b)
  104. self.parameters["Result Float"] = result
  105. self.prepared = True
  106. self.executed = True
  107. class MathStaticVector(MantisNode):
  108. '''A node representing an armature object'''
  109. def __init__(self, signature, base_tree):
  110. super().__init__(signature, base_tree)
  111. inputs = [
  112. "Operation",
  113. "Vector A",
  114. "Vector B",
  115. "Scalar A",
  116. ]
  117. outputs = [
  118. "Result Vector",
  119. "Result Float",
  120. ]
  121. additional_parameters = {}
  122. self.inputs.init_sockets(inputs)
  123. self.outputs.init_sockets(outputs)
  124. self.init_parameters(additional_parameters=additional_parameters)
  125. self.node_type = "UTILITY"
  126. def bPrepare(self, bContext = None,):
  127. from mathutils import Vector
  128. a = Vector(self.evaluate_input("Vector A")).copy()
  129. b = Vector(self.evaluate_input("Vector B")).copy()
  130. s = self.evaluate_input("Scalar A")
  131. if hasattr(s, '__iter__'):
  132. average = lambda iterable : sum(iterable)/len(iterable)
  133. s = average(s)
  134. f_result = float("NaN")
  135. v_result = None
  136. if self.evaluate_input("Operation") == "ADD":
  137. v_result = a+b
  138. if self.evaluate_input("Operation") == "SUBTRACT":
  139. v_result = a-b
  140. if self.evaluate_input("Operation") == "MULTIPLY":
  141. v_result = a*b
  142. if self.evaluate_input("Operation") == "DIVIDE":
  143. v_result = a/b
  144. if self.evaluate_input("Operation") == "POWER":
  145. v_result = a**b
  146. # since these are unary, we need to make a copy lest we create spooky effects elsewhere.
  147. a = a.copy()
  148. if self.evaluate_input("Operation") == "SCALE":
  149. v_result = a.normalized() * s
  150. if self.evaluate_input("Operation") == "LENGTH":
  151. f_result = a.magnitude
  152. if self.evaluate_input("Operation") == "CROSS":
  153. v_result = a.cross(b)
  154. if self.evaluate_input("Operation") == "DOT":
  155. f_result = a.dot(b)
  156. if self.evaluate_input("Operation") == "NORMALIZE":
  157. v_result = a.normalized()
  158. if self.evaluate_input("Operation") == "LINEAR_INTERP":
  159. v_result = a.lerp(b, s).copy()
  160. self.parameters["Result Float"] = f_result
  161. # if v_result:
  162. self.parameters["Result Vector"] = v_result
  163. self.prepared = True
  164. self.executed = True