primitives_containers.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from mantis.node_container_common import *
  2. from bpy.types import Node
  3. from .base_definitions import MantisNode
  4. def TellClasses():
  5. return [
  6. # Primitives
  7. CirclePrimitive,
  8. ]
  9. #*#-------------------------------#++#-------------------------------#*#
  10. # P R I M I T I V E S
  11. #*#-------------------------------#++#-------------------------------#*#
  12. class CirclePrimitive:
  13. '''A node representing float input'''
  14. def __init__(self, signature, base_tree):
  15. self.base_tree=base_tree
  16. self.executed = False
  17. self.signature = signature
  18. self.inputs = {
  19. "Name" : NodeSocket(is_input = True, name = "Name", node = self),
  20. "Radius" : NodeSocket(is_input = True, name = "Radius", node = self),
  21. "Number of Points" : NodeSocket(is_input = True, name = "Number of Points", node = self),
  22. }
  23. self.outputs = {
  24. "Circle" : NodeSocket(is_input = False, name = "Circle", node=self),
  25. }
  26. self.parameters = {
  27. "Name":None,
  28. "Radius":None,
  29. "Number of Points":None,
  30. "Circle":None,
  31. }
  32. self.node_type = "UTILITY"
  33. def evaluate_input(self, input_name):
  34. return evaluate_input(self, input_name)
  35. def bGetObject(self):
  36. from bpy import data
  37. # first try Curve, then try Mesh
  38. bObject = data.curves.get(self.evaluate_input("Name"))
  39. if not bObject:
  40. bObject = data.meshes.get(self.evaluate_input("Name"))
  41. return bObject
  42. def bExecute(self, bContext = None,):
  43. # Get the datablock
  44. data = self.bGetObject()
  45. import bpy
  46. if not data:
  47. data = bpy.data.meshes.new( self.evaluate_input("Name") )
  48. # make the circle
  49. import bmesh; bm = bmesh.new()
  50. bmesh.ops.create_circle( # lazy but easy
  51. bm,
  52. cap_ends=False,
  53. radius=max(self.evaluate_input("Radius"), 0.0001),
  54. segments=min( max( self.evaluate_input("Number of Points"), 3), 1024),
  55. )
  56. # this is rotated 90 degrees, we need Y-up instead of Z-up
  57. from mathutils import Matrix
  58. from math import pi
  59. for v in bm.verts:
  60. v.co = Matrix.Rotation(pi/2, 4, 'X') @ v.co
  61. # done with this, push it to the data and free the bmesh.
  62. bm.to_mesh(data); bm.free()
  63. def __repr__(self):
  64. return self.signature.__repr__()
  65. def fill_parameters(self):
  66. fill_parameters(self)