primitives_nodes.py 3.3 KB

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