-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgp_draw.py
350 lines (298 loc) · 10.9 KB
/
gp_draw.py
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import bpy
import os
import mathutils
from mathutils import Vector
import math
import random
_draw = None
def get_draw():
global _draw
if not _draw or not _draw.is_valid():
_draw = LineDraw("fence", (0, 0.8, 1.0))
return _draw
def clear():
draw = get_draw()
draw.clear()
def draw_debug():
# test_grease_pencil()
padding = bpy.context.scene.BGE_Settings.padding
draw = get_draw()
draw.clear()
step = 0
def add_text(step, text):
draw.add_text(text, Vector((0, -step, 0)), padding)
return step + 1
step = add_text(step, "abcdefghijklm")
step = add_text(step, "nopqrstuvwxyz")
step = add_text(step, "ABCDEFGHIJKLM")
step = add_text(step, "NOPQRSTUVWXYZ")
step = add_text(step, "0123456789")
step = add_text(step, "~!@#$%^&*()")
step = add_text(step, "_-+\"';:,.<>[](){}\\/?")
step = add_text(step, "www.renderhjs.net")
# Draw circles
# for i in range(1,8):
# draw.add_circle(Vector((4,4,0)), i*0.5, i*3)
class LineDraw:
name = ""
color = (0, 0, 0)
gp = None
gp_layer = None
gp_palette = None
gp_color = None
gp_frame = None
gp_mat = None
def __init__(self, name, color):
self.name = name
self.color = color
self.setup_gp()
def clear(self):
self.gp_frame.clear()
def add_box(self, position, size=1.0):
self.add_line([
position + Vector((-0.5, -0.5, -0.5)) * size,
position + Vector((+0.5, -0.5, -0.5)) * size,
position + Vector((+0.5, +0.5, -0.5)) * size,
position + Vector((-0.5, +0.5, -0.5)) * size,
position + Vector((-0.5, -0.5, -0.5)) * size,
])
self.add_line([
position + Vector((-0.5, -0.5, -0.5)) * size,
position + Vector((-0.5, -0.5, +0.5)) * size,
])
self.add_line([
position + Vector((+0.5, -0.5, -0.5)) * size,
position + Vector((+0.5, -0.5, +0.5)) * size,
])
self.add_line([
position + Vector((+0.5, +0.5, -0.5)) * size,
position + Vector((+0.5, +0.5, +0.5)) * size,
])
self.add_line([
position + Vector((-0.5, +0.5, -0.5)) * size,
position + Vector((-0.5, +0.5, +0.5)) * size,
])
self.add_line([
position + Vector((-0.5, -0.5, +0.5)) * size,
position + Vector((+0.5, -0.5, +0.5)) * size,
position + Vector((+0.5, +0.5, +0.5)) * size,
position + Vector((-0.5, +0.5, +0.5)) * size,
position + Vector((-0.5, -0.5, +0.5)) * size,
])
# def add_cross(self, position, size=1.0):
# print("...")
def add_circle(self, position, radius=1, sides=8, alpha=1.0, dash=0.0):
for i in range(sides):
a0 = ((i + 0) * (360 / sides)) * math.pi / 180
a1 = ((i + 1) * (360 / sides)) * math.pi / 180
A = position + Vector((math.cos(a0), math.sin(a0), 0)) * radius
B = position + Vector((math.cos(a1), math.sin(a1), 0)) * radius
self.add_line([A, B], alpha=alpha, dash=dash)
def add_lines(self, lines, alpha=1.0, dash=0.0):
for line in lines:
self.add_line(line, alpha, dash)
def add_line(self, points, alpha=1.0, dash=0.0):
if dash == 0:
stroke = self.get_gp_stroke()
offset = len(stroke.points)
stroke.points.add(len(points))
for i in range(len(points)):
index = offset + i
stroke.points[index].co = points[i]
stroke.points[index].select = True
stroke.points[index].pressure = 1
stroke.points[index].strength = alpha
else:
for i in range(len(points) - 1):
# stroke = self.get_gp_stroke()
length = (points[i] - points[i - 1]).magnitude
steps = math.floor((length / dash) / 2)
A = points[i]
B = points[i + 1]
for s in range(steps):
stroke = self.get_gp_stroke()
stroke.points.add(2)
stroke.points[-2].co = A + (B - A).normalized() * (s * (dash * 2))
stroke.points[-2].select = True
stroke.points[-2].pressure = 1
stroke.points[-2].strength = alpha
stroke.points[-1].co = A + (B - A).normalized() * (s * (dash * 2) + dash)
stroke.points[-1].select = True
stroke.points[-1].pressure = 1
stroke.points[-1].strength = alpha
def add_text(self, text, pos=Vector((0, 0, 0)), size=1.0):
# text = text.upper()
size_xy = Vector((0.66, 1)) * size
padding = size_xy.x / 2
offset = 0
def add_character(strokes):
nonlocal offset
for stroke in strokes:
path = []
for id in stroke:
x = (id % 3) * (size_xy.x / 2) + (offset * (size_xy.x * 1.5)) + padding
y = math.floor(id / 3) * size_xy.y / 2 + padding
path.append(pos + Vector((x, -size_xy.y - 2 * padding + y, 0)))
# add_mesh_edges(bm, path)
self.add_line(path)
# 6 -- 7 -- 8
# | | |
# 3 -- 4 -- 5
# | | |
# 0 -- 1 -- 2
# | | |
#-3 - -2 - -1
chars = {
'a': [[0, 3, 5, 1, 0], [5, 2]],
'b': [[6, 0, 2, 5, 3]],
'c': [[5, 3, 0, 2]],
'd': [[8, 2, 0, 3, 5]],
'e': [[5, 3, 0, 5], [0, 2]],
'f': [[7, 3, 0], [3, 4]],
'g': [[2, 5, 3, 0, 2, -2, -3]],
'h': [[6, 0], [3, 5, 2]],
'i': [[4, 1], [7, 8]],
'j': [[4, 1, -3], [7, 8]],
'k': [[6, 0], [3, 4], [3, 1]],
'l': [[6, 3, 1]],
'm': [[0, 3, 5, 2], [4, 1]],
'n': [[0, 3, 2, 5]],
'o': [[0, 3, 5, 2, 0]],
'p': [[-3, 3, 5, 2, 0]],
'q': [[-1, 5, 3, 0, 2]],
'r': [[3, 0, 4, 5]],
's': [[5, 4, 0, 2, -2, -3]],
't': [[3, 5], [4, 1]],
'u': [[3, 0, 2, 5]],
'v': [[3, 1, 5]],
'w': [[3, 0, 4, 1, 5]],
'x': [[3, 2], [0, 5]],
'y': [[3, 1], [5, -3]],
'z': [[3, 5, 0, 2]],
# Alhabet Uppercase
'A': [[0, 3, 7, 5, 2], [3, 5]],
'B': [[0, 6, 8, 4, 2, 0], [3, 4]],
'C': [[2, 1, 3, 7, 8]],
'D': [[0, 6, 7, 5, 1, 0]],
'E': [[2, 0, 6, 8], [3, 4]],
'F': [[0, 6, 8], [3, 4]],
'G': [[4, 5, 2, 0, 3, 7, 8]],
'H': [[0, 6], [3, 5], [2, 8]],
'I': [[0, 2], [1, 7], [6, 8]],
'J': [[6, 8, 5, 1, 0, 3]],
'K': [[6, 0], [3, 4, 8], [4, 2]],
'L': [[6, 0, 2]],
'M': [[0, 6, 4, 8, 2]],
'N': [[0, 6, 2, 8]],
'O': [[1, 3, 7, 5, 1]],
'P': [[0, 6, 7, 5, 3]],
'Q': [[1, 3, 7, 5, 1], [4, 2]],
'R': [[0, 6, 8, 4, 2], [3, 4]],
'S': [[0, 1, 5, 3, 7, 8]],
'T': [[6, 8], [7, 1]],
'U': [[6, 0, 2, 8]],
'V': [[6, 3, 1, 5, 8]],
'W': [[6, 0, 4, 2, 8]],
'X': [[6, 2], [0, 8]],
'Y': [[6, 4, 8], [4, 1]],
'Z': [[6, 8, 0, 2]],
# Special
' ': [],
'.': [[1, 2]],
',': [[1, -2]],
'+': [[3, 5], [7, 1]],
'-': [[3, 5]],
'_': [[0, 2]],
'|': [[1, 7]],
'/': [[0, 8]],
'\\': [[6, 2]],
'\'': [[7, 4]],
'*': [[0, 8], [3, 5], [6, 2], [1, 7]],
'%': [[6, 3], [8, 0], [5, 2]],
'"': [[6, 4], [7, 5]],
'~': [[3, 7, 4, 8]],
'@': [[0, 6, 8, 2, 1, 4]],
'$': [[0, 1, 5, 3, 7, 8], [7, 1]],
'^': [[3, 7, 5]],
': ': [[4, 5], [1, 2]],
';': [[4, 5], [1, -3]],
# '&': [[2,3,6,7,4,3,0,5]],
# Pairs
'(': [[1, 3, 7]],
')': [[7, 5, 1]],
'[': [[7, 6, 0, 1]],
']': [[7, 8, 2, 1]],
'<': [[8, 3, 2]],
'>': [[6, 5, 0]],
# Numbers
'0': [[6, 8, 2, 0, 6], [0, 8]],
'1': [[0, 2], [1, 7, 6]],
'2': [[6, 7, 5, 3, 0, 2]],
'3': [[6, 8, 4, 2, 0]],
'4': [[6, 3, 5], [8, 2]],
'5': [[8, 6, 3, 5, 1, 0]],
'6': [[8, 7, 3, 0, 2, 5, 3]],
'7': [[3, 6, 8, 5, 1]],
'8': [[6, 2, 0, 8, 6]],
'9': [[5, 3, 6, 8, 5, 1, 0]],
# Unknown
'?': [[3, 6, 8, 5, 4, 1]]
}
for char in text:
if char in chars:
add_character(chars[char])
else:
# unknown character
add_character(chars['?'])
offset += 1
def is_valid(self):
if not self.gp:
return False
if not bpy.context.scene.grease_pencil or self.gp != bpy.context.scene.grease_pencil:
return False
if not self.gp_layer:
return False
if not self.gp_palette:
return False
return True
def setup_gp(self):
# Documentation https://wiki.blender.org/index.php/User:Antoniov/Grease_Pencil_Api_Changes
id_grease = "id_grease"
id_layer = "id_layer"
id_palette = "id_palette"
#
bpy.context.space_data.show_object_viewport_grease_pencil = True
# gp = scene.grease_pencil
# if not gp:
# gp = bpy.data.grease_pencils.get(gname, None)
# if not gp:
# gp = bpy.data.grease_pencils.new(gname)
# print("Created new Grease Pencil", gp.name)
# scene.grease_pencil = gp
# print("Added Grease Pencil %s to current scene" % (gp.name) )
# return gp
# Grease Pencil
self.gp = bpy.context.scene.grease_pencil
if not self.gp:
if id_grease in bpy.data.grease_pencils:
self.gp = bpy.data.grease_pencils.get(id_grease, None)
else:
self.gp = bpy.data.grease_pencils.new(id_grease)
bpy.context.scene.grease_pencil = self.gp
# Layer
if id_layer in self.gp.layers:
self.gp_layer = self.gp.layers[id_layer]
else:
self.gp_layer = self.gp.layers.new(id_layer, set_active=True)
self.gp_layer.color = (0, 0.8, 1)
# Frame
if len(self.gp_layer.frames) == 0:
self.gp_frame = self.gp_layer.frames.new(bpy.context.scene.frame_current)
else:
self.gp_frame = self.gp_layer.frames[0]
def get_gp_stroke(self, id_grease="fance", id_layer="lines", id_palette="colors"):
stroke = self.gp_frame.strokes.new()
stroke.display_mode = '3DSPACE'
stroke.line_width = 100
stroke.material_index = 0
return stroke