Fix io_anim_camera error exporting cameras with quotes in their name
[blender-addons.git] / precision_drawing_tools / pdt_trig_waves.py
blob84bc4b6fbd73623d414675574b29497567871c97
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # -----------------------------------------------------------------------
4 # Author: Alan Odom (Clockmender), Rune Morling (ermo) Copyright (c) 2019
5 # -----------------------------------------------------------------------
7 import bpy
8 import bmesh
9 from math import sin, cos, tan, pi
10 from mathutils import Vector
11 from .pdt_functions import (
12 set_mode,
13 view_coords,
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"}
22 @classmethod
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.
30 Note:
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.
38 Args:
39 context: Blender bpy.context instance.
41 Returns:
42 Nothing.
43 """
45 pg = context.scene.pdt_pg
46 plane = pg.plane
47 # Find the horizontal, vertical and depth axes in the view from working plane.
48 # Order is: H, V, D.
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
62 if pg.trig_del:
63 # Delete all existing vertices first.
65 bpy.ops.object.mode_set(mode='EDIT')
66 for v in pg.trig_obj.data.vertices:
67 v.select = True
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":
87 if pg.trig_abs:
88 z_val = abs(sin((i / pg.trig_res) * pi) * pg.trig_amp)
89 else:
90 z_val = sin((i / pg.trig_res) * pi) * pg.trig_amp
91 elif pg.trig_type == "cos":
92 if pg.trig_abs:
93 z_val = abs(cos((i / pg.trig_res) * pi) * pg.trig_amp)
94 else:
95 z_val = cos((i / pg.trig_res) * pi) * pg.trig_amp
96 else:
97 if pg.trig_abs:
98 z_val = abs(tan((i / pg.trig_res) * pi) * pg.trig_amp)
99 else:
100 z_val = tan((i / pg.trig_res) * pi) * pg.trig_amp
102 if abs(z_val) > pg.trig_tanmax:
103 if z_val >= 0:
104 z_val = pg.trig_tanmax
105 else:
106 if pg.trig_abs:
107 z_val = pg.trig_tanmax
108 else:
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
117 if plane == "LO":
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()
125 if i > 0:
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')
133 return {"FINISHED"}