4.19. objmtl — Reading Wavefront OBJ/MTL files
This module contains the OBJReader and MTLReader classes which
can be used as a base class for reading Wavefront OBJ and MTL files. The classes
read the files and invoke callback methods with the corresponding data in the
file. Derived classes have to implement those callback methods and process the
data as appropriate.
4.19.1. OBJReader class
The OBJReader class reads Wavefront OBJ files and calls appropriate
methods which have to be implemented in a derived class.
-
class cgkit.objmtl.OBJReader
- Creates an instance of the reader.
-
OBJReader.read(f)
- Read the content of a file. f must be a file like object that can be used to
read the content of the file.
-
OBJReader.begin()
- Callback method that is called before the file is read.
-
OBJReader.end()
- Callback method that is called after the file was read.
-
OBJReader.handleUnknown(cmd, arglist)
- This method is called when a keyword is encountered that has no corresponding
handler method.
The following are the predefined handler methods:
-
OBJReader.call(filename, *args)
-
OBJReader.csh(cmd)
-
OBJReader.mtllib(*files)
-
OBJReader.usemtl(name)
-
OBJReader.g(*groups)
-
OBJReader.s(groupnumber)
-
OBJReader.v(vert)
-
OBJReader.vp(vert)
-
OBJReader.vn(normal)
-
OBJReader.vt(tvert)
-
OBJReader.f(*verts)
-
OBJReader.o(name)
-
OBJReader.bevel(onoff)
-
OBJReader.c_interp(onoff)
-
OBJReader.d_interp(onoff)
-
OBJReader.lod(level)
-
OBJReader.shadow_obj(filename)
-
OBJReader.trace_obj(filename)
4.19.2. MTLReader class
-
class cgkit.objmtl.MTLReader
-
read(f)
- Read the content of a file. f must be a file like object that can be used to
read the content of the file.
-
begin()
- Callback method that is called before the file is read.
-
end()
- Callback method that is called after the file was read.
-
handleUnknown(cmd, arglist)
- This method is called when a keyword is encountered that has no corresponding
handler method.
The following are the predefined handler methods:
-
newmtl(name)
- Start of a new material definition.
-
illum(model)
- Illumination model.
-
Ka(col)
- Ambient color.
-
Kd(col)
- Diffuse color.
-
Ks(col)
- Specular color.
-
Ke(col)
- Emissive color.
-
Ns(shininess)
-
Ni(ref)
- Refraction index.
-
Tr(alpha)
- Transparency.
-
d(alpha)
- Transparency.
-
Tf(col)
- Transparency.
-
sharpness(v)
-
map_Ka(mapname, options)
-
map_Kd(mapname, options)
-
map_Ks(mapname, options)
-
map_Ke(mapname, options)
-
map_Ns(mapname, options)
-
map_d(mapname, options)
-
map_Bump(mapname, options)
-
refl(mapname, options)
4.19.3. Handling new keywords
The OBJReader and MTLReader classes can also handle keywords
that are not known to the classes. For each keyword, the classes look for a
method called handle_<keyword> which is invoked with the arguments as
parameters. If such a handler is not found, the handleUnknown() method is
called.
For the standard keywords such handle_<keyword> handlers are already
available. Their task is to preprocess the arguments and call the final handler
method (which is just named after the keyword).