primitives_containers.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from .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.signature = signature
  17. self.inputs = {
  18. "Name" : NodeSocket(is_input = True, name = "Name", node = self),
  19. "Radius" : NodeSocket(is_input = True, name = "Radius", node = self),
  20. "Number of Points" : NodeSocket(is_input = True, name = "Number of Points", node = self),
  21. }
  22. self.outputs = {
  23. "Circle" : NodeSocket(is_input = False, name = "Circle", node=self),
  24. }
  25. self.parameters = {
  26. "Name":None,
  27. "Radius":None,
  28. "Number of Points":None,
  29. "Circle":None,
  30. }
  31. self.node_type = "UTILITY"
  32. self.hierarchy_connections = []
  33. self.connections = []
  34. self.hierarchy_dependencies = []
  35. self.dependencies = []
  36. self.prepared = True
  37. self.executed = False
  38. def bGetObject(self):
  39. from bpy import data
  40. # first try Curve, then try Mesh
  41. bObject = data.curves.get(self.evaluate_input("Name"))
  42. if not bObject:
  43. bObject = data.meshes.get(self.evaluate_input("Name"))
  44. return bObject
  45. def bExecute(self, bContext = None,):
  46. # Get the datablock
  47. data = self.bGetObject()
  48. import bpy
  49. if not data:
  50. data = bpy.data.meshes.new( self.evaluate_input("Name") )
  51. # make the circle
  52. import bmesh; bm = bmesh.new()
  53. bmesh.ops.create_circle( # lazy but easy
  54. bm,
  55. cap_ends=False,
  56. radius=max(self.evaluate_input("Radius"), 0.0001),
  57. segments=min( max( self.evaluate_input("Number of Points"), 3), 1024),
  58. )
  59. # this is rotated 90 degrees, we need Y-up instead of Z-up
  60. from mathutils import Matrix
  61. from math import pi
  62. for v in bm.verts:
  63. v.co = Matrix.Rotation(pi/2, 4, 'X') @ v.co
  64. # done with this, push it to the data and free the bmesh.
  65. bm.to_mesh(data); bm.free()
  66. self.executed = True
  67. for c in TellClasses():
  68. setup_container(c)