Skip to content

Commit 17c17b2

Browse files
committed
Separate texture name and path
The fact that we only store the absulte path to the texture creates a lot of problems for users.
1 parent c4d6183 commit 17c17b2

File tree

2 files changed

+53
-28
lines changed

2 files changed

+53
-28
lines changed

pywavefront/material.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -130,23 +130,23 @@ def set_specular(self, values=None):
130130
def set_emissive(self, values=None):
131131
self.emissive = self.pad_light(values or [])
132132

133-
def set_texture(self, path):
134-
self.texture = Texture(path)
133+
def set_texture(self, name, path=None):
134+
self.texture = Texture(name, path)
135135

136-
def set_texture_ambient(self, path):
137-
self.texture_ambient = Texture(path)
136+
def set_texture_ambient(self, name, path=None):
137+
self.texture_ambient = Texture(name, path)
138138

139-
def set_texture_specular_color(self, path):
140-
self.texture_specular_color = Texture(path)
139+
def set_texture_specular_color(self, name, path=None):
140+
self.texture_specular_color = Texture(name, path)
141141

142-
def set_texture_specular_highlight(self, path):
143-
self.texture_specular_highlight = Texture(path)
142+
def set_texture_specular_highlight(self, name, path=None):
143+
self.texture_specular_highlight = Texture(name, path)
144144

145-
def set_texture_alpha(self, path):
146-
self.texture_alpha = Texture(path)
145+
def set_texture_alpha(self, name, path=None):
146+
self.texture_alpha = Texture(name, path)
147147

148-
def set_texture_bump(self, path):
149-
self.texture_bump = Texture(path)
148+
def set_texture_bump(self, name, path=None):
149+
self.texture_bump = Texture(name, path)
150150

151151
def unset_texture(self):
152152
self.texture = None
@@ -210,38 +210,44 @@ def parse_Tr(self):
210210
@auto_consume
211211
def parse_map_Kd(self):
212212
"""Diffuse map"""
213-
Kd = os.path.join(self.dir, " ".join(self.values[1:]))
214-
self.this_material.set_texture(Kd)
213+
name = "".join(self.values[1:])
214+
path = os.path.join(self.dir, name)
215+
self.this_material.set_texture(name, path)
215216

216217
@auto_consume
217218
def parse_map_Ka(self):
218219
"""Ambient map"""
219-
Kd = os.path.join(self.dir, " ".join(self.values[1:]))
220-
self.this_material.set_texture_ambient(Kd)
220+
name = "".join(self.values[1:])
221+
path = os.path.join(self.dir, name)
222+
self.this_material.set_texture_ambient(name, path)
221223

222224
@auto_consume
223225
def parse_map_Ks(self):
224226
"""Specular color map"""
225-
Kd = os.path.join(self.dir, " ".join(self.values[1:]))
226-
self.this_material.set_texture_specular_color(Kd)
227+
name = "".join(self.values[1:])
228+
path = os.path.join(self.dir, name)
229+
self.this_material.set_texture_specular_color(name, path)
227230

228231
@auto_consume
229232
def parse_map_Ns(self):
230233
"""Specular color map"""
231-
Kd = os.path.join(self.dir, " ".join(self.values[1:]))
232-
self.this_material.set_texture_specular_highlight(Kd)
234+
name = "".join(self.values[1:])
235+
path = os.path.join(self.dir, name)
236+
self.this_material.set_texture_specular_highlight(name, path)
233237

234238
@auto_consume
235239
def parse_map_d(self):
236240
"""Alpha map"""
237-
Kd = os.path.join(self.dir, " ".join(self.values[1:]))
238-
self.this_material.set_texture_alpha(Kd)
241+
name = "".join(self.values[1:])
242+
path = os.path.join(self.dir, name)
243+
self.this_material.set_texture_alpha(name, path)
239244

240245
@auto_consume
241246
def parse_map_bump(self):
242247
"""Bump map"""
243-
Kd = os.path.join(self.dir, " ".join(self.values[1:]))
244-
self.this_material.set_texture_bump(Kd)
248+
name = "".join(self.values[1:])
249+
path = os.path.join(self.dir, name)
250+
self.this_material.set_texture_bump(name, path)
245251

246252
def parse_bump(self):
247253
self.parse_map_bump()

pywavefront/texture.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,39 @@
3535

3636

3737
class Texture(object):
38-
def __init__(self, path):
38+
def __init__(self, name, path=None):
3939
# Treat path as part of a file uri always using forward slashes
40-
self.path = path.replace('\\', os.path.sep)
40+
self._name = name
41+
self._path = path or name
42+
43+
# Unsed externtally by visualization
4144
self.image = None
4245

46+
@property
47+
def name(self):
48+
return self._name
49+
50+
@name.setter
51+
def name(self, value):
52+
self._name = value
53+
54+
@property
55+
def path(self):
56+
return self._path
57+
58+
@path.setter
59+
def path(self, value):
60+
self._path = value
61+
4362
@property
4463
def image_name(self):
4564
"""Wrap the old property name to not break compatibility"""
46-
return self.path
65+
return self._name
4766

4867
@image_name.setter
4968
def image_name(self, value):
5069
"""Wrap the old property name to not break compatibility"""
51-
self.path = value
70+
self._name = value
5271

5372
def exists(self):
5473
return os.path.exists(self.path)

0 commit comments

Comments
 (0)