primitives_containers.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 PrimitiveNode(MantisNode):
  12. def __init__(self, signature, base_tree, socket_templates=[]):
  13. super().__init__(signature, base_tree, socket_templates)
  14. self.node_type = "UTILITY"
  15. self.prepared = True
  16. def reset_execution(self):
  17. super().reset_execution()
  18. self.prepared=True
  19. class CirclePrimitive(PrimitiveNode):
  20. '''A node representing a Circle Primitive mesh'''
  21. def __init__(self, signature, base_tree):
  22. super().__init__(signature, base_tree)
  23. inputs = [
  24. "Name",
  25. "Radius",
  26. "Number of Points",
  27. ]
  28. outputs = [
  29. "Circle",
  30. ]
  31. additional_parameters = {}
  32. self.inputs.init_sockets(inputs)
  33. self.outputs.init_sockets(outputs)
  34. self.init_parameters(additional_parameters=additional_parameters)
  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. self.executed = True