Export_3ds: Improved distance cue nodes setup
[blender-addons.git] / curve_tools / show_resolution.py
blob485500adbdb676c1e7c76e70f8c2013f19597903
1 # SPDX-FileCopyrightText: 2019-2023 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
6 # LOAD MODULE #
7 import bpy
8 from bpy import *
9 from bpy.props import *
11 import blf
12 import gpu
13 from gpu_extras.batch import batch_for_shader
15 import math
16 import mathutils
17 from mathutils import Vector
18 from mathutils.geometry import interpolate_bezier
21 def get_points(spline, matrix_world):
23 bezier_points = spline.bezier_points
25 if len(bezier_points) < 2:
26 return []
28 r = spline.resolution_u + 1
29 if r < 2:
30 return []
31 segments = len(bezier_points)
33 if not spline.use_cyclic_u:
34 segments -= 1
36 point_list = []
37 for i in range(segments):
38 inext = (i + 1) % len(bezier_points)
40 bezier_points1 = matrix_world @ bezier_points[i].co
41 handle1 = matrix_world @ bezier_points[i].handle_right
42 handle2 = matrix_world @ bezier_points[inext].handle_left
43 bezier_points2 = matrix_world @ bezier_points[inext].co
45 bezier = bezier_points1, handle1, handle2, bezier_points2, r
46 points = interpolate_bezier(*bezier)
47 point_list.extend(points)
49 return point_list
51 def draw(self, context, splines, curve_vertcolor, matrix_world):
53 for spline in splines:
54 points = get_points(spline, matrix_world)
56 shader = gpu.shader.from_builtin('UNIFORM_COLOR')
58 batch = batch_for_shader(shader, 'POINTS', {"pos": points})
60 shader.bind()
61 shader.uniform_float("color", curve_vertcolor)
62 batch.draw(shader)
65 class ShowCurveResolution(bpy.types.Operator):
66 bl_idname = "curvetools.show_resolution"
67 bl_label = "Show Curve Resolution"
68 bl_description = "Show curve Resolution / [ESC] - remove"
70 handlers = []
72 def modal(self, context, event):
73 context.area.tag_redraw()
75 if event.type in {'ESC'}:
76 for handler in self.handlers:
77 try:
78 bpy.types.SpaceView3D.draw_handler_remove(handler, 'WINDOW')
79 except:
80 pass
81 for handler in self.handlers:
82 self.handlers.remove(handler)
83 return {'CANCELLED'}
85 return {'PASS_THROUGH'}
88 def invoke(self, context, event):
90 if context.area.type == 'VIEW_3D':
92 # color change in the panel
93 curve_vertcolor = bpy.context.scene.curvetools.curve_vertcolor
95 splines = context.active_object.data.splines
96 matrix_world = context.active_object.matrix_world
98 # the arguments we pass the the callback
99 args = (self, context, splines, curve_vertcolor, matrix_world)
101 # Add the region OpenGL drawing callback
102 # draw in view space with 'POST_VIEW' and 'PRE_VIEW'
103 self.handlers.append(bpy.types.SpaceView3D.draw_handler_add(draw, args, 'WINDOW', 'POST_VIEW'))
105 context.window_manager.modal_handler_add(self)
106 return {'RUNNING_MODAL'}
107 else:
108 self.report({'WARNING'},
109 "View3D not found, cannot run operator")
110 return {'CANCELLED'}
112 @classmethod
113 def poll(cls, context):
114 return (context.object is not None and
115 context.object.type == 'CURVE')
117 def register():
118 for cls in classes:
119 bpy.utils.register_class(operators)
121 def unregister():
122 for cls in classes:
123 bpy.utils.unregister_class(operators)
125 if __name__ == "__main__":
126 register()
128 operators = [
129 ShowCurveResolution,