1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and / or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 - 1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
#
import bpy
import bgl
import blf
import gpu
from gpu_extras.batch import batch_for_shader
import math
import mathutils
from mathutils import Vector
from bpy.props import (
EnumProperty,
)
# ------------------------------------------------------------
# ShowSplinesSequence
def draw_number(n, co, font_height):
point_list = []
numeral = [
[Vector((0, 0, 0)), Vector((0, 2, 0)), Vector((0, 2, 0)), Vector((1, 2, 0)), Vector((1, 2, 0)), Vector((1, 0, 0)), Vector((1, 0, 0)), Vector((0, 0, 0))],
[Vector((0, 1, 0)), Vector((1, 2, 0)), Vector((1, 2, 0)), Vector((1, 0, 0))],
[Vector((0, 2, 0)), Vector((1, 2, 0)), Vector((1, 2, 0)), Vector((1, 1, 0)), Vector((1, 1, 0)), Vector((0, 0, 0)), Vector((0, 0, 0)), Vector((1, 0, 0))],
[Vector((0, 2, 0)), Vector((1, 2, 0)), Vector((1, 2, 0)), Vector((0, 1, 0)), Vector((0, 1, 0)), Vector((1, 1, 0)), Vector((1, 1, 0)), Vector((0, 0, 0))],
[Vector((0, 2, 0)), Vector((0, 1, 0)), Vector((0, 1, 0)), Vector((1, 1, 0)), Vector((1, 1, 0)), Vector((1, 2, 0)), Vector((1, 2, 0)), Vector((1, 0, 0))],
[Vector((1, 2, 0)), Vector((0, 2, 0)), Vector((0, 2, 0)), Vector((0, 1, 0)), Vector((0, 1, 0)), Vector((1, 1, 0)), Vector((1, 1, 0)), Vector((1, 0, 0)), Vector((1, 0, 0)), Vector((0, 0, 0))],
[Vector((1, 2, 0)), Vector((0, 1, 0)), Vector((0, 1, 0)), Vector((0, 0, 0)), Vector((0, 0, 0)), Vector((1, 0, 0)), Vector((1, 0, 0)), Vector((1, 1, 0)), Vector((1, 1, 0)), Vector((0, 1, 0))],
[Vector((0, 2, 0)), Vector((1, 2, 0)), Vector((1, 2, 0)), Vector((0, 1, 0)), Vector((0, 1, 0)), Vector((0, 0, 0))],
[Vector((0, 1, 0)), Vector((0, 2, 0)), Vector((0, 2, 0)), Vector((1, 2, 0)), Vector((1, 2, 0)), Vector((1, 0, 0)), Vector((1, 0, 0)), Vector((0, 0, 0)), Vector((0, 0, 0)), Vector((0, 1, 0)), Vector((0, 1, 0)), Vector((1, 1, 0))],
[Vector((0, 0, 0)), Vector((1, 1, 0)), Vector((1, 1, 0)), Vector((1, 2, 0)), Vector((1, 2, 0)), Vector((0, 2, 0)), Vector((0, 2, 0)), Vector((0, 1, 0)), Vector((0, 1, 0)), Vector((1, 1, 0))],
]
for num in numeral[n]:
point_list.extend([num * font_height + co])
return point_list
def draw(self, context, splines, sequence_color, font_thickness, font_size, matrix_world):
splines_len = len(splines)
for n in range(0, splines_len):
res = [int(x) for x in str(n)]
i = 0
for r in res:
# draw some text
if splines[n].type == 'BEZIER':
first_point_co = matrix_world @ splines[n].bezier_points[0].co
else:
first_point = matrix_world @ splines[n].points[0].co
first_point_co = Vector((first_point.x, first_point.y, first_point.z))
first_point_co = Vector((i, 0, 0)) + first_point_co
points = draw_number(r, first_point_co, font_size)
shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
batch = batch_for_shader(shader, 'LINES', {"pos": points})
shader.bind()
bgl.glLineWidth(font_thickness)
shader.uniform_float("color", sequence_color)
batch.draw(shader)
i += font_size + font_size * 0.5
class ShowSplinesSequence(bpy.types.Operator):
bl_idname = "curve.show_splines_sequence"
bl_label = "Show Splines Sequence"
bl_description = "Show Splines Sequence / [ESC] - remove"
handlers = []
def modal(self, context, event):
context.area.tag_redraw()
if event.type in {'ESC'}:
for handler in self.handlers:
try:
bpy.types.SpaceView3D.draw_handler_remove(handler, 'WINDOW')
except:
pass
for handler in self.handlers:
self.handlers.remove(handler)
return {'CANCELLED'}
return {'PASS_THROUGH'}
def invoke(self, context, event):
if context.area.type == 'VIEW_3D':
# color change in the panel
sequence_color = bpy.context.scene.curvetools.sequence_color
font_thickness = bpy.context.scene.curvetools.font_thickness
font_size = bpy.context.scene.curvetools.font_size
splines = context.active_object.data.splines
matrix_world = context.active_object.matrix_world
# the arguments we pass the the callback
args = (self, context, splines, sequence_color, font_thickness, font_size, matrix_world)
# Add the region OpenGL drawing callback
# draw in view space with 'POST_VIEW' and 'PRE_VIEW'
self.handlers.append(bpy.types.SpaceView3D.draw_handler_add(draw, args, 'WINDOW', 'POST_VIEW'))
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
else:
self.report({'WARNING'},
"View3D not found, cannot run operator")
return {'CANCELLED'}
@classmethod
def poll(cls, context):
return (context.object is not None and
context.object.type == 'CURVE')
# ------------------------------------------------------------
# RearrangeSpline
def rearrangesplines(dataCurve, select_spline1, select_spline2):
spline1 = dataCurve.splines[select_spline1]
spline2 = dataCurve.splines[select_spline2]
bpy.ops.curve.select_all(action='SELECT')
bpy.ops.curve.spline_type_set(type='BEZIER')
bpy.ops.curve.select_all(action='DESELECT')
type1 = spline1.type
type2 = spline2.type
len_spline1 = len(spline1.bezier_points)
len_spline2 = len(spline2.bezier_points)
newSpline = dataCurve.splines.new(type=type1)
newSpline.bezier_points.add(len_spline1 - 1)
newSpline.use_cyclic_u = spline1.use_cyclic_u
for n in range(0, len_spline1):
newSpline.bezier_points[n].co = spline1.bezier_points[n].co
newSpline.bezier_points[n].handle_left_type = spline1.bezier_points[n].handle_left_type
newSpline.bezier_points[n].handle_left = spline1.bezier_points[n].handle_left
newSpline.bezier_points[n].handle_right_type = spline1.bezier_points[n].handle_right_type
newSpline.bezier_points[n].handle_right = spline1.bezier_points[n].handle_right
spline1.bezier_points[n].select_control_point = True
spline1.bezier_points[0].select_control_point = False
spline1.bezier_points[0].select_left_handle = False
spline1.bezier_points[0].select_right_handle = False
bpy.ops.curve.delete(type='VERT')
spline1.bezier_points[0].select_control_point = True
bpy.ops.curve.spline_type_set(type=type2)
bpy.ops.curve.select_all(action='DESELECT')
spline1.bezier_points.add(len_spline2 - 1)
spline1.use_cyclic_u = spline2.use_cyclic_u
for n in range(0, len_spline2):
spline1.bezier_points[n].co = spline2.bezier_points[n].co
spline1.bezier_points[n].handle_left_type = spline2.bezier_points[n].handle_left_type
spline1.bezier_points[n].handle_left = spline2.bezier_points[n].handle_left
spline1.bezier_points[n].handle_right_type = spline2.bezier_points[n].handle_right_type
spline1.bezier_points[n].handle_right = spline2.bezier_points[n].handle_right
spline1.bezier_points[n].select_control_point = False
spline1.bezier_points[n].select_left_handle = False
spline1.bezier_points[n].select_right_handle = False
spline2.bezier_points[n].select_control_point = True
spline2.bezier_points[0].select_control_point = False
spline2.bezier_points[0].select_left_handle = False
spline2.bezier_points[0].select_right_handle = False
bpy.ops.curve.delete(type='VERT')
spline2.bezier_points[0].select_control_point = True
bpy.ops.curve.spline_type_set(type=type1)
spline2.bezier_points.add(len_spline1 - 1)
spline2.use_cyclic_u = newSpline.use_cyclic_u
for m in range(0, len_spline1):
spline2.bezier_points[m].co = newSpline.bezier_points[m].co
spline2.bezier_points[m].handle_left_type = newSpline.bezier_points[m].handle_left_type
spline2.bezier_points[m].handle_left = newSpline.bezier_points[m].handle_left
spline2.bezier_points[m].handle_right_type = newSpline.bezier_points[m].handle_right_type
spline2.bezier_points[m].handle_right = newSpline.bezier_points[m].handle_right
bpy.ops.curve.select_all(action='DESELECT')
for point in newSpline.bezier_points:
point.select_control_point = True
bpy.ops.curve.delete(type='VERT')
spline2.bezier_points[0].select_control_point = True
def rearrange(dataCurve, select_spline, command):
len_splines = len(dataCurve.splines)
if command == 'NEXT':
if select_spline < len_splines - 1:
rearrangesplines(dataCurve, select_spline + 1, select_spline)
if command == 'PREV':
if select_spline > 0:
rearrangesplines(dataCurve, select_spline, select_spline - 1)
class RearrangeSpline(bpy.types.Operator):
bl_idname = "curve.rearrange_spline"
bl_label = "Rearrange Spline"
bl_description = "Rearrange Spline"
Types = [('NEXT', "Next", "next"),
('PREV', "Prev", "prev")]
command : EnumProperty(
name="command",
description="Command (prev or next)",
items=Types
)
def execute(self, context):
bpy.ops.object.mode_set(mode = 'EDIT')
bpy.context.view_layer.update()
dataCurve = context.active_object.data
splines = context.active_object.data.splines
select_spline = 0
sn = 0
for spline in splines:
for bezier_points in spline.bezier_points:
if bezier_points.select_control_point:
select_spline = sn
sn += 1
sn = 0
for spline in splines:
for point in spline.points:
if point.select:
select_spline = sn
sn += 1
rearrange(dataCurve, select_spline, self.command)
return {'FINISHED'}
@classmethod
def poll(cls, context):
return (context.object is not None and
context.object.type == 'CURVE')
|