forked from johnhw/blender_knots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
knot_plugin.py
413 lines (318 loc) · 12.8 KB
/
knot_plugin.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
bl_info = {
"name": "ASCII Knots",
"description": "Import ASCII art .knt files.",
"author": "Shankar Sivarajan",
"version": (0, 0, 5),
"blender": (2, 93, 0),
"location": "File > Import-Export",
"category": "Import-Export"
}
### Knot parsing
from collections import defaultdict
char_dirs = {"^":(0,-1), "V":(0,1), ">":(1,0), "<":(-1,0), "O":(0,0)}
inv_dirs = {v:k for k,v in char_dirs.items()}
def nonempty(kmap, x, y):
return [(x_off, y_off)
for x_off,y_off in char_dirs.values()
if (x+x_off, y+y_off) in kmap]
# ^V>< move in this direction
# # invalid direction; this is an error
# U continue in current direction, but go underneath
# O stop; end of lead
# C check neighbours; must only be one neighbour not equal to current direction; take that
# L change label of lead (from the label map)
table = """
* O ^ V > <
O C # # # #
. # . . . .
V V # V U U
v V # V U U
> > U U > #
< < U U # <
- # U U > <
| # ^ V U U
/ # > < ^ V
\ # < > V ^
+ # C C C C
L # L L L L
"""
# compute the table of possibilities
follow_map = {}
for line in table.splitlines():
base_dirs = "O^V><"
if len(line)>1:
chars = line.split()
in_char,out_chars = chars[0], chars[1:]
for i, c in enumerate(base_dirs):
follow_map[(in_char, base_dirs[i])] = out_chars[i]
class KnotException(Exception):
pass
class Knot:
def parse_map(self, s):
# convert from the ascii string to a dictionary of coordinates
self.map = {}
self.inv_map = defaultdict(list)
self.labels = {}
self.crossovers = []
self.lead_map = defaultdict(list)
# represents a label object, that many
# cells might refer to
class Label:
def __init__(self):
self.label = ""
def append(self, c):
self.label += c
def mark_label(x,y):
self.map[(x,y)] = 'L'
self.inv_map['L'].append((x,y))
self.labels[(x,y)] = label
for y, line in enumerate(s.splitlines()):
mark_invalid = False # clear invalid area flag
for x, char in enumerate(line):
if not mark_invalid:
# label
if char=='[':
mark_invalid = True
label = Label() # new label object
mark_label(x,y)
elif not char.isspace():
self.map[(x,y)] = char
self.inv_map[char].append((x,y))
else:
if char==']':
# label finished; record it
mark_invalid = False
#self.labels[label_start] = line[label_start[0]+1:x]
mark_label(x,y)
else:
# mark these parts of the map as invalid
mark_label(x,y)
# and append to the label (reading left-to-right)
label.append(char)
def choose(self, x, y, dx, dy):
"""Return the neighbouring point that is going in a different direction,
or throw an error if there is none or more than one."""
neighbours = nonempty(self.map, x, y)
valid = [(vx, vy) for vx,vy in neighbours if not(vx==-dx and vy==-dy) and not(vx==0 and vy==0)]
if len(valid)<1:
self.raise_error(x,y, "No neighbour to turn to")
if len(valid)>1:
self.raise_error(x,y,"Ambiguous neighbour")
return valid[0]
def find_heads(self):
"""Find each starting head"""
heads = []
# add in digit characters
head_dirs = dict(char_dirs)
head_dirs.update({str(d):(0,0) for d in range(10)})
# handle both cases of V
head_dirs["v"] = head_dirs["V"]
# find all potential heads
for char, (x_off,y_off) in head_dirs.items():
locations = self.inv_map[char]
for x,y in locations:
if x_off==0 and y_off==0:
# undirected head
x_off, y_off = self.choose(x,y,0,0)
if char.isdigit():
# named head
heads.append((x,y,x_off,y_off,0,char))
else:
heads.append((x,y,x_off,y_off,0,""))
else:
# check if this is a head (no space beforehand)
prev_lead = self.map.get((x-x_off, y-y_off), None)
if prev_lead is None:
heads.append((x,y,x_off,y_off,0,""))
# sort by y then x
return sorted(heads, key=lambda x:(x[1], x[0]))
def print_error(self, x, y, msg=""):
"""Print out a message with an error about the knot being parsed"""
k = 3
n = 6
print(msg.center(n*2))
for i in range(-n,n+1):
for j in range(-n, n+1):
if (abs(j)==k and abs(i)<k) or (abs(j)<k and abs(i)==k):
print("@", end="")
else:
char = self.map.get((x+j,y+i))
char = char or " "
print(char, end="")
print()
raise KnotException(msg)
def raise_error(self, x, y, msg=""):
"""Print out a message with an error about the knot being parsed"""
k = 3
n = 6
str = [msg.center(n*2)]
for i in range(-n,n+1):
for j in range(-n, n+1):
if (abs(j)==k and abs(i)<k) or (abs(j)<k and abs(i)==k):
str.append("@")
else:
char = self.map.get((x+j,y+i))
char = char or " "
str.append(char)
str.append("\n")
raise KnotException("".join(str))
def print_map(self, kmap):
"""Print out the entire map, in canonical form."""
def vrange(ix):
vs = [pt[ix] for pt in kmap.keys()]
return min(vs), max(vs)
min_x, max_x = vrange(0)
min_y, max_y = vrange(1)
for i in range(min_y, max_y+1):
for j in range(min_x, max_x+1):
char = kmap.get((j,i))
char = char or " "
print(char, end="")
print()
def trace_leads(self):
heads = self.find_heads()
self.leads = []
self.over_map = defaultdict(list)
ix = 0
# trace each lead, starting from each head found
for head in heads:
lead = []
x,y,dx,dy,z,name = head
lead.append((x,y,dx,dy,z,name))
x,y = x+dx, y+dy
while (x,y) in self.map:
char = self.map.get((x,y))
# where we are going now (as a character)
dir_char = inv_dirs[(dx,dy)]
action = follow_map.get((char, dir_char))
# simple change of direction
if action in char_dirs:
dx, dy = char_dirs[action]
z = 0
elif action=='L':
# rename lead
name = self.labels[(x,y)].label
elif action=='U':
# dx,dy don't change
z = -1
self.crossovers.append((x,y))
elif action=='C':
dx, dy = self.choose(x,y,dx,dy)
z = 0
elif action=='.':
break
elif action=='#':
self.raise_error(x,y, "Invalid direction")
elif action is None:
self.raise_error(x,y,"Character %s unexpected"%char)
lead.append((x,y,dx,dy,z,name))
self.over_map[(x,y)].append((ix, dx, dy, z))
# record where we are in the lead map
self.lead_map[(x,y)].append((lead, ix))
ix += 1
x, y = x+dx, y+dy
self.leads.append(lead)
def show_lead(self, lead):
kmap = {}
for x,y,dx,dy,z,name in lead:
if dx==0 and (x,y) not in kmap:
kmap[(x,y)] = '|'
elif (x,y) not in kmap:
kmap[(x,y)] = '-'
elif (x,y) in kmap:
kmap[(x,y)] = '+'
self.raise_map(kmap)
def show_lead_directed(self, lead):
kmap = {}
for x,y,dx,dy,z,name in lead:
char = inv_dirs[(dx,dy)]
kmap[(x,y)] = char
self.print_map(kmap)
def show_all_leads_directed(self):
kmap = {}
for lead in self.leads:
for x,y,dx,dy,z,name in lead:
char = inv_dirs[(dx,dy)]
kmap[(x,y)] = char
self.print_map(kmap)
def __init__(self, s):
"""Take a string representation of a knot, and fill
in the knot data from the string."""
# generate map of characters
self.parse_map(s)
self.trace_leads()
def is_crossing(self, x, y):
print((x,y))
cross = self.over_map.get((x,y))
return cross is not None and len(cross)>1
### END knot parsing
### Blender Python interface
import bpy
from bpy_extras.io_utils import ImportHelper
from mathutils import Vector
class ImportKnot(bpy.types.Operator, ImportHelper):
bl_idname = "import_knot.knt"
bl_label = "Import Knot"
bl_description = "Import knot"
# bl_options = {'UNDO'}
filename_ext = ".knt", ".txt";
filepath: bpy.props.StringProperty(subtype="FILE_PATH")
filter_glob: bpy.props.StringProperty(
default="*.knt",
options={"HIDDEN"},
)
@classmethod
def poll(cls, context):
return True
def execute(self, context):
filename = self.filepath
with open(filename, "r") as knot_file:
knot_obj = Knot(knot_file.read())
ix = 0
verts = []
edges = []
bias = 0
z_scale = 1
if len(knot_obj.leads)<1:
raise KnotException("Warning: no valid knot found")
for lead in knot_obj.leads:
prev = None
for x,y,dx,dy,z,name in lead:
if knot_obj.is_crossing(x,y):
if z==-1:
verts.append(Vector((x,-y,-z_scale * (bias+1)/2)))
else:
verts.append(Vector((x,-y,z_scale* (bias+1)/2)))
else:
verts.append(Vector((x,-y,0)))
if prev is not None:
edges.append((prev, ix))
prev = ix
ix += 1
name = bpy.path.display_name_from_filepath(filename)
me = bpy.data.meshes.new(name)
me.from_pydata(verts, edges, [])
ob = bpy.data.objects.new(name, me)
col = bpy.context.collection
col.objects.link(ob)
bpy.context.view_layer.objects.active = ob
ob.select_set(True)
# First separate by loose parts, and iterate the following modifiers over each.
# ob.modifiers.new("Subsurface", type='SUBSURF')
# bpy.context.object.modifiers["Subsurface"].levels = 2
# ob.modifiers.new("Skin", type='SKIN')
# ob.modifiers.new("Subsurface", type='SUBSURF')
# bpy.context.object.modifiers["Subsurface"].levels = 1
return {'FINISHED'}
def draw(self, context):
pass
def menu_import(self, context):
self.layout.operator(ImportKnot.bl_idname, text="Knot (.knt)")
def register():
bpy.utils.register_class(ImportKnot)
bpy.types.TOPBAR_MT_file_import.append(menu_import)
def unregister():
bpy.utils.unregister_class(ImportKnot)
bpy.types.TOPBAR_MT_file_import.remove(menu_import)
if __name__ == "__main__":
register()