primitives_nodes.py 3.2 KB

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