forked from cuddlyogre/ExportLDraw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexmap.py
More file actions
175 lines (148 loc) · 6.83 KB
/
texmap.py
File metadata and controls
175 lines (148 loc) · 6.83 KB
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
import math
import mathutils
import uuid
import base64
import os
texmaps = []
texmap = None
def reset_caches():
global texmaps
global texmap
texmaps = []
texmap = None
# https://github.com/trevorsandy/lpub3d/blob/e7c39cd3df518cf16521dc2c057a9f125cc3b5c3/lclib/common/lc_meshloader.h#L56
# https://github.com/trevorsandy/lpub3d/blob/e7c39cd3df518cf16521dc2c057a9f125cc3b5c3/lclib/common/lc_meshloader.cpp#L12
# https://github.com/trevorsandy/lpub3d/blob/e7c39cd3df518cf16521dc2c057a9f125cc3b5c3/lclib/common/lc_meshloader.cpp#L1486
# https://stackoverflow.com/questions/53970131/how-to-find-the-clockwise-angle-between-two-vectors-in-python#53970746
class TexMap:
def __init__(self, method, parameters, texture, glossmap=''):
self.id = str(uuid.uuid4())
self.method = method
self.parameters = parameters
self.texture = texture
self.glossmap = glossmap
self.uvs = dict()
def uv_unwrap_face(self, bm, face):
if self.method in ['planar']:
self.map_planar(bm, face)
elif self.method in ['cylindrical']:
self.map_cylindrical(bm, face)
elif self.method in ['spherical']:
self.map_spherical(bm, face)
# negative v because blender uv starts at bottom left of image, LDraw orientation of up=-y so use top left
def map_planar(self, bm, face):
a = self.parameters[0]
b = self.parameters[1]
c = self.parameters[2]
ab = b - a
bc = c - b
ac = c - a
# texmap_cross = ab.cross(ac)
# texmap_normal = texmap_cross / texmap_cross.length
p1_length = ab.length
p1_normal = ab / p1_length
p2_length = ac.length
p2_normal = ac / p2_length
# https://blender.stackexchange.com/a/53808
# https://blender.stackexchange.com/questions/53709/bmesh-how-to-map-vertex-based-uv-coordinates-to-loops
# https://blenderartists.org/t/solved-how-to-uv-unwrap-and-scale-uvs-with-python-while-in-object-mode/1115953/2
# DISTANCE BETWEEN POINT AND PLANE
# https://stackoverflow.com/a/3863777
# float dist = dotProduct(p.normal, (vectorSubtract(point, p.point)));
# https://mathinsight.org/distance_point_plane
# absolute value of the dot product of the normal and
# the length between the point and a point on the plane
# TODO: UV PROJECT HERE
uv_layer = bm.loops.layers.uv.verify()
for loop in face.loops:
p = loop.vert.co
p_str = str(p)
if p_str not in self.uvs:
du = p1_normal.dot(p - a) / p1_length
dv = p2_normal.dot(p - c) / p2_length
# - up_length to move uv to bottom left in blender
uv = [du, -dv]
self.uvs[p_str] = uv
uv = self.uvs[p_str]
loop[uv_layer].uv = uv
def map_cylindrical(self, bm, face):
a = self.parameters[0]
b = self.parameters[1]
c = self.parameters[2]
angle1 = self.parameters[3]
up = a - b
up_length = up.length
front = (c - b).normalized()
plane_1_normal = up / up_length
plane_2_normal = front.cross(up).normalized()
front_plane = mathutils.Vector(tuple(front) + (-front.dot(b),))
up_length = up_length
plane_1 = mathutils.Vector(tuple(plane_1_normal) + (-plane_1_normal.dot(b),))
plane_2 = mathutils.Vector(tuple(plane_2_normal) + (-plane_2_normal.dot(b),))
angle_1 = 360.0 / angle1
uv_layer = bm.loops.layers.uv.verify()
for loop in face.loops:
p = loop.vert.co
p_str = str(p)
if p_str not in self.uvs:
# - up_length to move uv to bottom left in blender
dot_plane_1 = mathutils.Vector((p[0], p[1] - up_length, p[2],) + (1.0,)).dot(plane_1)
point_in_plane_1 = p - mathutils.Vector((plane_1[0], plane_1[1], plane_1[2],)) * dot_plane_1
dot_front_plane = point_in_plane_1.dot(front_plane)
dot_plane_2 = mathutils.Vector(tuple(point_in_plane_1) + (1.0,)).dot(plane_2)
_angle_1 = math.atan2(dot_plane_2, dot_front_plane) / math.pi * angle_1
du = self.clamp(0.5 + 0.5 * _angle_1, 0, 1)
dv = dot_plane_1 / up_length
uv = [du, -dv]
self.uvs[p_str] = uv
uv = self.uvs[p_str]
loop[uv_layer].uv = uv
def map_spherical(self, bm, face):
a = self.parameters[0]
b = self.parameters[1]
c = self.parameters[2]
angle1 = self.parameters[3]
angle2 = self.parameters[4]
front = (b - a).normalized()
plane_1_normal = front.cross(c - a).normalized()
plane_2_normal = plane_1_normal.cross(front).normalized()
front_plane = mathutils.Vector(tuple(front) + (-front.dot(a),))
center = a
plane_1 = mathutils.Vector(tuple(plane_1_normal) + (-plane_1_normal.dot(a),))
plane_2 = mathutils.Vector(tuple(plane_2_normal) + (-plane_2_normal.dot(a),))
angle_1 = 360.0 / angle1
angle_2 = 180.0 / angle2
uv_layer = bm.loops.layers.uv.verify()
for loop in face.loops:
p = loop.vert.co
p_str = str(p)
if p_str not in self.uvs:
vertex_direction = p - center
dot_plane_1 = mathutils.Vector((p[0], p[1], p[2],) + (1.0,)).dot(plane_1)
point_in_plane_1 = p - mathutils.Vector((plane_1[0], plane_1[1], plane_1[2],)) * dot_plane_1
dot_front_plane = point_in_plane_1.dot(front_plane)
dot_plane_2 = point_in_plane_1.dot(plane_2)
_angle_1 = math.atan2(dot_plane_2, dot_front_plane) / math.pi * angle_1
du = 0.5 + 0.5 * _angle_1
_angle_2 = math.asin(dot_plane_1 / vertex_direction.length) / math.pi * angle_2
# -0.5 instead of 0.5 to move uv to bottom left in blender
dv = -0.5 - _angle_2
uv = [du, -dv]
self.uvs[p_str] = uv
uv = self.uvs[p_str]
loop[uv_layer].uv = uv
def clamp(self, num, min_value, max_value):
return max(min(num, max_value), min_value)
# TODO: will be used for stud.io parts that have textures
# TexMap.base64_to_png(filename, img_data)
@staticmethod
def base64_to_png(filename, img_data):
if type(img_data) is str:
img_data = bytes(img_data.encode())
this_script_dir = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(this_script_dir, f"{filename}.png"), "wb") as fh:
fh.write(base64.decodebytes(img_data))
if __name__ == "__main__":
filename = 'test'
img_data = 'iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAC0lEQVQIHWNgQAcAABIAAYAUyswAAAAASUVORK5CYII='
TexMap.base64_to_png(filename, img_data)