-
Notifications
You must be signed in to change notification settings - Fork 8
/
lithophane_utils.py
171 lines (120 loc) · 4.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
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
import sys
IS_PY_2 = sys.version_info.major < 3
import math
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(includeObject=False):
selection = FreeCADGui.Selection.getSelection()
if includeObject:
notFound = (None, None, None)
else:
notFound = (None, None)
if len(selection) != 1:
return notFound
selectedObject = selection[0]
if not hasattr(selectedObject, 'Proxy') or selectedObject.Proxy is None:
return notFound
if not hasattr(selectedObject.Proxy, 'isLithophaneImage') or not selectedObject.Proxy.isLithophaneImage:
return notFound
if includeObject:
return (selection[0].Proxy, selection[0].Label, selection[0])
else:
return (selection[0].Proxy, selection[0].Label)
def findSelectedMesh():
selection = FreeCADGui.Selection.getSelection()
if len(selection) != 1:
return (None, None)
selectedObject = selection[0]
if not hasattr(selectedObject, 'Mesh'):
return (None, None)
return (selection[0].Mesh, selection[0].Label)
def findSelectedBooleanMesh():
selection = FreeCADGui.Selection.getSelection()
notFound = (None, None)
if len(selection) < 1:
return notFound
for selectedObject in selection:
print(selectedObject)
if not hasattr(selectedObject, 'Proxy') or selectedObject.Proxy is None:
continue
if not hasattr(selectedObject.Proxy, 'isBooleanMesh') or not selectedObject.Proxy.isBooleanMesh:
continue
return (selectedObject.Proxy, selectedObject.Label)
return notFound
def findSelectedBaseFeature():
selection = FreeCADGui.Selection.getSelection()
if len(selection) < 1:
return None
for selectedObject in selection:
if hasattr(selectedObject, 'Mesh') and selectedObject.Mesh and isinstance(selectedObject.Mesh, Mesh.Mesh):
return selectedObject
if hasattr(selectedObject, 'Shape') and selectedObject.Shape:
return selectedObject
return None
def checkOpenscadInstalled():
found = True
try:
import OpenSCADUtils
found = OpenSCADUtils.getopenscadversion() is not None
except:
found = False
if not found:
qtutils.showInfo('OpenSCAD import failed', 'The import of OpenSCADUtils failed. Make sure you have OpenSCAD installed and configured in the preferences.')
return found
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''
byteList = []
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_2:
byteList.append(chr(value))
else:
byteList.append(chr(value).encode('latin-1'))
imageBytes = b''.join(byteList)
soImage.setValue(size, 1, imageBytes)
return soImage
def diameterFromPerimeter(perimeter):
return perimeter / math.pi
def perimeterFromDiameter(diameter):
return diameter * math.pi
if __name__ == '__main__':
imagePath = 'C:\\Meine Daten\\projects\\02_Lithophane\\Nachttischlampe_Rund\\Bauernhof.jpg'
image = qtutils.readImage(imagePath)
print(convertImageToTexture(image))