primitives_containers.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from .node_container_common import *
  2. from .base_definitions import MantisNode, NodeSocket
  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. super().__init__(signature, base_tree)
  15. inputs = [
  16. "Name",
  17. "Radius",
  18. "Number of Points",
  19. ]
  20. outputs = [
  21. "Circle",
  22. ]
  23. additional_parameters = {}
  24. self.inputs.init_sockets(inputs)
  25. self.outputs.init_sockets(outputs)
  26. self.init_parameters(additional_parameters=additional_parameters)
  27. self.node_type = "UTILITY"
  28. self.prepared = True
  29. def bGetObject(self):
  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 bExecute(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