-
Notifications
You must be signed in to change notification settings - Fork 8
/
lithophane_utils.py
96 lines (69 loc) · 2.41 KB
/
lithophane_utils.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
import sys
IS_PY_3 = sys.version_info.major == 3
import FreeCAD, FreeCADGui
import itertools, Mesh
from pivy import coin
import utils.qtutils as qtutils
def recomputeView():
FreeCAD.ActiveDocument.recompute()
FreeCADGui.updateGui()
FreeCADGui.activeDocument().activeView().viewAxonometric()
FreeCADGui.SendMsgToActiveView("ViewFit")
def findSelectedImage():
selection = FreeCADGui.Selection.getSelection()
if len(selection) != 1:
return (None, None)
selectedObject = selection[0]
if not hasattr(selectedObject, 'Proxy') or selectedObject.Proxy is None:
return (None, None)
if not hasattr(selectedObject.Proxy, 'isLithophaneImage') or not selectedObject.Proxy.isLithophaneImage:
return (None, None)
return (selection[0].Proxy, selection[0].Label)
def findSelectedMesh():
selection = FreeCADGui.Selection.getSelection()
if len(selection) != 1:
return None
selectedObject = selection[0]
if not hasattr(selectedObject, 'Mesh'):
return None
return selection[0].Mesh, selection[0].Label
def vectorAtGround(vector):
return FreeCAD.Vector(vector.x, vector.y, 0)
def toChunks(iterable, chunksize):
"""
Splits the iterable into evenly sized chunks. The last chunk can be smaller when iterable contains not enough elements
"""
i = iter(iterable)
while True:
wrapped_chunk = [list(itertools.islice(i, int(chunksize)))]
if not wrapped_chunk[0]:
break
yield wrapped_chunk.pop()
def vectorToTuple(vector):
return (vector.x, vector.y, vector.z)
def tupleToVector(t):
return FreeCAD.Vector(t[0], t[1], t[2])
def convertImageToTexture(image):
size = coin.SbVec2s(image.width(), image.height())
buffersize = image.byteCount()
soImage = coin.SoSFImage()
width = size[0]
height = size[1]
imageBytes = b''
for y in range(height -1, -1, -1):
for x in range(width):
rgb = image.pixel(x,y)
color = qtutils.QColor()
color.setRgba(rgb)
alpha = color.alpha()
value = 0
if alpha < 255:
value = 254 - alpha
else:
value = color.lightness()
if IS_PY_3:
imageBytes = imageBytes + chr(value).encode('latin-1')
else:
imageBytes = imageBytes + chr(value)
soImage.setValue(size, 1, imageBytes)
return soImage