Skip to main content
gamedev.net
🔒 Locked

OBJ files

Started by Geometrian Jul 27, 2007 at 1:54 PM 3 replies 1.8k views
Original Post
Geometrian
Geometrian
http://www.pygame.org/wiki/OBJFileLoader?parent=CookBook Can anyone show me how this works? It says something about a command line input, but I haven't been able to get it working. Thanks in advance! Geometrian
[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]
CrimsonSun
CrimsonSun
The example program takes the name of the model that you wish to load as a parameter when you call it. So if you wish to load a model named "abc.obj", you'd call
python objloader.py abc.obj
from a command prompt (provided your directory is the active one and your obj file resides in there as well).

Every argument that you pass to "python" is available from the sys module under as the list sys.argv.
Geometrian
Geometrian
I get an error: " AttributeError: OBJ instance has no attribute 'mtl' "
[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]
CrimsonSun
CrimsonSun
That's probably because your obj file has no "mtl" line in it. Thus when your OBJ's __init__ method is called, the parsing of your file will not create the variable MTL and therefore when it is referenced later on it does not exist.

So basically your problem is that the the example provided assumes that your obj file is going to have an "mtl" line as well as an associated materials file. Here's some python code I have lying around for loading simple objs. It ignores materials and assumes that all faces are going to have a vertex index, normal index, and texture index, but it is much simpler and perhaps may be easier for you to understand:

import pygamefrom OpenGL.GL import *from OpenGL.GLU import *class objloader:    def display():        glCallList(self.list)    def load(self, filename):        starttime = time.time()        vertices = []        normals = []        texcoords = []        faces = []        for line in file(filename, "r"):            values = line.split()            if not values: continue            elif values[0].startswith("#"):                continue            elif values[0] == "v":                vertices.append( [float(x) for x in values[1:4]] )            elif values[0] == "vn":                normals.append( [float(x) for x in values[1:4]] )            elif values[0] == "vt":                texcoords.append( [float(x) for x in values[1:4]] )            elif values[0] == "f":                face = []                for elem in values[1:]:                    face.append ( [int(x) - 1 for x in elem.split("/")] )                faces.append( face )        avnorms = []        for i in range(len(vertices)):            avnorms.append([])        for elem in faces:            for (v, t, n) in elem:                avnorms[v].append(normals[n])        for i in range(len(avnorms)):            x, y, z = 0.0, 0.0, 0.0            num = float(len(avnorms))            if num:                for value in avnorms:                    x = x + value[0]                    y = y + value[1]                    z = z + value[2]                avnorms = ( x /  num, y / num, z / num )            else: avnorms = (0, 0, 1)                                self.list = glGenLists(1)        glNewList(self.list, GL_COMPILE)                            glBegin(GL_TRIANGLES)        for face in faces:            for (vi, ti, ni) in face:                glNormal3fv( avnorms[vi] )                glTexCoord2fv( texcoords[ti] )                glVertex3fv(vertices[vi] )        glEnd()        glEndList()        print 'loading "%s" (%.1fms)'% (filename, (time.time() - starttime) * 1000.0)
Geometrian
Geometrian
Thanks, but, um, how do I use it? (I'm assuming it's a stand-alone app.?)
[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.