primitives_nodes.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from .utilities.node_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. # mode is unused but needed for interface consistency
  37. def bGetObject(self, mode=''):
  38. from bpy import data
  39. # first try Curve, then try Mesh
  40. bObject = data.curves.get(self.evaluate_input("Name"))
  41. if not bObject:
  42. bObject = data.meshes.get(self.evaluate_input("Name"))
  43. return bObject
  44. def bTransformPass(self, bContext = None,):
  45. # Get the datablock
  46. data = self.bGetObject()
  47. import bpy
  48. if not data:
  49. data = bpy.data.meshes.new( self.evaluate_input("Name") )
  50. # make the circle
  51. import bmesh; bm = bmesh.new()
  52. bmesh.ops.create_circle( # lazy but easy
  53. bm,
  54. cap_ends=False,
  55. radius=max(self.evaluate_input("Radius"), 0.0001),
  56. segments=min( max( self.evaluate_input("Number of Points"), 3), 1024),
  57. )
  58. # this is rotated 90 degrees, we need Y-up instead of Z-up
  59. from mathutils import Matrix
  60. from math import pi
  61. for v in bm.verts:
  62. v.co = Matrix.Rotation(pi/2, 4, 'X') @ v.co
  63. # done with this, push it to the data and free the bmesh.
  64. bm.to_mesh(data); bm.free()
  65. self.executed = True
  66. from .primitives_sockets import LatticeSockets
  67. class GeometryLattice(PrimitiveNode):
  68. '''A node representing a Circle Primitive mesh'''
  69. def __init__(self, signature, base_tree):
  70. super().__init__(signature, base_tree, LatticeSockets)
  71. self.init_parameters(additional_parameters= {})
  72. self.prepared = False
  73. def reset_execution(self):
  74. super().reset_execution()
  75. self.prepared=False
  76. self.executed=False
  77. # mode is unused but needed for interface consistency
  78. def bGetObject(self, mode=''):
  79. from bpy import data
  80. bObject = data.lattices.get(self.evaluate_input("Name"))
  81. return bObject
  82. def bPrepare(self, bContext = None,):
  83. # Get the datablock
  84. data = self.bGetObject()
  85. import bpy
  86. if not data:
  87. data = bpy.data.lattices.new( self.evaluate_input("Name") )
  88. props_sockets = self.gen_property_socket_map()
  89. evaluate_sockets(self, data, props_sockets)
  90. self.prepared = True; self.executed = True