Fix io_anim_camera error exporting cameras with quotes in their name
[blender-addons.git] / curve_tools / show_resolution.py
blobcd63f59f01d530ae900ced82549d6b9bff933930
1 # SPDX-License-Identifier: GPL-2.0-or-later
4 # LOAD MODUL #
5 import bpy
6 from bpy import *
7 from bpy.props import *
9 import bgl
10 import blf
11 import gpu
12 from gpu_extras.batch import batch_for_shader
14 import math
15 import mathutils
16 from mathutils import Vector
17 from mathutils.geometry import interpolate_bezier
20 def get_points(spline, matrix_world):
22 bezier_points = spline.bezier_points
24 if len(bezier_points) < 2:
25 return []
27 r = spline.resolution_u + 1
28 if r < 2:
29 return []
30 segments = len(bezier_points)
32 if not spline.use_cyclic_u:
33 segments -= 1
35 point_list = []
36 for i in range(segments):
37 inext = (i + 1) % len(bezier_points)
39 bezier_points1 = matrix_world @ bezier_points[i].co
40 handle1 = matrix_world @ bezier_points[i].handle_right
41 handle2 = matrix_world @ bezier_points[inext].handle_left
42 bezier_points2 = matrix_world @ bezier_points[inext].co
44 bezier = bezier_points1, handle1, handle2, bezier_points2, r
45 points = interpolate_bezier(*bezier)
46 point_list.extend(points)
48 return point_list
50 def draw(self, context, splines, curve_vertcolor, matrix_world):
52 for spline in splines:
53 points = get_points(spline, matrix_world)
55 shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
57 batch = batch_for_shader(shader, 'POINTS', {"pos": points})
59 shader.bind()
60 shader.uniform_float("color", curve_vertcolor)
61 batch.draw(shader)
64 class ShowCurveResolution(bpy.types.Operator):
65 bl_idname = "curvetools.show_resolution"
66 bl_label = "Show Curve Resolution"
67 bl_description = "Show curve Resolution / [ESC] - remove"
69 handlers = []
71 def modal(self, context, event):
72 context.area.tag_redraw()
74 if event.type in {'ESC'}:
75 for handler in self.handlers:
76 try:
77 bpy.types.SpaceView3D.draw_handler_remove(handler, 'WINDOW')
78 except:
79 pass
80 for handler in self.handlers:
81 self.handlers.remove(handler)
82 return {'CANCELLED'}
84 return {'PASS_THROUGH'}
87 def invoke(self, context, event):
89 if context.area.type == 'VIEW_3D':
91 # color change in the panel
92 curve_vertcolor = bpy.context.scene.curvetools.curve_vertcolor
94 splines = context.active_object.data.splines
95 matrix_world = context.active_object.matrix_world
97 # the arguments we pass the the callback
98 args = (self, context, splines, curve_vertcolor, matrix_world)
100 # Add the region OpenGL drawing callback
101 # draw in view space with 'POST_VIEW' and 'PRE_VIEW'
102 self.handlers.append(bpy.types.SpaceView3D.draw_handler_add(draw, args, 'WINDOW', 'POST_VIEW'))
104 context.window_manager.modal_handler_add(self)
105 return {'RUNNING_MODAL'}
106 else:
107 self.report({'WARNING'},
108 "View3D not found, cannot run operator")
109 return {'CANCELLED'}
111 @classmethod
112 def poll(cls, context):
113 return (context.object is not None and
114 context.object.type == 'CURVE')
116 def register():
117 for cls in classes:
118 bpy.utils.register_class(operators)
120 def unregister():
121 for cls in classes:
122 bpy.utils.unregister_class(operators)
124 if __name__ == "__main__":
125 register()
127 operators = [
128 ShowCurveResolution,