Node Wrangler: create world output if the node tree is of type world
[blender-addons.git] / add_curve_extra_objects / add_curve_spirals.py
blob25a217a7bd07e3459a2cf16eeadb9076bdcf7e78
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 """
4 bl_info = {
5 "name": "Spirals",
6 "description": "Make spirals",
7 "author": "Alejandro Omar Chocano Vasquez",
8 "version": (1, 2, 2),
9 "blender": (2, 80, 0),
10 "location": "View3D > Add > Curve",
11 "warning": "",
12 "doc_url": "{BLENDER_MANUAL_URL}/addons/add_curve/extra_objects.html",
13 "category": "Add Curve",
15 """
17 import bpy
18 import time
19 from bpy.props import (
20 EnumProperty,
21 BoolProperty,
22 FloatProperty,
23 IntProperty,
24 FloatVectorProperty
26 from mathutils import (
27 Vector,
28 Matrix,
30 from math import (
31 sin, cos, pi
33 from bpy_extras.object_utils import object_data_add
34 from bpy.types import (
35 Operator,
36 Menu,
38 from bl_operators.presets import AddPresetBase
41 # make normal spiral
42 # ----------------------------------------------------------------------------
44 def make_spiral(props, context):
45 # archemedian and logarithmic can be plotted in cylindrical coordinates
47 # INPUT: turns->degree->max_phi, steps, direction
48 # Initialise Polar Coordinate Environment
49 props.degree = 360 * props.turns # If you want to make the slider for degree
50 steps = props.steps * props.turns # props.steps[per turn] -> steps[for the whole spiral]
51 props.z_scale = props.dif_z * props.turns
53 max_phi = pi * props.degree / 180 # max angle in radian
54 step_phi = max_phi / steps # angle in radians between two vertices
56 if props.spiral_direction == 'CLOCKWISE':
57 step_phi *= -1 # flip direction
58 max_phi *= -1
60 step_z = props.z_scale / (steps - 1) # z increase in one step
62 verts = []
63 verts.append([props.radius, 0, 0])
65 cur_phi = 0
66 cur_z = 0
68 # Archemedean: dif_radius, radius
69 cur_rad = props.radius
70 step_rad = props.dif_radius / (steps * 360 / props.degree)
71 # radius increase per angle for archemedean spiral|
72 # (steps * 360/props.degree)...Steps needed for 360 deg
73 # Logarithmic: radius, B_force, ang_div, dif_z
75 while abs(cur_phi) <= abs(max_phi):
76 cur_phi += step_phi
77 cur_z += step_z
79 if props.spiral_type == 'ARCH':
80 cur_rad += step_rad
81 if props.spiral_type == 'LOG':
82 # r = a*e^{|theta| * b}
83 cur_rad = props.radius * pow(props.B_force, abs(cur_phi))
85 px = cur_rad * cos(cur_phi)
86 py = cur_rad * sin(cur_phi)
87 verts.append([px, py, cur_z])
89 return verts
92 # make Spheric spiral
93 # ----------------------------------------------------------------------------
95 def make_spiral_spheric(props, context):
96 # INPUT: turns, steps[per turn], radius
97 # use spherical Coordinates
98 step_phi = (2 * pi) / props.steps # Step of angle in radians for one turn
99 steps = props.steps * props.turns # props.steps[per turn] -> steps[for the whole spiral]
101 max_phi = 2 * pi * props.turns # max angle in radian
102 step_phi = max_phi / steps # angle in radians between two vertices
103 if props.spiral_direction == 'CLOCKWISE': # flip direction
104 step_phi *= -1
105 max_phi *= -1
106 step_theta = pi / (steps - 1) # theta increase in one step (pi == 180 deg)
108 verts = []
109 verts.append([0, 0, -props.radius]) # First vertex at south pole
111 cur_phi = 0
112 cur_theta = -pi / 2 # Beginning at south pole
114 while abs(cur_phi) <= abs(max_phi):
115 # Coordinate Transformation sphere->rect
116 px = props.radius * cos(cur_theta) * cos(cur_phi)
117 py = props.radius * cos(cur_theta) * sin(cur_phi)
118 pz = props.radius * sin(cur_theta)
120 verts.append([px, py, pz])
121 cur_theta += step_theta
122 cur_phi += step_phi
124 return verts
127 # make torus spiral
128 # ----------------------------------------------------------------------------
130 def make_spiral_torus(props, context):
131 # INPUT: turns, steps, inner_radius, curves_number,
132 # mul_height, dif_inner_radius, cycles
133 max_phi = 2 * pi * props.turns * props.cycles # max angle in radian
134 step_phi = 2 * pi / props.steps # Step of angle in radians between two vertices
136 if props.spiral_direction == 'CLOCKWISE': # flip direction
137 step_phi *= -1
138 max_phi *= -1
140 step_theta = (2 * pi / props.turns) / props.steps
141 step_rad = props.dif_radius / (props.steps * props.turns)
142 step_inner_rad = props.dif_inner_radius / props.steps
143 step_z = props.dif_z / (props.steps * props.turns)
145 verts = []
147 cur_phi = 0 # Inner Ring Radius Angle
148 cur_theta = 0 # Ring Radius Angle
149 cur_rad = props.radius
150 cur_inner_rad = props.inner_radius
151 cur_z = 0
152 n_cycle = 0
154 while abs(cur_phi) <= abs(max_phi):
155 # Torus Coordinates -> Rect
156 px = (cur_rad + cur_inner_rad * cos(cur_phi)) * \
157 cos(props.curves_number * cur_theta)
158 py = (cur_rad + cur_inner_rad * cos(cur_phi)) * \
159 sin(props.curves_number * cur_theta)
160 pz = cur_inner_rad * sin(cur_phi) + cur_z
162 verts.append([px, py, pz])
164 if props.touch and cur_phi >= n_cycle * 2 * pi:
165 step_z = ((n_cycle + 1) * props.dif_inner_radius +
166 props.inner_radius) * 2 / (props.steps * props.turns)
167 n_cycle += 1
169 cur_theta += step_theta
170 cur_phi += step_phi
171 cur_rad += step_rad
172 cur_inner_rad += step_inner_rad
173 cur_z += step_z
175 return verts
177 # ------------------------------------------------------------
178 # calculates the matrix for the new object
179 # depending on user pref
181 def align_matrix(context, location):
182 loc = Matrix.Translation(location)
183 obj_align = context.preferences.edit.object_align
184 if (context.space_data.type == 'VIEW_3D' and
185 obj_align == 'VIEW'):
186 rot = context.space_data.region_3d.view_matrix.to_3x3().inverted().to_4x4()
187 else:
188 rot = Matrix()
189 align_matrix = loc @ rot
191 return align_matrix
193 # ------------------------------------------------------------
194 # get array of vertcoordinates according to splinetype
195 def vertsToPoints(Verts, splineType):
197 # main vars
198 vertArray = []
200 # array for BEZIER spline output (V3)
201 if splineType == 'BEZIER':
202 for v in Verts:
203 vertArray += v
205 # array for nonBEZIER output (V4)
206 else:
207 for v in Verts:
208 vertArray += v
209 if splineType == 'NURBS':
210 # for nurbs w=1
211 vertArray.append(1)
212 else:
213 # for poly w=0
214 vertArray.append(0)
215 return vertArray
217 def draw_curve(props, context, align_matrix):
218 # output splineType 'POLY' 'NURBS' 'BEZIER'
219 splineType = props.curve_type
221 if props.spiral_type == 'ARCH':
222 verts = make_spiral(props, context)
223 if props.spiral_type == 'LOG':
224 verts = make_spiral(props, context)
225 if props.spiral_type == 'SPHERE':
226 verts = make_spiral_spheric(props, context)
227 if props.spiral_type == 'TORUS':
228 verts = make_spiral_torus(props, context)
230 # create object
231 if bpy.context.mode == 'EDIT_CURVE':
232 Curve = context.active_object
233 newSpline = Curve.data.splines.new(type=splineType) # spline
234 else:
235 # create curve
236 dataCurve = bpy.data.curves.new(name='Spiral', type='CURVE') # curvedatablock
237 newSpline = dataCurve.splines.new(type=splineType) # spline
239 # create object with newCurve
240 Curve = object_data_add(context, dataCurve) # place in active scene
241 Curve.matrix_world = align_matrix # apply matrix
242 Curve.rotation_euler = props.rotation_euler
243 Curve.select_set(True)
245 # turn verts into array
246 vertArray = vertsToPoints(verts, splineType)
248 for spline in Curve.data.splines:
249 if spline.type == 'BEZIER':
250 for point in spline.bezier_points:
251 point.select_control_point = False
252 point.select_left_handle = False
253 point.select_right_handle = False
254 else:
255 for point in spline.points:
256 point.select = False
258 # create newSpline from vertarray
259 if splineType == 'BEZIER':
260 newSpline.bezier_points.add(int(len(vertArray) * 0.33))
261 newSpline.bezier_points.foreach_set('co', vertArray)
262 for point in newSpline.bezier_points:
263 point.handle_right_type = props.handleType
264 point.handle_left_type = props.handleType
265 point.select_control_point = True
266 point.select_left_handle = True
267 point.select_right_handle = True
268 else:
269 newSpline.points.add(int(len(vertArray) * 0.25 - 1))
270 newSpline.points.foreach_set('co', vertArray)
271 newSpline.use_endpoint_u = False
272 for point in newSpline.points:
273 point.select = True
275 # set curveOptions
276 newSpline.use_cyclic_u = props.use_cyclic_u
277 newSpline.use_endpoint_u = props.endp_u
278 newSpline.order_u = props.order_u
280 # set curveOptions
281 Curve.data.dimensions = props.shape
282 Curve.data.use_path = True
283 if props.shape == '3D':
284 Curve.data.fill_mode = 'FULL'
285 else:
286 Curve.data.fill_mode = 'BOTH'
288 # move and rotate spline in edit mode
289 if bpy.context.mode == 'EDIT_CURVE':
290 bpy.ops.transform.translate(value = props.startlocation)
291 bpy.ops.transform.rotate(value = props.rotation_euler[0], orient_axis = 'X')
292 bpy.ops.transform.rotate(value = props.rotation_euler[1], orient_axis = 'Y')
293 bpy.ops.transform.rotate(value = props.rotation_euler[2], orient_axis = 'Z')
295 class CURVE_OT_spirals(Operator):
296 bl_idname = "curve.spirals"
297 bl_label = "Curve Spirals"
298 bl_description = "Create different types of spirals"
299 bl_options = {'REGISTER', 'UNDO', 'PRESET'}
301 # align_matrix for the invoke
302 align_matrix : Matrix()
304 spiral_type : EnumProperty(
305 items=[('ARCH', "Archemedian", "Archemedian"),
306 ("LOG", "Logarithmic", "Logarithmic"),
307 ("SPHERE", "Spheric", "Spheric"),
308 ("TORUS", "Torus", "Torus")],
309 default='ARCH',
310 name="Spiral Type",
311 description="Type of spiral to add"
313 spiral_direction : EnumProperty(
314 items=[('COUNTER_CLOCKWISE', "Counter Clockwise",
315 "Wind in a counter clockwise direction"),
316 ("CLOCKWISE", "Clockwise",
317 "Wind in a clockwise direction")],
318 default='COUNTER_CLOCKWISE',
319 name="Spiral Direction",
320 description="Direction of winding"
322 turns : IntProperty(
323 default=1,
324 min=1, max=1000,
325 description="Length of Spiral in 360 deg"
327 steps : IntProperty(
328 default=24,
329 min=2, max=1000,
330 description="Number of Vertices per turn"
332 radius : FloatProperty(
333 default=1.00,
334 min=0.00, max=100.00,
335 description="Radius for first turn"
337 dif_z : FloatProperty(
338 default=0,
339 min=-10.00, max=100.00,
340 description="Increase in Z axis per turn"
342 # needed for 1 and 2 spiral_type
343 # Archemedian variables
344 dif_radius : FloatProperty(
345 default=0.00,
346 min=-50.00, max=50.00,
347 description="Radius increment in each turn"
349 # step between turns(one turn equals 360 deg)
350 # Log variables
351 B_force : FloatProperty(
352 default=1.00,
353 min=0.00, max=30.00,
354 description="Factor of exponent"
356 # Torus variables
357 inner_radius : FloatProperty(
358 default=0.20,
359 min=0.00, max=100,
360 description="Inner Radius of Torus"
362 dif_inner_radius : FloatProperty(
363 default=0,
364 min=-10, max=100,
365 description="Increase of inner Radius per Cycle"
367 dif_radius : FloatProperty(
368 default=0,
369 min=-10, max=100,
370 description="Increase of Torus Radius per Cycle"
372 cycles : FloatProperty(
373 default=1,
374 min=0.00, max=1000,
375 description="Number of Cycles"
377 curves_number : IntProperty(
378 default=1,
379 min=1, max=400,
380 description="Number of curves of spiral"
382 touch: BoolProperty(
383 default=False,
384 description="No empty spaces between cycles"
386 # Curve Options
387 shapeItems = [
388 ('2D', "2D", "2D shape Curve"),
389 ('3D', "3D", "3D shape Curve")]
390 shape : EnumProperty(
391 name="2D / 3D",
392 items=shapeItems,
393 description="2D or 3D Curve",
394 default='3D'
396 curve_type : EnumProperty(
397 name="Output splines",
398 description="Type of splines to output",
399 items=[
400 ('POLY', "Poly", "Poly Spline type"),
401 ('NURBS', "Nurbs", "Nurbs Spline type"),
402 ('BEZIER', "Bezier", "Bezier Spline type")],
403 default='POLY'
405 use_cyclic_u : BoolProperty(
406 name="Cyclic",
407 default=False,
408 description="make curve closed"
410 endp_u : BoolProperty(
411 name="Use endpoint u",
412 default=True,
413 description="stretch to endpoints"
415 order_u : IntProperty(
416 name="Order u",
417 default=4,
418 min=2, soft_min=2,
419 max=6, soft_max=6,
420 description="Order of nurbs spline"
422 handleType : EnumProperty(
423 name="Handle type",
424 default='VECTOR',
425 description="Bezier handles type",
426 items=[
427 ('VECTOR', "Vector", "Vector type Bezier handles"),
428 ('AUTO', "Auto", "Automatic type Bezier handles")]
430 edit_mode : BoolProperty(
431 name="Show in edit mode",
432 default=True,
433 description="Show in edit mode"
435 startlocation : FloatVectorProperty(
436 name="",
437 description="Start location",
438 default=(0.0, 0.0, 0.0),
439 subtype='TRANSLATION'
441 rotation_euler : FloatVectorProperty(
442 name="",
443 description="Rotation",
444 default=(0.0, 0.0, 0.0),
445 subtype='EULER'
448 def draw(self, context):
449 layout = self.layout
450 col = layout.column_flow(align=True)
452 col.label(text="Presets:")
454 row = col.row(align=True)
455 row.menu("OBJECT_MT_spiral_curve_presets",
456 text=bpy.types.OBJECT_MT_spiral_curve_presets.bl_label)
457 row.operator("curve_extras.spiral_presets", text=" + ")
458 op = row.operator("curve_extras.spiral_presets", text=" - ")
459 op.remove_active = True
461 layout.prop(self, "spiral_type")
462 layout.prop(self, "spiral_direction")
464 col = layout.column(align=True)
465 col.label(text="Spiral Parameters:")
466 col.prop(self, "turns", text="Turns")
467 col.prop(self, "steps", text="Steps")
469 box = layout.box()
470 if self.spiral_type == 'ARCH':
471 box.label(text="Archemedian Settings:")
472 col = box.column(align=True)
473 col.prop(self, "dif_radius", text="Radius Growth")
474 col.prop(self, "radius", text="Radius")
475 col.prop(self, "dif_z", text="Height")
477 if self.spiral_type == 'LOG':
478 box.label(text="Logarithmic Settings:")
479 col = box.column(align=True)
480 col.prop(self, "radius", text="Radius")
481 col.prop(self, "B_force", text="Expansion Force")
482 col.prop(self, "dif_z", text="Height")
484 if self.spiral_type == 'SPHERE':
485 box.label(text="Spheric Settings:")
486 box.prop(self, "radius", text="Radius")
488 if self.spiral_type == 'TORUS':
489 box.label(text="Torus Settings:")
490 col = box.column(align=True)
491 col.prop(self, "cycles", text="Number of Cycles")
493 if self.dif_inner_radius == 0 and self.dif_z == 0:
494 self.cycles = 1
495 col.prop(self, "radius", text="Radius")
497 if self.dif_z == 0:
498 col.prop(self, "dif_z", text="Height per Cycle")
499 else:
500 box2 = box.box()
501 col2 = box2.column(align=True)
502 col2.prop(self, "dif_z", text="Height per Cycle")
503 col2.prop(self, "touch", text="Make Snail")
505 col = box.column(align=True)
506 col.prop(self, "curves_number", text="Curves Number")
507 col.prop(self, "inner_radius", text="Inner Radius")
508 col.prop(self, "dif_radius", text="Increase of Torus Radius")
509 col.prop(self, "dif_inner_radius", text="Increase of Inner Radius")
511 row = layout.row()
512 row.prop(self, "shape", expand=True)
514 # output options
515 col = layout.column()
516 col.label(text="Output Curve Type:")
517 col.row().prop(self, "curve_type", expand=True)
519 if self.curve_type == 'NURBS':
520 col.prop(self, "order_u")
521 elif self.curve_type == 'BEZIER':
522 col.row().prop(self, 'handleType', expand=True)
524 col = layout.column()
525 col.row().prop(self, "use_cyclic_u", expand=True)
527 col = layout.column()
528 col.row().prop(self, "edit_mode", expand=True)
530 box = layout.box()
531 box.label(text="Location:")
532 box.prop(self, "startlocation")
533 box = layout.box()
534 box.label(text="Rotation:")
535 box.prop(self, "rotation_euler")
537 @classmethod
538 def poll(cls, context):
539 return context.scene is not None
541 def execute(self, context):
542 # turn off 'Enter Edit Mode'
543 use_enter_edit_mode = bpy.context.preferences.edit.use_enter_edit_mode
544 bpy.context.preferences.edit.use_enter_edit_mode = False
546 time_start = time.time()
547 self.align_matrix = align_matrix(context, self.startlocation)
548 draw_curve(self, context, self.align_matrix)
550 if use_enter_edit_mode:
551 bpy.ops.object.mode_set(mode = 'EDIT')
553 # restore pre operator state
554 bpy.context.preferences.edit.use_enter_edit_mode = use_enter_edit_mode
556 if self.edit_mode:
557 bpy.ops.object.mode_set(mode = 'EDIT')
558 else:
559 bpy.ops.object.mode_set(mode = 'OBJECT')
561 #self.report({'INFO'},
562 #"Drawing Spiral Finished: %.4f sec" % (time.time() - time_start))
564 return {'FINISHED'}
567 class CURVE_EXTRAS_OT_spirals_presets(AddPresetBase, Operator):
568 bl_idname = "curve_extras.spiral_presets"
569 bl_label = "Spirals"
570 bl_description = "Spirals Presets"
571 preset_menu = "OBJECT_MT_spiral_curve_presets"
572 preset_subdir = "curve_extras/curve.spirals"
574 preset_defines = [
575 "op = bpy.context.active_operator",
577 preset_values = [
578 "op.spiral_type",
579 "op.curve_type",
580 "op.spiral_direction",
581 "op.turns",
582 "op.steps",
583 "op.radius",
584 "op.dif_z",
585 "op.dif_radius",
586 "op.B_force",
587 "op.inner_radius",
588 "op.dif_inner_radius",
589 "op.cycles",
590 "op.curves_number",
591 "op.touch",
595 class OBJECT_MT_spiral_curve_presets(Menu):
596 '''Presets for curve.spiral'''
597 bl_label = "Spiral Curve Presets"
598 bl_idname = "OBJECT_MT_spiral_curve_presets"
599 preset_subdir = "curve_extras/curve.spirals"
600 preset_operator = "script.execute_preset"
602 draw = bpy.types.Menu.draw_preset
605 # Register
606 classes = [
607 CURVE_OT_spirals,
608 CURVE_EXTRAS_OT_spirals_presets,
609 OBJECT_MT_spiral_curve_presets
612 def register():
613 from bpy.utils import register_class
614 for cls in classes:
615 register_class(cls)
617 def unregister():
618 from bpy.utils import unregister_class
619 for cls in reversed(classes):
620 unregister_class(cls)
622 if __name__ == "__main__":
623 register()