Skip to content

Commit 22dd1e8

Browse files
author
Kurt Yoder
authored
Merge pull request #59 from einarf/texture-loading
Texture loading improvements
2 parents 1984ee3 + b7b79e4 commit 22dd1e8

File tree

5 files changed

+22
-11
lines changed

5 files changed

+22
-11
lines changed

pywavefront/texture.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,22 @@
3333
# ----------------------------------------------------------------------------
3434
import os
3535

36-
from pywavefront.exceptions import PywavefrontException
37-
3836

3937
class Texture(object):
4038
def __init__(self, path):
41-
self.image_name = path
39+
# Treat path as part of a file uri always using forward slashes
40+
self.path = path.replace('\\', os.path.sep)
4241
self.image = None
4342

44-
if not os.path.exists(path):
45-
raise PywavefrontException("Requested file does not exist")
43+
@property
44+
def image_name(self):
45+
"""Wrap the old property name to not break compatibility"""
46+
return self.path
47+
48+
@image_name.setter
49+
def image_name(self, value):
50+
"""Wrap the old property name to not break compatibility"""
51+
self.path = value
52+
53+
def exists(self):
54+
return os.path.exists(self.path)

pywavefront/visualization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def gl_light(lighting):
119119
def bind_texture(texture):
120120
"""Draw a single texture"""
121121
if not getattr(texture, 'image', None):
122-
texture.image = load_image(texture.image_name)
122+
texture.image = load_image(texture.path)
123123

124124
glEnable(texture.image.target)
125125
glBindTexture(texture.image.target, texture.image.id)

test/test_material.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ def testSetEmissive(self):
6060

6161
class TestInvalidMaterial(unittest.TestCase):
6262

63-
def testSetInvalidTexture(self):
63+
def test_missing_texture(self):
6464
"""Running set_texture with a nonexistent file should raise an exception."""
6565
material = pywavefront.material.Material('material')
66-
self.assertRaises(Exception, material.set_texture, 'missing.file.do.not.create')
66+
material.set_texture('missing.file.do.not.create')
67+
self.assertFalse(material.texture.exists())

test/test_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def testMtlSpecular(self):
175175
def testMtlTextureName(self):
176176
"""Parsing an obj file with known material texture should set its name."""
177177
# also tests d
178-
self.assertEqual(self.material1.texture.image_name,
178+
self.assertEqual(self.material1.texture.path,
179179
prepend_dir('4x4.png'))
180180

181181

test/test_texture.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ class TestTexture(unittest.TestCase):
1313
def testPathedImageName(self):
1414
"""For Texture objects, the image name should be the last component of the path."""
1515
my_texture = pywavefront.texture.Texture(prepend_dir('4x4.png'))
16-
self.assertEqual(my_texture.image_name, prepend_dir('4x4.png'))
16+
self.assertEqual(my_texture.path, prepend_dir('4x4.png'))
1717

1818
def testMissingFile(self):
1919
"""Referencing a missing texture file should raise an exception."""
20-
self.assertRaises(Exception, pywavefront.texture.Texture, 'missing.file.do.not.create')
20+
texture = pywavefront.texture.Texture('missing.file.do.not.create')
21+
self.assertFalse(texture.exists())

0 commit comments

Comments
 (0)