primitives_containers.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from .node_container_common import *
  2. from .base_definitions import MantisNode
  3. def TellClasses():
  4. return [
  5. # Primitives
  6. CirclePrimitive,
  7. ]
  8. #*#-------------------------------#++#-------------------------------#*#
  9. # P R I M I T I V E S
  10. #*#-------------------------------#++#-------------------------------#*#
  11. class CirclePrimitive(MantisNode):
  12. '''A node representing a Circle Primitive mesh'''
  13. def __init__(self, signature, base_tree):
  14. self.base_tree=base_tree
  15. self.signature = signature
  16. self.inputs = {
  17. "Name" : NodeSocket(is_input = True, name = "Name", node = self),
  18. "Radius" : NodeSocket(is_input = True, name = "Radius", node = self),
  19. "Number of Points" : NodeSocket(is_input = True, name = "Number of Points", node = self),
  20. }
  21. self.outputs = {
  22. "Circle" : NodeSocket(is_input = False, name = "Circle", node=self),
  23. }
  24. self.parameters = {
  25. "Name":None,
  26. "Radius":None,
  27. "Number of Points":None,
  28. "Circle":None,
  29. }
  30. self.node_type = "UTILITY"
  31. self.hierarchy_connections = []
  32. self.connections = []
  33. self.hierarchy_dependencies = []
  34. self.dependencies = []
  35. self.prepared = True
  36. self.executed = False
  37. def bGetObject(self):
  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 bExecute(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