-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlatk_ml.py
1283 lines (1032 loc) · 47 KB
/
latk_ml.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import addon_utils
import bpy
import gpu
#import bgl
from mathutils import Vector, Matrix
import bmesh
import sys
import argparse
import cv2
import numpy as np
import platform
#import latk
from . import latk
from . latk import *
from . latk_tools import *
from . latk_mtl import *
from . latk_mesh import *
from . latk_draw import *
from . latk_rw import *
from . latk_svg import *
#from . latk_binvox import *
from skimage.morphology import skeletonize
from mathutils import Vector, Quaternion
from collections import namedtuple
import random
import itertools
import h5py
import skeletor as sk
import trimesh
from scipy.spatial.distance import cdist
from scipy.spatial import Delaunay
from scipy.spatial import cKDTree
import scipy.ndimage as nd
from pyntcloud import PyntCloud
import pandas as pd
import pdb
import networkx as nx
import jellyfish
from random import uniform as rnd
from . growing_neural_gas.neuralgas import *
platform_system = platform.system().lower()
platform_processor = platform.processor().lower()
platform_machine = platform.machine().lower()
if (sys.version_info.major == 3):
if (sys.version_info.minor == 10):
if (platform_system == "windows"):
if (platform_processor.startswith("intel64")):
from . skeleton_tracing.swig.bin.python310.win.x64.trace_skeleton import *
elif (platform_system == "linux"):
if (platform_processor == "x86_64"):
from . skeleton_tracing.swig.bin.python310.linux.x64.trace_skeleton import *
elif (platform_processor == "aarch64"):
from . skeleton_tracing.swig.bin.python310.linux.arm.trace_skeleton import *
elif (platform_system == "darwin"):
if (platform_processor == "arm"):
from . skeleton_tracing.swig.bin.python310.mac.arm.trace_skeleton import *
elif (platform_processor == "i386"):
from . skeleton_tracing.swig.bin.python310.mac.x64.trace_skeleton import *
elif (sys.version_info.minor == 11):
if (platform_system == "windows"):
if (platform_processor.startswith("intel64")):
from . skeleton_tracing.swig.bin.python311.win.x64.trace_skeleton import *
elif (platform_system == "linux"):
if (platform_processor == "x86_64"):
from . skeleton_tracing.swig.bin.python311.linux.x64.trace_skeleton import *
elif (platform_processor == "aarch64"):
from . skeleton_tracing.swig.bin.python311.linux.arm.trace_skeleton import *
elif (platform_system == "darwin"):
if (platform_processor == "arm"):
from . skeleton_tracing.swig.bin.python311.mac.arm.trace_skeleton import *
elif (platform_processor == "i386"):
from . skeleton_tracing.swig.bin.python311.mac.x64.trace_skeleton import *
else:
from . skeleton_tracing.py.trace_skeleton import *
else:
from . skeleton_tracing.py.trace_skeleton import *
'''
try:
from . skeleton_tracing.swig.trace_skeleton import *
except Exception as error:
print(error)
from . skeleton_tracing.py.trace_skeleton import *
'''
#from . import binvox_rw
from . vox2vox import binvox_rw
from . latk_onnx import *
from . latk_pytorch import *
def findAddonPath(name=None):
#if not name:
#name = __name__
for mod in addon_utils.modules():
if mod.bl_info["name"] == name:
url = mod.__file__
return os.path.dirname(url)
return None
def getModelPath(name, url):
return os.path.join(findAddonPath(name), url)
def group_points_into_strokes(points, radius, minPointsCount):
strokeGroups = []
unassigned_points = set(range(len(points)))
while len(unassigned_points) > 0:
strokeGroup = [next(iter(unassigned_points))]
unassigned_points.remove(strokeGroup[0])
for i in range(len(points)):
if i in unassigned_points and cdist([points[i]], [points[strokeGroup[-1]]])[0][0] < radius:
strokeGroup.append(i)
unassigned_points.remove(i)
if (len(strokeGroup) >= minPointsCount):
strokeGroup = list(dict.fromkeys(strokeGroup)) # remove duplicates
strokeGroups.append(strokeGroup)
print("Found " + str(len(strokeGroups)) + " strokeGroups, " + str(len(unassigned_points)) + " points remaining.")
return strokeGroups
def checkForMesh(verts, faces):
mesh = None
try:
mesh = trimesh.Trimesh(verts, faces)
except:
tri = Delaunay(verts)
mesh = trimesh.Trimesh(tri.points, tri.simplices)
return mesh
def neuralGasGen(verts, colors=None, matrix_world=None, max_neurons=100000, max_iter=100, max_age=10, eb=0.1, en=0.006, alpha=0.5, beta=0.995, l=20):
latk_settings = bpy.context.scene.latk_settings
origCursorLocation = bpy.context.scene.cursor.location
bpy.context.scene.cursor.location = (0.0, 0.0, 0.0)
gp = getActiveGp()
layer = getActiveLayer()
if not layer:
layer = gp.data.layers.new(name="meshToGp")
frame = getActiveFrame()
if not frame or frame.frame_number != currentFrame():
frame = layer.frames.new(currentFrame())
# 1. Generate GNG
gas = GrowingNeuralGas(verts, max_neurons=max_neurons, max_iter=max_iter, max_age=max_age, eb=eb, en=en, alpha=alpha, beta=beta, l=l)
gas.learn()
# 2. get edge indices
edgeIndices = []
for edge in gas.gng.es:
edgeIndices.append((edge.source, edge.target))
# 3. merge edges with matching indices
reps = 10
for i in range(0, reps):
newEdgeIndices = []
while edgeIndices:
edge = edgeIndices.pop(0)
for j, matchEdge in enumerate(edgeIndices):
if (edge[1] == matchEdge[0]):
newEdge = edgeIndices.pop(j)
edge = edge + newEdge
break
elif (edge[0] == matchEdge[1]):
newEdge = edgeIndices.pop(j)
edge = newEdge + edge
break
edge = list(dict.fromkeys(edge)) # this removes repeated indices
#edge.sort()
newEdgeIndices.append(edge)
edgeIndices = newEdgeIndices
# 4. Get points from indices
edgeList = []
for edge in edgeIndices:
points = []
for index in edge:
points.append(gas.gng.vs[index]["weight"])
#points = sorted(points, key=lambda point: distance(point, points[0]))
edgeList.append(points)
allPoints = []
for edge in edgeList:
for point in edge:
if matrix_world:
point = matrix_world @ Vector(point)
allPoints.append(point)
strokeColors = transferVertexColors(verts, colors, allPoints)
strokeColorCounter = 0
for edge in edgeList:
stroke = frame.strokes.new()
stroke.display_mode = '3DSPACE'
stroke.line_width = int(latk_settings.thickness2) #10 # adjusted from 100 for 2.93
stroke.material_index = gp.active_material_index
stroke.points.add(len(edge))
for i, point in enumerate(edge):
#point = matrixWorldInverted @ Vector((point[0], point[2], point[1]))
#point = (point[0], point[1], point[2])
if matrix_world:
point = matrix_world @ Vector(point)
pressure = 1.0
strength = 1.0
createPoint(stroke, i, point, pressure, strength, strokeColors[strokeColorCounter])
strokeColorCounter += 1
bpy.context.scene.cursor.location = origCursorLocation
bpy.data.grease_pencils[gp.name].stroke_depth_order = "3D"
return gp
def neuralGasGen2(verts, colors=None, matrix_world=None, max_neurons=100000, max_iter=100, max_age=10, eb=0.1, en=0.006, alpha=0.5, beta=0.995, l=20, radius=2, minPointsCount=5):
latk_settings = bpy.context.scene.latk_settings
origCursorLocation = bpy.context.scene.cursor.location
bpy.context.scene.cursor.location = (0.0, 0.0, 0.0)
gp = getActiveGp()
layer = getActiveLayer()
if not layer:
layer = gp.data.layers.new(name="meshToGp")
frame = getActiveFrame()
if not frame or frame.frame_number != currentFrame():
frame = layer.frames.new(currentFrame())
gas = GrowingNeuralGas(verts, max_neurons=max_neurons, max_iter=max_iter, max_age=max_age, eb=eb, en=en, alpha=alpha, beta=beta, l=l)
gas.learn()
verts = []
for vert in gas.gng.vs:
verts.append(vert["weight"])
strokeGroups = group_points_into_strokes(verts, radius, minPointsCount)
lastColor = (1,1,1,1)
for strokeGroup in strokeGroups:
strokeColors = []
for i in range(0, len(strokeGroup)):
try:
newColor = colors[strokeGroup[i]]
strokeColors.append(newColor)
lastColor = newColor
except:
strokeColors.append((0,1,0,1)) #lastColor)
stroke = frame.strokes.new()
stroke.display_mode = '3DSPACE'
stroke.line_width = int(latk_settings.thickness2) #10 # adjusted from 100 for 2.93
stroke.material_index = gp.active_material_index
stroke.points.add(len(strokeGroup))
for i, strokeIndex in enumerate(strokeGroup):
if not matrix_world:
point = verts[strokeIndex]
else:
point = matrix_world @ Vector(verts[strokeIndex])
#point = matrixWorldInverted @ Vector((point[0], point[2], point[1]))
#point = (point[0], point[1], point[2])
pressure = 1.0
strength = 1.0
createPoint(stroke, i, point, pressure, strength, strokeColors[i])
bpy.context.scene.cursor.location = origCursorLocation
bpy.data.grease_pencils[gp.name].stroke_depth_order = "3D"
return gp
def similar2(a, b):
return jellyfish.jaro_distance(a, b)
def neuralGasGen3(verts, colors=None, matrix_world=None, max_neurons=100000, max_iter=100, max_age=10, eb=0.1, en=0.006, alpha=0.5, beta=0.995, l=20, radius=2, minPointsCount=5):
latk_settings = bpy.context.scene.latk_settings
origCursorLocation = bpy.context.scene.cursor.location
bpy.context.scene.cursor.location = (0.0, 0.0, 0.0)
gp = getActiveGp()
layer = getActiveLayer()
if not layer:
layer = gp.data.layers.new(name="meshToGp")
frame = getActiveFrame()
if not frame or frame.frame_number != currentFrame():
frame = layer.frames.new(currentFrame())
gas = GrowingNeuralGas(verts, max_neurons=max_neurons, max_iter=max_iter, max_age=max_age, eb=eb, en=en, alpha=alpha, beta=beta, l=l)
gas.learn()
verts = []
for vert in gas.gng.vs:
verts.append(vert["weight"])
edges = []
for edge in gas.gng.es:
edges.append((edge.source, edge.target))
lengths = []
for edge in edges:
lengths.append(getDistance(verts[edge[0]], verts[edge[1]]))
print(str(len(verts)) + ", " + str(len(edges)) + ", " + str(len(lengths)))
g = nx.Graph()
for edge, L in zip(edges, lengths):
g.add_edge(*edge, length=L)
strokeCounter = 0
similarityScores = []
numStrokes = 100
minStrokePoints = 10
maxStrokePoints = 9999
maxSimilarity = 0.8
while strokeCounter < numStrokes:
points = []
similarity = []
start = int(rnd(0, len(verts) - 2))
end = start + 1
try:
# run the shortest path query using length for edge weight
path = nx.shortest_path(g, source=start, target=end, weight='length')
for index in path:
vert = verts[index]
similarity.append(str(index))
#col = mesh.visual.vertex_colors[index]
#col = (float(col[0])/255.0, float(col[1])/255.0, float(col[2])/255.0, float(col[3])/255.0)
# TODO find out why colors are too light
#col = (col[0] * col[0], col[1] * col[1], col[2] * col[2], col[3])
col = (1,1,1,1)
newVert = (-vert[0], vert[2], vert[1])
if matrix_world:
newVert = matrix_world @ Vector(newVert)
pressure = 1.0
strength = 1.0
lp = latk.LatkPoint(co=newVert, pressure=pressure, strength=strength, col=col)
points.append(lp)
if (len(points) >= maxStrokePoints):
break
except:
print("Skipped stroke: No path from start to end.")
pass
if (len(points) >= minStrokePoints):
readyToAdd = True
similarityString = ' '.join(map(str, similarity))
if (checkSimilarity == True):
for score in similarityScores:
similarityTest = similar2(score, similarityString)
if (similarityTest > maxSimilarity):
readyToAdd = False
print("Skipped stroke: Failed similarity test (" + str(similarityTest) + ").")
break
if (readyToAdd):
similarityScores.append(similarityString)
stroke = frame.strokes.new()
stroke.display_mode = '3DSPACE'
stroke.line_width = int(latk_settings.thickness2) #10 # adjusted from 100 for 2.93
stroke.material_index = gp.active_material_index
stroke.points = points
strokeCounter += 1
print("Added stroke " + str(strokeCounter) + " / " + str(numStrokes) + ".")
bpy.context.scene.cursor.location = origCursorLocation
bpy.data.grease_pencils[gp.name].stroke_depth_order = "3D"
return gp
def strokeGen(verts, colors, matrix_world=None, radius=2, minPointsCount=5): #, origin=None): #limitPalette=32):
latk_settings = bpy.context.scene.latk_settings
origCursorLocation = bpy.context.scene.cursor.location
bpy.context.scene.cursor.location = (0.0, 0.0, 0.0)
gp = getActiveGp()
layer = getActiveLayer()
if not layer:
layer = gp.data.layers.new(name="meshToGp")
frame = getActiveFrame()
if not frame or frame.frame_number != currentFrame():
frame = layer.frames.new(currentFrame())
strokeGroups = group_points_into_strokes(verts, radius, minPointsCount)
lastColor = (1,1,1,1)
for strokeGroup in strokeGroups:
strokeColors = []
for i in range(0, len(strokeGroup)):
try:
newColor = colors[strokeGroup[i]]
strokeColors.append(newColor)
lastColor = newColor
except:
strokeColors.append((0,1,0,1)) #lastColor)
'''
if (limitPalette == 0):
createColor(color)
else:
createAndMatchColorPalette(color, limitPalette, 5) # num places
'''
stroke = frame.strokes.new()
stroke.display_mode = '3DSPACE'
stroke.line_width = int(latk_settings.thickness2) #10 # adjusted from 100 for 2.93
stroke.material_index = gp.active_material_index
stroke.points.add(len(strokeGroup))
for i, strokeIndex in enumerate(strokeGroup):
if not matrix_world:
point = verts[strokeIndex]
else:
point = matrix_world @ Vector(verts[strokeIndex])
#point = matrixWorldInverted @ Vector((point[0], point[2], point[1]))
#point = (point[0], point[1], point[2])
pressure = 1.0
strength = 1.0
createPoint(stroke, i, point, pressure, strength, strokeColors[i])
bpy.context.scene.cursor.location = origCursorLocation
bpy.data.grease_pencils[gp.name].stroke_depth_order = "3D"
return gp
def contourGen(verts, faces, matrix_world):
latk_settings = bpy.context.scene.latk_settings
origCursorLocation = bpy.context.scene.cursor.location
bpy.context.scene.cursor.location = (0.0, 0.0, 0.0)
la = latk.Latk(init=True)
gp = getActiveGp()
layer = getActiveLayer()
if not layer:
layer = gp.data.layers.new(name="meshToGp")
frame = getActiveFrame()
if not frame or frame.frame_number != currentFrame():
frame = layer.frames.new(currentFrame())
mesh = checkForMesh(verts, faces)
bounds = getDistance(mesh.bounds[0], mesh.bounds[1])
# generate a set of contour lines at regular intervals
interval = bounds * 0.01 #0.03 #0.1 # the spacing between contours
print("Interval: " + str(interval))
# x, z
slice_range = np.arange(mesh.bounds[0][2], mesh.bounds[1][2], interval)
# y
#slice_range = np.arange(mesh.bounds[0][1], mesh.bounds[0][2], interval)
# loop over the z values and generate a contour at each level
for slice_pos in slice_range:
# x
#slice_mesh = mesh.section(plane_origin=[slice_pos, 0, 0], plane_normal=[1, 0, 0])
# y
#slice_mesh = mesh.section(plane_origin=[0, slice_pos, 0], plane_normal=[0, 1, 0])
# z
slice_mesh = mesh.section(plane_origin=[0, 0, slice_pos], plane_normal=[0, 0, 1])
if slice_mesh != None:
for entity in slice_mesh.entities:
stroke = frame.strokes.new()
stroke.display_mode = '3DSPACE'
stroke.line_width = int(latk_settings.thickness2) #10 # adjusted from 100 for 2.93
stroke.material_index = gp.active_material_index
stroke.points.add(len(entity.points))
for i, index in enumerate(entity.points):
vert = None
if not matrix_world:
vert = slice_mesh.vertices[index]
else:
vert = matrix_world @ Vector(slice_mesh.vertices[index])
#vert = [vert[0], vert[1], vert[2]]
createPoint(stroke, i, vert, 1.0, 1.0)
#fromLatkToGp(la, resizeTimeline=False)
#setThickness(latk_settings.thickness)
bpy.context.scene.cursor.location = origCursorLocation
bpy.data.grease_pencils[gp.name].stroke_depth_order = "3D"
return gp
def skelGen(verts, faces, matrix_world):
latk_settings = bpy.context.scene.latk_settings
origCursorLocation = bpy.context.scene.cursor.location
bpy.context.scene.cursor.location = (0.0, 0.0, 0.0)
la = latk.Latk(init=True)
gp = getActiveGp()
layer = getActiveLayer()
if not layer:
layer = gp.data.layers.new(name="meshToGp")
frame = getActiveFrame()
if not frame or frame.frame_number != currentFrame():
frame = layer.frames.new(currentFrame())
mesh = checkForMesh(verts, faces)
fixed = sk.pre.fix_mesh(mesh, remove_disconnected=5, inplace=False)
skel = sk.skeletonize.by_wavefront(fixed, waves=1, step_size=1)
for entity in skel.skeleton.entities:
stroke = frame.strokes.new()
stroke.display_mode = '3DSPACE'
stroke.line_width = int(latk_settings.thickness2) #10 # adjusted from 100 for 2.93
stroke.material_index = gp.active_material_index
stroke.points.add(len(entity.points))
for i, index in enumerate(entity.points):
vert = None
if not matrix_world:
vert = skel.vertices[index]
else:
vert = matrix_world @ Vector(skel.vertices[index])
createPoint(stroke, i, vert, 1.0, 1.0)
#fromLatkToGp(la, resizeTimeline=False)
#setThickness(latk_settings.thickness)
bpy.context.scene.cursor.location = origCursorLocation
bpy.data.grease_pencils[gp.name].stroke_depth_order = "3D"
return gp
def differenceEigenvalues(verts): #, k_n=50, thresh=0.03):
latk_settings = bpy.context.scene.latk_settings
# MIT License Copyright (c) 2015 Dena Bazazian Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
pdVerts = pd.DataFrame(verts, columns=["x", "y", "z"])
pcd1 = PyntCloud(pdVerts)
# define hyperparameters
k_n = latk_settings.edges_k_n # 50
thresh = latk_settings.edges_thresh # 0.03
pcd_np = np.zeros((len(pcd1.points), 6))
# find neighbors
kdtree_id = pcd1.add_structure("kdtree")
k_neighbors = pcd1.get_neighbors(k=k_n, kdtree=kdtree_id)
# calculate eigenvalues
ev = pcd1.add_scalar_field("eigen_values", k_neighbors=k_neighbors)
x = pcd1.points['x'].values
y = pcd1.points['y'].values
z = pcd1.points['z'].values
e1 = pcd1.points['e3('+str(k_n+1)+')'].values
e2 = pcd1.points['e2('+str(k_n+1)+')'].values
e3 = pcd1.points['e1('+str(k_n+1)+')'].values
sum_eg = np.add(np.add(e1,e2),e3)
sigma = np.divide(e1,sum_eg)
sigma_value = sigma
# visualize the edges
sigma = sigma>thresh
# Save the edges and point cloud
thresh_min = sigma_value < thresh
sigma_value[thresh_min] = 0
thresh_max = sigma_value > thresh
sigma_value[thresh_max] = 255
pcd_np[:,0] = x
pcd_np[:,1] = y
pcd_np[:,2] = z
pcd_np[:,3] = sigma_value
edge_np = np.delete(pcd_np, np.where(pcd_np[:,3] == 0), axis=0)
print(len(edge_np))
clmns = ['x','y','z','red','green','blue']
#pcd_pd = pd.DataFrame(data=pcd_np,columns=clmns)
#pcd_pd['red'] = sigma_value.astype(np.uint8)
#pcd_points = PyntCloud(pcd_pd)
#edge_points = PyntCloud(pd.DataFrame(data=edge_np,columns=clmns))
#PyntCloud.to_file(edge_points, outputPath) # Save just the edge points
newVerts = []
#for i in range(0, len(edge_points.points)):
# newVerts.append((edge_points.points["x"][i], edge_points.points["y"][i], edge_points.points["z"][i]))
for edge in edge_np:
newVerts.append((edge[0], edge[1], edge[2]))
return newVerts
def strokeGen_orig(obj=None, strokeLength=1, strokeGaps=10.0, shuffleOdds=1.0, spreadPoints=0.1, limitPalette=32):
if not obj:
obj = ss()
mesh = obj.data
mat = obj.matrix_world
#~
gp = getActiveGp()
layer = getActiveLayer()
if not layer:
layer = gp.data.layers.new(name="meshToGp")
frame = getActiveFrame()
if not frame or frame.frame_number != currentFrame():
frame = layer.frames.new(currentFrame())
#~
images = None
try:
images = getUvImages()
except:
pass
#~
allPoints, allColors = getVertsAndColorsAlt(target=obj, useWorldSpace=True, useColors=True, useBmesh=False)
#~
pointSeqsToAdd = []
colorsToAdd = []
for i in range(0, len(allPoints), strokeLength):
color = None
if not images:
try:
color = allColors[i]
except:
color = getColorExplorer(obj, i)
else:
try:
color = getColorExplorer(obj, i, images)
except:
color = getColorExplorer(obj, i)
colorsToAdd.append(color)
#~
pointSeq = []
for j in range(0, strokeLength):
#point = allPoints[i]
try:
point = allPoints[i+j]
if (len(pointSeq) == 0 or getDistance(pointSeq[len(pointSeq)-1], point) < strokeGaps):
pointSeq.append(point)
except:
break
if (len(pointSeq) > 0):
pointSeqsToAdd.append(pointSeq)
for i, pointSeq in enumerate(pointSeqsToAdd):
color = colorsToAdd[i]
#createColor(color)
if (limitPalette == 0):
createColor(color)
else:
createAndMatchColorPalette(color, limitPalette, 5) # num places
#stroke = frame.strokes.new(getActiveColor().name)
#stroke.draw_mode = "3DSPACE"
stroke = frame.strokes.new()
stroke.display_mode = '3DSPACE'
stroke.line_width = 10 # adjusted from 100 for 2.93
stroke.material_index = gp.active_material_index
stroke.points.add(len(pointSeq))
if (random.random() < shuffleOdds):
random.shuffle(pointSeq)
for j, point in enumerate(pointSeq):
x = point[0] + (random.random() * 2.0 * spreadPoints) - spreadPoints
y = point[2] + (random.random() * 2.0 * spreadPoints) - spreadPoints
z = point[1] + (random.random() * 2.0 * spreadPoints) - spreadPoints
pressure = 1.0
strength = 1.0
createPoint(stroke, j, (x, y, z), pressure, strength)
def scale_numpy_array(arr, min_v, max_v):
new_range = (min_v, max_v)
max_range = max(new_range)
min_range = min(new_range)
scaled_unit = (max_range - min_range) / (np.max(arr) - np.min(arr))
return arr * scaled_unit - np.min(arr) * scaled_unit + min_range
def resizeVoxels(voxel, shape):
ratio = shape[0] / voxel.shape[0]
voxel = nd.zoom(voxel,
ratio,
order=1,
mode='nearest')
voxel[np.nonzero(voxel)] = 1.0
return voxel
def getAveragePositionObj(obj=None, applyTransforms=False):
if not obj:
obj = ss()
if (applyTransforms == True):
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
return getAveragePosition(obj.data.vertices, obj.matrix_world)
def getAveragePosition(verts, matrix_world=None):
returns = Vector((0,0,0))
for vert in verts:
if not matrix_world:
returns += Vector(vert)
else:
returns += matrix_world @ Vector(vert)
returns /= float(len(verts))
return returns
def transferVertexColors(sourceVerts, sourceColors, destVerts):
sourceVerts = np.array(sourceVerts)
sourceColors = np.array(sourceColors)
destVerts = np.array(destVerts)
tree = cKDTree(sourceVerts)
_, indices = tree.query(destVerts) #, k=1)
destColors = sourceColors[indices]
return destColors
def vertsToBinvox(verts, dims=256, doFilter=False, axis='xyz'):
shape = (dims, dims, dims)
data = np.zeros(shape, dtype=bool)
translate = (0, 0, 0)
scale = 1
axis_order = axis
bv = binvox_rw.Voxels(data, shape, translate, scale, axis_order)
verts = normalize(verts, minVal=0.0, maxVal=float(dims-1))
for vert in verts:
x = int(vert[0])
y = int(vert[1])
z = int(vert[2])
data[x][y][z] = True
if (doFilter == True):
for i in range(0, 1): # 1
nd.binary_dilation(bv.data.copy(), output=bv.data)
for i in range(0, 3): # 3
nd.sobel(bv.data.copy(), output=bv.data)
nd.median_filter(bv.data.copy(), size=4, output=bv.data) # 4
for i in range(0, 2): # 2
nd.laplace(bv.data.copy(), output=bv.data)
for i in range(0, 0): # 0
nd.binary_erosion(bv.data.copy(), output=bv.data)
return bv
'''
def binvoxToVerts(voxel, dims=256, axis='xyz'):
verts = []
for x in range(0, dims):
for y in range(0, dims):
for z in range(0, dims):
if (voxel.data[x][y][z] == True):
verts.append([x, y, z])
return verts
'''
def binvoxToH5(voxel, dims=256):
shape=(dims, dims, dims)
voxel_data = voxel.data.astype(float) #voxel.data.astype(np.float)
if shape is not None and voxel_data.shape != shape:
voxel_data = resize(voxel.data.astype(np.float64), shape)
return voxel_data
def h5ToBinvox(data, dims=256):
data = np.rint(data).astype(np.uint8)
shape = (dims, dims, dims) #data.shape
translate = [0, 0, 0]
scale = 1.0
axis_order = 'xzy'
return binvox_rw.Voxels(data, shape, translate, scale, axis_order)
def writeTempH5(data, url="output.im"):
url = os.path.join(bpy.app.tempdir, url)
f = h5py.File(url, 'w')
# more compression options: https://docs.h5py.org/en/stable/high/dataset.html
f.create_dataset('data', data=data, compression='gzip')
f.flush()
f.close()
def readTempH5(url="output.im"):
url = os.path.join(bpy.app.tempdir, url)
return h5py.File(url, 'r').get('data')[()]
def writeTempBinvox(data, dims=256, url="output.binvox"):
url = os.path.join(bpy.app.tempdir, url)
data = np.rint(data).astype(np.uint8)
shape = (dims, dims, dims) #data.shape
translate = [0, 0, 0]
scale = 1.0
axis_order = 'xzy'
voxel = binvox_rw.Voxels(data, shape, translate, scale, axis_order)
with open(url, 'bw') as f:
voxel.write(f)
def readTempBinvox(dims=256, axis='xyz', url="output.binvox"):
url = os.path.join(bpy.app.tempdir, url)
voxel = None
print("Reading from: " + url)
with open(url, 'rb') as f:
voxel = binvox_rw.read_as_3d_array(f, True) # fix coords
verts = []
for x in range(0, dims):
for y in range(0, dims):
for z in range(0, dims):
if (voxel.data[x][y][z] == True):
verts.append([z, y, x])
return verts
def npToCv(img):
return cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
def cvToNp(img):
return np.asarray(img)
def cvToBlender(img):
rgb_image = cv2.cvtColor(img, cv2.COLOR_BGRA2RGBA)
blender_image = bpy.data.images.new("Image", width=rgb_image.shape[1], height=rgb_image.shape[0])
pixels = np.flip(rgb_image.flatten())
blender_image.pixels.foreach_set(pixels)
blender_image.update()
return blender_image
def createTempOutputSettings(newFilename="render.png", newFormat="PNG"):
newFilepath = os.path.join(bpy.app.tempdir, newFilename)
oldFilepath = bpy.context.scene.render.filepath
oldFormat = bpy.context.scene.render.image_settings.file_format
bpy.context.scene.render.filepath = newFilepath
bpy.context.scene.render.image_settings.file_format = newFormat
return oldFilepath, oldFormat, newFilepath, newFormat
def restoreOldOutputSettings(oldFilepath, oldFormat):
bpy.context.scene.render.filepath = oldFilepath
bpy.context.scene.render.image_settings.file_format = oldFormat
def renderFrame(depthPass=False):
oldFilepath, oldFormat, newFilepath, newFormat = createTempOutputSettings()
if (depthPass == True):
setupDepthPass(newFilepath.split("." + newFormat)[0] + "_depth")
bpy.ops.render.render(write_still=True)
restoreOldOutputSettings(oldFilepath, oldFormat)
if (depthPass == True):
return os.path.join(newFilepath.split("." + newFormat)[0] + "_depth", "Image" + str(bpy.data.scenes['Scene'].frame_current).zfill(4) + "." + newFormat)
else:
return newFilepath
# https://www.saifkhichi.com/blog/blender-depth-map-surface-normals
def getDepthPassAlt():
"""Obtains depth map from Blender render.
:return: The depth map of the rendered camera view as a numpy array of size (H,W).
"""
z = bpy.data.images['Viewer Node']
w, h = z.size
dmap = np.array(z.pixels[:], dtype=np.float32) # convert to numpy array
dmap = np.reshape(dmap, (h, w, 4))[:,:,0]
dmap = np.rot90(dmap, k=2)
dmap = np.fliplr(dmap)
return dmap
# https://blender.stackexchange.com/questions/2170/how-to-access-render-result-pixels-from-python-script/23309#23309
# https://blender.stackexchange.com/questions/56967/how-to-get-depth-data-using-python-api
def setupDepthPass(url="/my_path/"):
# Set up rendering of depth map:
bpy.context.scene.use_nodes = True
tree = bpy.context.scene.node_tree
links = tree.links
#~
# clear default nodes
for n in tree.nodes:
tree.nodes.remove(n)
#~
# create input render layer node
rl = tree.nodes.new('CompositorNodeRLayers')
#~
map = tree.nodes.new(type="CompositorNodeMapValue")
# Size is chosen kind of arbitrarily, try out until you're satisfied with resulting depth map.
map.size = [0.08]
map.use_min = True
map.min = [0]
map.use_max = True
map.max = [255]
links.new(rl.outputs[2], map.inputs[0])
#~
invert = tree.nodes.new(type="CompositorNodeInvert")
links.new(map.outputs[0], invert.inputs[1])
#~
# The viewer can come in handy for inspecting the results in the GUI
depthViewer = tree.nodes.new(type="CompositorNodeViewer")
links.new(invert.outputs[0], depthViewer.inputs[0])
# Use alpha from input.
links.new(rl.outputs[1], depthViewer.inputs[1])
#~
# create a file output node and set the path
fileOutput = tree.nodes.new(type="CompositorNodeOutputFile")
fileOutput.base_path = url
links.new(invert.outputs[0], fileOutput.inputs[0])
def renderToCv(depthPass=False):
image_path = renderFrame(depthPass)
image = cv2.imread(image_path)
return cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
def renderToNp(depthPass=False):
image_path = renderFrame(depthPass)
width = bpy.context.scene.render.resolution_x
height = bpy.context.scene.render.resolution_y
image = bpy.data.images.load(image_path)
image_array = np.array(image.pixels[:])
image_array = image_array.reshape((height, width, 4))
image_array = np.flipud(image_array)
image_array = image_array[:, :, :3]
return image_array.astype(np.float32)
# ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
def loadModel003(name):
latk_settings = bpy.context.scene.latk_settings
returns = modelSelector003(name, latk_settings.Operation1)
return returns
def loadModel004(name):
latk_settings = bpy.context.scene.latk_settings
returns1 = modelSelector004(name, latk_settings.ModelStyle1)
returns2 = modelSelector004(name, latk_settings.ModelStyle2)
return returns1, returns2
def modelSelector003(name, modelName):
latk_settings = bpy.context.scene.latk_settings
modelName = modelName.lower()
latk_settings.dims = int(modelName.split("_")[0])
return Vox2Vox_PyTorch(name, os.path.join("model", modelName + ".pth"))
def modelSelector004(name, modelName):
modelName = modelName.lower()
latk_settings = bpy.context.scene.latk_settings
if (bpy.context.preferences.addons[name].preferences.Backend.lower() == "pytorch"):
if (modelName == "anime"):
return Informative_Drawings_PyTorch(name, os.path.join("checkpoints", "anime_style_netG_A_latest.pth"))
elif (modelName == "contour"):
return Informative_Drawings_PyTorch(name, os.path.join("checkpoints", "contour_style_netG_A_latest.pth"))
elif (modelName == "opensketch"):
return Informative_Drawings_PyTorch(name, os.path.join("checkpoints", "opensketch_style_netG_A_latest.pth"))
elif (modelName == "pxp_001"):
return Pix2Pix_PyTorch(name, os.path.join("checkpoints", "pix2pix002-001_60_net_G.pth"))
elif (modelName == "pxp_002"):