3131# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3232# POSSIBILITY OF SUCH DAMAGE.
3333# ----------------------------------------------------------------------------
34+ # See: http://paulbourke.net/dataformats/mtl/
35+
3436import pywavefront
3537from pathlib import Path , PureWindowsPath
3638import re
3739
40+ class TextureOptions :
41+
42+ def __init__ (self ):
43+ """Set up options with sane defaults"""
44+ self .name = "default"
45+ self .blendu = "on"
46+ self .blendv = "on"
47+ self .bm = 1.0
48+ self .boost = 0.0
49+ self .cc = "off"
50+ self .clamp = "off"
51+ self .imfchan = "l"
52+ self .mm = (0.0 , 1.0 )
53+ self .o = (0.0 , 0.0 , 0.0 )
54+ self .s = (1.0 , 1.0 , 1.0 )
55+ self .t = (0.0 , 0.0 , 0.0 )
56+ self .texres = None
57+
58+
59+ class TextureOptionsParser :
60+
61+ def __init__ (self , line ):
62+ self ._line = line
63+ self ._gen = None
64+ self ._options = TextureOptions ()
65+ self ._dispatch = {
66+ '-blendu' : self .parse_blendu ,
67+ '-blendv' : self .parse_blendv ,
68+ '-bm' : self .parse_bm ,
69+ '-boost' : self .parse_boost ,
70+ '-cc' : self .parse_cc ,
71+ '-clamp' : self .parse_clamp ,
72+ '-imfchan' : self .parse_imfchan ,
73+ '-mm' : self .parse_mm ,
74+ '-o' : self .parse_o ,
75+ '-s' : self .parse_s ,
76+ '-t' : self .parse_t ,
77+ '-texres' : self .parse_texres ,
78+ }
79+
80+ def parse (self ):
81+ def create_generator ():
82+ for t in self ._line .split ():
83+ yield t
84+
85+ self ._gen = create_generator ()
86+
87+ try :
88+ while True :
89+ item = next (self ._gen )
90+ func = self ._dispatch .get (item , None )
91+ if func :
92+ print (func )
93+ func ()
94+ else :
95+ self ._options .name = ' ' .join (list (self ._gen ))
96+ except StopIteration :
97+ pass
98+
99+ return self ._options
100+
101+ def parse_blendu (self ):
102+ """The -blendu option turns texture blending in the horizontal direction
103+ (u direction) on or off. The default is on.
104+ """
105+ self ._options .blendu = next (self ._gen )
106+
107+ def parse_blendv (self ):
108+ """The -blendv option turns texture blending in the vertical direction (v
109+ direction) on or off. The default is on.
110+ """
111+ self ._options .blendv = next (self ._gen )
112+
113+ def parse_bm (self ):
114+ """The -bm option specifies a bump multiplier"""
115+ self ._options .bm = float (next (self ._gen ))
116+
117+ def parse_boost (self ):
118+ """The -boost option increases the sharpness, or clarity, of mip-mapped
119+ texture files
120+ """
121+ self ._options .boost = float (next (self ._gen ))
122+
123+ def parse_cc (self ):
124+ """The -cc option turns on color correction for the texture"""
125+ self ._options .cc = next (self ._gen )
126+
127+ def parse_clamp (self ):
128+ """The -clamp option turns clamping on or off."""
129+ self ._options .clamp = next (self ._gen )
130+
131+ def parse_imfchan (self ):
132+ """The -imfchan option specifies the channel used to create a scalar or
133+ bump texture.
134+ """
135+ self ._options .imfchan = next (self ._gen )
136+
137+ def parse_mm (self ):
138+ """The -mm option modifies the range over which scalar or color texture
139+ values may vary
140+ """
141+ base = float (next (self ._gen ))
142+ gain = float (next (self ._gen ))
143+ self ._options .mm = base , gain
144+
145+ def parse_o (self ):
146+ """The -o option offsets the position of the texture map on the surface by
147+ shifting the position of the map origin.
148+ """
149+ u = float (next (self ._gen ))
150+ v = float (next (self ._gen ))
151+ w = float (next (self ._gen ))
152+ self ._options .o = u , v , w
153+
154+ def parse_s (self ):
155+ """The -s option scales the size of the texture pattern on the textured
156+ surface by expanding or shrinking the pattern
157+ """
158+ u = float (next (self ._gen ))
159+ v = float (next (self ._gen ))
160+ w = float (next (self ._gen ))
161+ self ._options .s = u , v , w
162+
163+ def parse_t (self ):
164+ """The -t option turns on turbulence for textures."""
165+ u = float (next (self ._gen ))
166+ v = float (next (self ._gen ))
167+ w = float (next (self ._gen ))
168+ self ._options .t = u , v , w
169+
170+ def parse_texres (self ):
171+ """The -texres option specifies the resolution of texture created when an
172+ image is used.
173+ """
174+ self ._options .imfchan = next (self ._gen )
175+
38176
39177class Texture :
40178 def __init__ (self , name , search_path ):
@@ -44,9 +182,11 @@ def __init__(self, name, search_path):
44182 name (str): The texture possibly with path as it appear in the material
45183 search_path (str): Absolute or relative path the texture might be located.
46184 """
47- self ._name = name
185+ # The parsed name from the material might contain options
186+ self ._options = TextureOptionsParser (name ).parse ()
187+ self ._name = self ._options .name
48188 self ._search_path = Path (search_path )
49- self ._path = Path (search_path , name )
189+ self ._path = Path (search_path , self . name )
50190
51191 # Unsed externally by visualization
52192 self .image = None
@@ -60,6 +200,11 @@ def name(self):
60200 def name (self , value ):
61201 self ._name = value
62202
203+ @property
204+ def options (self ) -> TextureOptions :
205+ """TextureOptions: Options for this texture"""
206+ return self ._options
207+
63208 def find (self , path = None ):
64209 """Find the texture in the configured search path
65210 By default a search will be done in the same directory as
0 commit comments