1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # -----------------------------------------------------------------------
4 # Author: Alan Odom (Clockmender), Rune Morling (ermo) Copyright (c) 2019
5 # -----------------------------------------------------------------------
9 from math
import sin
, cos
, tan
, pi
10 from mathutils
import Vector
11 from .pdt_functions
import (
16 class PDT_OT_WaveGenerator(bpy
.types
.Operator
):
17 """Generate Trig Waves in Active Object"""
18 bl_idname
= "pdt.wave_generator"
19 bl_label
= "Generate Waves"
20 bl_options
= {"REGISTER", "UNDO"}
23 def poll(cls
, context
):
24 pg
= context
.scene
.pdt_pg
25 return pg
.trig_obj
is not None
27 def execute(self
, context
):
28 """Generate Trig Waves in Active Object.
31 Uses all the PDT trig_* variables.
33 This function will draw a trigonometrical wave based upon cycle length
34 One cycle is assumed to be 180 degrees, so half a revolution of an imaginary
35 rotating object. If a full cycle from 0 to 360 degrees is required, the cycles
36 number should be set to 2.
39 context: Blender bpy.context instance.
45 pg
= context
.scene
.pdt_pg
47 # Find the horizontal, vertical and depth axes in the view from working plane.
50 a1
, a2
, a3
= set_mode(plane
)
51 # Make sure object selected in the UI is the active object.
53 for obj
in bpy
.data
.objects
:
54 obj
.select_set(state
=False)
55 context
.view_layer
.objects
.active
= pg
.trig_obj
56 # x_inc is the increase in X (Horiz axis) per unit of resolution of the wave, so if
57 # resolution is 9, nine points will be drawn in each cycle representing increases of
58 # 20 degrees and 1/9th of the cycle length.
60 x_inc
= pg
.trig_len
/ pg
.trig_res
63 # Delete all existing vertices first.
65 bpy
.ops
.object.mode_set(mode
='EDIT')
66 for v
in pg
.trig_obj
.data
.vertices
:
68 bpy
.ops
.mesh
.delete(type='VERT')
69 bpy
.ops
.object.mode_set(mode
='OBJECT')
71 if pg
.trig_obj
.mode
!= "EDIT":
72 bpy
.ops
.object.mode_set(mode
='EDIT')
73 bm
= bmesh
.from_edit_mesh(pg
.trig_obj
.data
)
75 # Loop for each point in the number of cycles times the resolution value.
76 # Uses basic trigonomtry to calculate the wave locations.
77 # If Absolute has been set, all values are made positive.
78 # z_val is assumed to be the offset from the horizontal axis of the wave.
79 # These values will be offset by the Offset Vector given in the UI.
81 for i
in range((pg
.trig_res
* pg
.trig_cycles
) + 1):
82 # Uses a calculation of trig function angle of imaginary object times maximum amplitude
83 # of wave. So with reolution at 9, angular increments are 20 degrees.
84 # Angles must be in Radians for this calcultion.
86 if pg
.trig_type
== "sin":
88 z_val
= abs(sin((i
/ pg
.trig_res
) * pi
) * pg
.trig_amp
)
90 z_val
= sin((i
/ pg
.trig_res
) * pi
) * pg
.trig_amp
91 elif pg
.trig_type
== "cos":
93 z_val
= abs(cos((i
/ pg
.trig_res
) * pi
) * pg
.trig_amp
)
95 z_val
= cos((i
/ pg
.trig_res
) * pi
) * pg
.trig_amp
98 z_val
= abs(tan((i
/ pg
.trig_res
) * pi
) * pg
.trig_amp
)
100 z_val
= tan((i
/ pg
.trig_res
) * pi
) * pg
.trig_amp
102 if abs(z_val
) > pg
.trig_tanmax
:
104 z_val
= pg
.trig_tanmax
107 z_val
= pg
.trig_tanmax
109 z_val
= -pg
.trig_tanmax
111 # Start with Offset Vector from UI and add wave offsets to it.
112 # Axis a3 (depth) is never changed from offset vector in UI.
114 vert_loc
= Vector(pg
.trig_off
)
115 vert_loc
[a1
] = vert_loc
[a1
] + (i
* x_inc
)
116 vert_loc
[a2
] = vert_loc
[a2
] + z_val
118 # Translate view local coordinates (horiz, vert, depth) into World XYZ
120 vert_loc
= view_coords(vert_loc
[a1
], vert_loc
[a2
], vert_loc
[a3
])
121 vertex_new
= bm
.verts
.new(vert_loc
)
122 # Refresh Vertices list in object data.
124 bm
.verts
.ensure_lookup_table()
126 # Make an edge from last two vertices in object data.
128 bm
.edges
.new([bm
.verts
[-2], vertex_new
])
130 bmesh
.update_edit_mesh(pg
.trig_obj
.data
)
131 bpy
.ops
.object.mode_set(mode
='OBJECT')