|
| 1 | + |
| 2 | +from math import sqrt |
| 3 | + |
| 4 | +from mathutils import Matrix |
| 5 | +import bpy |
| 6 | +from bpy.props import FloatProperty, EnumProperty, BoolProperty |
| 7 | + |
| 8 | +from sverchok.node_tree import SverchCustomTreeNode |
| 9 | +from sverchok.data_structure import updateNode, zip_long_repeat, ensure_nesting_level |
| 10 | + |
| 11 | +from sverchok.utils.curve import SvEllipse |
| 12 | + |
| 13 | +class SvEllipseCurveNodeMK2(SverchCustomTreeNode, bpy.types.Node): |
| 14 | + """ |
| 15 | + Triggers: Ellipse Curve |
| 16 | + Tooltip: Generate ellipse curve |
| 17 | + """ |
| 18 | + bl_idname = 'SvEllipseCurveNodeMK2' |
| 19 | + bl_label = 'Ellipse (Curve)' |
| 20 | + sv_icon = 'SV_ELLIPSE' |
| 21 | + |
| 22 | + mode_items = [("AB", "a b", "Major Radius / Minor Radius", 1), |
| 23 | + ("AE", "a e", "Major Radius / Eccentricity", 2), |
| 24 | + ("AC", "a c", "Major Radius / Focal Length", 3)] |
| 25 | + |
| 26 | + centering_items = [(SvEllipse.F1, "F1", "Ellipse focal point 1", 1), |
| 27 | + (SvEllipse.CENTER, "C", "Ellipse center point", 2), |
| 28 | + (SvEllipse.F2, "F2", "Ellipse focal point 2", 3)] |
| 29 | + |
| 30 | + def update_sockets(self, context): |
| 31 | + self.inputs['Minor Radius'].enabled = (self.mode == "AB") |
| 32 | + self.inputs['Eccentricity'].enabled = (self.mode == "AE") |
| 33 | + self.inputs['Focal Length'].enabled = (self.mode == "AC") |
| 34 | + |
| 35 | + updateNode(self, context) |
| 36 | + |
| 37 | + mode: EnumProperty( |
| 38 | + name="Mode", items=mode_items, |
| 39 | + description="Ellipse definition mode", |
| 40 | + default="AB", update=update_sockets) |
| 41 | + |
| 42 | + centering: EnumProperty( |
| 43 | + name="Centering", items=centering_items, |
| 44 | + description="Center the ellipse around F1, C or F2", |
| 45 | + default=SvEllipse.CENTER, |
| 46 | + update=update_sockets) |
| 47 | + |
| 48 | + major_radius: FloatProperty( |
| 49 | + name='Major Radius', description='Ellipse major radius (semiaxis)', |
| 50 | + default=1.0, min=0.0, update=update_sockets) |
| 51 | + |
| 52 | + minor_radius: FloatProperty( |
| 53 | + name='Minor Radius', description='Ellipse minor radius (semiaxis)', |
| 54 | + default=0.8, min=0.0, update=update_sockets) |
| 55 | + |
| 56 | + eccentricity: FloatProperty( |
| 57 | + name='Eccentricity', description='Ellipse eccentricity', |
| 58 | + default=0.6, min=0.0, max=1.0, update=update_sockets) |
| 59 | + |
| 60 | + focal_length: FloatProperty( |
| 61 | + name='Focal Length', description='Ellipse focal length. Distance from ellipse’s center to it’s focal points', |
| 62 | + default=0.6, min=0.0, update=update_sockets) |
| 63 | + |
| 64 | + def draw_buttons(self, context, layout): |
| 65 | + col = layout.column(align=True) |
| 66 | + row = col.row(align=True) |
| 67 | + row.prop(self, "mode", expand=True) |
| 68 | + row = col.row(align=True) |
| 69 | + row.prop(self, "centering", expand=True) |
| 70 | + |
| 71 | + def sv_init(self, context): |
| 72 | + self.width = 160 |
| 73 | + self.inputs.new('SvStringsSocket', "Major Radius").prop_name = "major_radius" # 0 |
| 74 | + self.inputs.new('SvStringsSocket', "Minor Radius").prop_name = "minor_radius" # 1 |
| 75 | + self.inputs.new("SvStringsSocket", "Eccentricity").prop_name = "eccentricity" |
| 76 | + self.inputs.new("SvStringsSocket", "Focal Length").prop_name = "focal_length" |
| 77 | + self.inputs.new('SvMatrixSocket', "Matrix") # 2 |
| 78 | + |
| 79 | + self.outputs.new('SvCurveSocket', "Ellipse") |
| 80 | + self.outputs.new('SvVerticesSocket', "F1") |
| 81 | + self.outputs.new('SvVerticesSocket', "F2") |
| 82 | + |
| 83 | + self.update_sockets(context) |
| 84 | + |
| 85 | + def process(self): |
| 86 | + outputs = self.outputs |
| 87 | + # return if no outputs are connected |
| 88 | + if not any(s.is_linked for s in outputs): |
| 89 | + return |
| 90 | + |
| 91 | + major_radius_s = self.inputs['Major Radius'].sv_get(deepcopy=False) |
| 92 | + if self.mode == 'AB': |
| 93 | + input2_s = self.inputs["Minor Radius"].sv_get(deepcopy=False) |
| 94 | + elif self.mode == 'AE': |
| 95 | + input2_s = self.inputs["Eccentricity"].sv_get(deepcopy=False) |
| 96 | + else: |
| 97 | + input2_s = self.inputs["Focal Length"].sv_get(deepcopy=False) |
| 98 | + |
| 99 | + matrices_s = self.inputs['Matrix'].sv_get(default = [[Matrix()]]) |
| 100 | + |
| 101 | + major_radius_s = ensure_nesting_level(major_radius_s, 2) |
| 102 | + input2_s = ensure_nesting_level(input2_s, 2) |
| 103 | + matrices_s = ensure_nesting_level(matrices_s, 2, data_types=(Matrix,)) |
| 104 | + |
| 105 | + curves_out = [] |
| 106 | + f1_out = [] |
| 107 | + f2_out = [] |
| 108 | + for major_radius_i, input2_i, matrices_i in zip_long_repeat(major_radius_s, input2_s, matrices_s): |
| 109 | + new_curves = [] |
| 110 | + new_f1 = [] |
| 111 | + new_f2 = [] |
| 112 | + for major_radius, input2, matrix in zip_long_repeat(major_radius_i, input2_i, matrices_i): |
| 113 | + if self.mode == 'AB': |
| 114 | + minor_radius = input2 |
| 115 | + elif self.mode == 'AE': |
| 116 | + e = input2 |
| 117 | + minor_radius = major_radius * sqrt(1 - e*e) |
| 118 | + else: # AC |
| 119 | + c = input2 |
| 120 | + a = major_radius |
| 121 | + minor_radius = sqrt(a*a - c*c) |
| 122 | + |
| 123 | + ellipse = SvEllipse(matrix, major_radius, minor_radius, center_type = self.centering) |
| 124 | + f1, f2 = ellipse.to_equation().focal_points() |
| 125 | + new_f1.append(f1) |
| 126 | + new_f2.append(f2) |
| 127 | + new_curves.append(ellipse) |
| 128 | + |
| 129 | + curves_out.append(new_curves) |
| 130 | + f1_out.append(new_f1) |
| 131 | + f2_out.append(new_f2) |
| 132 | + |
| 133 | + self.outputs['Ellipse'].sv_set(curves_out) |
| 134 | + self.outputs['F1'].sv_set(f1_out) |
| 135 | + self.outputs['F2'].sv_set(f2_out) |
| 136 | + |
| 137 | +def register(): |
| 138 | + bpy.utils.register_class(SvEllipseCurveNodeMK2) |
| 139 | + |
| 140 | +def unregister(): |
| 141 | + bpy.utils.unregister_class(SvEllipseCurveNodeMK2) |
| 142 | + |
0 commit comments