primitives_nodes.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from .utilities.node_common import *
  2. from .base_definitions import MantisNode, NodeSocket
  3. from .primitives_sockets import *
  4. def TellClasses():
  5. return [
  6. # Primitives
  7. CirclePrimitive,
  8. GeometryLattice,
  9. ]
  10. #*#-------------------------------#++#-------------------------------#*#
  11. # P R I M I T I V E S
  12. #*#-------------------------------#++#-------------------------------#*#
  13. class PrimitiveNode(MantisNode):
  14. __slots__ = ()
  15. node_type = "UTILITY"
  16. def __init__(self, signature, base_tree, socket_templates=[],
  17. additional_parameters = {}):
  18. super().__init__(signature, base_tree, socket_templates,
  19. additional_parameters)
  20. self.prepared = True
  21. def reset_execution(self):
  22. super().reset_execution()
  23. self.prepared=True
  24. class CirclePrimitive(PrimitiveNode):
  25. '''A node representing a Circle Primitive mesh'''
  26. __slots__ = ()
  27. socket_templates = CirclePrimitiveSockets
  28. passes=frozenset({'bPrepare',})
  29. # mode is unused but needed for interface consistency
  30. def bGetObject(self, mode=''):
  31. from bpy import data
  32. # first try Curve, then try Mesh
  33. bObject = data.curves.get(self.evaluate_input("Name"))
  34. if not bObject:
  35. bObject = data.meshes.get(self.evaluate_input("Name"))
  36. return bObject
  37. def bPrepare(self, bContext = None,):
  38. # Get the datablock
  39. data = self.bGetObject()
  40. import bpy
  41. if not data:
  42. data = bpy.data.meshes.new( self.evaluate_input("Name") )
  43. # make the circle
  44. import bmesh; bm = bmesh.new()
  45. bmesh.ops.create_circle( # lazy but easy
  46. bm,
  47. cap_ends=False,
  48. radius=max(self.evaluate_input("Radius"), 0.0001),
  49. segments=min( max( self.evaluate_input("Number of Points"), 3), 1024),
  50. )
  51. # this is rotated 90 degrees, we need Y-up instead of Z-up
  52. from mathutils import Matrix
  53. from math import pi
  54. for v in bm.verts:
  55. v.co = Matrix.Rotation(pi/2, 4, 'X') @ v.co
  56. # done with this, push it to the data and free the bmesh.
  57. bm.to_mesh(data); bm.free()
  58. self.executed = True
  59. from .primitives_sockets import LatticeSockets
  60. class GeometryLattice(PrimitiveNode):
  61. '''A node representing a Circle Primitive mesh'''
  62. __slots__ = ()
  63. socket_templates = LatticeSockets
  64. def __init__(self, signature, base_tree):
  65. super().__init__(signature, base_tree, self.socket_templates)
  66. self.prepared = False
  67. def reset_execution(self):
  68. super().reset_execution()
  69. self.prepared=False
  70. self.executed=False
  71. # mode is unused but needed for interface consistency
  72. def bGetObject(self, mode=''):
  73. from bpy import data
  74. bObject = data.lattices.get(self.evaluate_input("Name"))
  75. return bObject
  76. def bPrepare(self, bContext = None,):
  77. # Get the datablock
  78. data = self.bGetObject()
  79. import bpy
  80. if not data:
  81. data = bpy.data.lattices.new( self.evaluate_input("Name") )
  82. props_sockets = self.property_socket_map
  83. evaluate_sockets(self, {"DEFAULT":data}, props_sockets)
  84. self.prepared = True; self.executed = True