What am I supposed to place in “filename” in the OBJFileLoader?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















In the OBJFileLoader found at https://www.pygame.org/wiki/OBJFileLoader, what am I supposed to put in place of "filename".



In the Class OBJ, in the line: def init(self, filename, swapyz=False):
when I put the full path to the .obj file, it says incorrect syntax.



New Error Message:



Traceback (most recent call last):
File "C:/Users/Winter/PycharmProjects/Plane/Plane.py", line 26, in <module>
obj = OBJ('C:/Users/Winter/Desktop/TAL16OBJ.obj')
File "C:UsersWinterPycharmProjectsPlaneMain.py", line 86, in __init__
mtl = self.mtl[material]
AttributeError: 'OBJ' object has no attribute 'mtl'


This is the current code:



import pygame
from OpenGL.GL import *

def MTL(filename):
contents = {}
mtl = None
for line in open("C:/Users/Winter/Desktop/TAL16OBJ.mtl", "r"):
if line.startswith('#'): continue
values = line.split()
if not values: continue
if values[0] == 'newmtl':
mtl = contents[values[1]] = {}
elif mtl is None:
raise ValueError("mtl file doesn't start with newmtl stmt")
elif values[0] == 'map_Kd':
# load the texture referred to by this declaration
mtl[values[0]] = values[1]
surf = pygame.image.load(mtl['map_Kd'])
image = pygame.image.tostring(surf, 'RGBA', 1)
ix, iy = surf.get_rect().size
texid = mtl['texture_Kd'] = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texid)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ix, iy, 0, GL_RGBA,
GL_UNSIGNED_BYTE, image)
else:
mtl[values[0]] = list(map(float, values[1:]))
return contents

class OBJ:
def __init__(self, filename, swapyz=False):
"""Loads a Wavefront OBJ file. """
self.vertices =
self.normals =
self.texcoords =
self.faces =

material = None
for line in open("C:/Users/Winter/Desktop/TAL16OBJ.obj", "r"):
if line.startswith('#'): continue
values = line.split()
if not values: continue
if values[0] == 'v':
v = list(map(float, values[1:4]))
if swapyz:
v = v[0], v[2], v[1]
self.vertices.append(v)
elif values[0] == 'vn':
v = list(map(float, values[1:4]))
if swapyz:
v = v[0], v[2], v[1]
self.normals.append(v)
elif values[0] == 'vt':
self.texcoords.append(list(map(float, values[1:3])))
elif values[0] in ('usemtl', 'usemat'):
material = values[1]
elif values[0] == 'mtllib':
self.mtl = MTL(values[1])
elif values[0] == 'f':
face =
texcoords =
norms =
for v in values[1:]:
w = v.split('/')
face.append(int(w[0]))
if len(w) >= 2 and len(w[1]) > 0:
texcoords.append(int(w[1]))
else:
texcoords.append(0)
if len(w) >= 3 and len(w[2]) > 0:
norms.append(int(w[2]))
else:
norms.append(0)
self.faces.append((face, norms, texcoords, material))

self.gl_list = glGenLists(1)
glNewList(self.gl_list, GL_COMPILE)
glEnable(GL_TEXTURE_2D)
glFrontFace(GL_CCW)
for face in self.faces:
vertices, normals, texture_coords, material = face

mtl = self.mtl[material]
if 'texture_Kd' in mtl:
# use diffuse texmap
glBindTexture(GL_TEXTURE_2D, mtl['texture_Kd'])
else:
# just use diffuse colour
glColor(*mtl['Kd'])

glBegin(GL_POLYGON)
for i in range(len(vertices)):
if normals[i] > 0:
glNormal3fv(self.normals[normals[i] - 1])
if texture_coords[i] > 0:
glTexCoord2fv(self.texcoords[texture_coords[i] - 1])
glVertex3fv(self.vertices[vertices[i] - 1])
glEnd()
glDisable(GL_TEXTURE_2D)
glEndList()

from Main import *
import sys
import pygame
from pygame.locals import *
from pygame.constants import *
from OpenGL.GL import *
from OpenGL.GLU import *

pygame.init()
viewport = (800,600)
hx = viewport[0]/2
hy = viewport[1]/2
srf = pygame.display.set_mode(viewport, OPENGL | DOUBLEBUF)

glLightfv(GL_LIGHT0, GL_POSITION, (-40, 200, 100, 0.0))
glLightfv(GL_LIGHT0, GL_AMBIENT, (0.2, 0.2, 0.2, 1.0))
glLightfv(GL_LIGHT0, GL_DIFFUSE, (0.5, 0.5, 0.5, 1.0))
glEnable(GL_LIGHT0)
glEnable(GL_LIGHTING)
glEnable(GL_COLOR_MATERIAL)
glEnable(GL_DEPTH_TEST)
glShadeModel(GL_SMOOTH) # most obj files expect to be smooth-shaded

# LOAD OBJECT AFTER PYGAME INIT
obj = OBJ("C:/Users/Winter/Desktop/TAL16OBJ.obj")

clock = pygame.time.Clock()

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
width, height = viewport
gluPerspective(90.0, width/float(height), 1, 100.0)
glEnable(GL_DEPTH_TEST)
glMatrixMode(GL_MODELVIEW)

rx, ry = (0,0)
tx, ty = (0,0)
zpos = 5
rotate = move = False
while 1:
clock.tick(30)
for e in pygame.event.get():
if e.type == QUIT:
sys.exit()
elif e.type == KEYDOWN and e.key == K_ESCAPE:
sys.exit()
elif e.type == MOUSEBUTTONDOWN:
if e.button == 4: zpos = max(1, zpos-1)
elif e.button == 5: zpos += 1
elif e.button == 1: rotate = True
elif e.button == 3: move = True
elif e.type == MOUSEBUTTONUP:
if e.button == 1: rotate = False
elif e.button == 3: move = False
elif e.type == MOUSEMOTION:
i, j = e.rel
if rotate:
rx += i
ry += j
if move:
tx += i
ty -= j

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()

# RENDER OBJECT
glTranslate(tx/20., ty/20., - zpos)
glRotate(ry, 1, 0, 0)
glRotate(rx, 0, 1, 0)
glCallList(obj.gl_list)









share|improve this question

























  • Please show exactly how you call it

    – JETM
    Nov 29 '18 at 4:25











  • def __init__(self, C:/Users/Winter/Desktop/TAL16OBJ.obj, swapyz=False):

    – Manly Winter
    Nov 29 '18 at 4:25






  • 1





    filename expects a string. That is, the path should be in quotes

    – JETM
    Nov 29 '18 at 4:27











  • Still the same result.

    – Manly Winter
    Nov 29 '18 at 4:30











  • This is not how to use a class. You might want to look at a python beginners guide.

    – tkausl
    Nov 29 '18 at 4:33


















0















In the OBJFileLoader found at https://www.pygame.org/wiki/OBJFileLoader, what am I supposed to put in place of "filename".



In the Class OBJ, in the line: def init(self, filename, swapyz=False):
when I put the full path to the .obj file, it says incorrect syntax.



New Error Message:



Traceback (most recent call last):
File "C:/Users/Winter/PycharmProjects/Plane/Plane.py", line 26, in <module>
obj = OBJ('C:/Users/Winter/Desktop/TAL16OBJ.obj')
File "C:UsersWinterPycharmProjectsPlaneMain.py", line 86, in __init__
mtl = self.mtl[material]
AttributeError: 'OBJ' object has no attribute 'mtl'


This is the current code:



import pygame
from OpenGL.GL import *

def MTL(filename):
contents = {}
mtl = None
for line in open("C:/Users/Winter/Desktop/TAL16OBJ.mtl", "r"):
if line.startswith('#'): continue
values = line.split()
if not values: continue
if values[0] == 'newmtl':
mtl = contents[values[1]] = {}
elif mtl is None:
raise ValueError("mtl file doesn't start with newmtl stmt")
elif values[0] == 'map_Kd':
# load the texture referred to by this declaration
mtl[values[0]] = values[1]
surf = pygame.image.load(mtl['map_Kd'])
image = pygame.image.tostring(surf, 'RGBA', 1)
ix, iy = surf.get_rect().size
texid = mtl['texture_Kd'] = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texid)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ix, iy, 0, GL_RGBA,
GL_UNSIGNED_BYTE, image)
else:
mtl[values[0]] = list(map(float, values[1:]))
return contents

class OBJ:
def __init__(self, filename, swapyz=False):
"""Loads a Wavefront OBJ file. """
self.vertices =
self.normals =
self.texcoords =
self.faces =

material = None
for line in open("C:/Users/Winter/Desktop/TAL16OBJ.obj", "r"):
if line.startswith('#'): continue
values = line.split()
if not values: continue
if values[0] == 'v':
v = list(map(float, values[1:4]))
if swapyz:
v = v[0], v[2], v[1]
self.vertices.append(v)
elif values[0] == 'vn':
v = list(map(float, values[1:4]))
if swapyz:
v = v[0], v[2], v[1]
self.normals.append(v)
elif values[0] == 'vt':
self.texcoords.append(list(map(float, values[1:3])))
elif values[0] in ('usemtl', 'usemat'):
material = values[1]
elif values[0] == 'mtllib':
self.mtl = MTL(values[1])
elif values[0] == 'f':
face =
texcoords =
norms =
for v in values[1:]:
w = v.split('/')
face.append(int(w[0]))
if len(w) >= 2 and len(w[1]) > 0:
texcoords.append(int(w[1]))
else:
texcoords.append(0)
if len(w) >= 3 and len(w[2]) > 0:
norms.append(int(w[2]))
else:
norms.append(0)
self.faces.append((face, norms, texcoords, material))

self.gl_list = glGenLists(1)
glNewList(self.gl_list, GL_COMPILE)
glEnable(GL_TEXTURE_2D)
glFrontFace(GL_CCW)
for face in self.faces:
vertices, normals, texture_coords, material = face

mtl = self.mtl[material]
if 'texture_Kd' in mtl:
# use diffuse texmap
glBindTexture(GL_TEXTURE_2D, mtl['texture_Kd'])
else:
# just use diffuse colour
glColor(*mtl['Kd'])

glBegin(GL_POLYGON)
for i in range(len(vertices)):
if normals[i] > 0:
glNormal3fv(self.normals[normals[i] - 1])
if texture_coords[i] > 0:
glTexCoord2fv(self.texcoords[texture_coords[i] - 1])
glVertex3fv(self.vertices[vertices[i] - 1])
glEnd()
glDisable(GL_TEXTURE_2D)
glEndList()

from Main import *
import sys
import pygame
from pygame.locals import *
from pygame.constants import *
from OpenGL.GL import *
from OpenGL.GLU import *

pygame.init()
viewport = (800,600)
hx = viewport[0]/2
hy = viewport[1]/2
srf = pygame.display.set_mode(viewport, OPENGL | DOUBLEBUF)

glLightfv(GL_LIGHT0, GL_POSITION, (-40, 200, 100, 0.0))
glLightfv(GL_LIGHT0, GL_AMBIENT, (0.2, 0.2, 0.2, 1.0))
glLightfv(GL_LIGHT0, GL_DIFFUSE, (0.5, 0.5, 0.5, 1.0))
glEnable(GL_LIGHT0)
glEnable(GL_LIGHTING)
glEnable(GL_COLOR_MATERIAL)
glEnable(GL_DEPTH_TEST)
glShadeModel(GL_SMOOTH) # most obj files expect to be smooth-shaded

# LOAD OBJECT AFTER PYGAME INIT
obj = OBJ("C:/Users/Winter/Desktop/TAL16OBJ.obj")

clock = pygame.time.Clock()

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
width, height = viewport
gluPerspective(90.0, width/float(height), 1, 100.0)
glEnable(GL_DEPTH_TEST)
glMatrixMode(GL_MODELVIEW)

rx, ry = (0,0)
tx, ty = (0,0)
zpos = 5
rotate = move = False
while 1:
clock.tick(30)
for e in pygame.event.get():
if e.type == QUIT:
sys.exit()
elif e.type == KEYDOWN and e.key == K_ESCAPE:
sys.exit()
elif e.type == MOUSEBUTTONDOWN:
if e.button == 4: zpos = max(1, zpos-1)
elif e.button == 5: zpos += 1
elif e.button == 1: rotate = True
elif e.button == 3: move = True
elif e.type == MOUSEBUTTONUP:
if e.button == 1: rotate = False
elif e.button == 3: move = False
elif e.type == MOUSEMOTION:
i, j = e.rel
if rotate:
rx += i
ry += j
if move:
tx += i
ty -= j

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()

# RENDER OBJECT
glTranslate(tx/20., ty/20., - zpos)
glRotate(ry, 1, 0, 0)
glRotate(rx, 0, 1, 0)
glCallList(obj.gl_list)









share|improve this question

























  • Please show exactly how you call it

    – JETM
    Nov 29 '18 at 4:25











  • def __init__(self, C:/Users/Winter/Desktop/TAL16OBJ.obj, swapyz=False):

    – Manly Winter
    Nov 29 '18 at 4:25






  • 1





    filename expects a string. That is, the path should be in quotes

    – JETM
    Nov 29 '18 at 4:27











  • Still the same result.

    – Manly Winter
    Nov 29 '18 at 4:30











  • This is not how to use a class. You might want to look at a python beginners guide.

    – tkausl
    Nov 29 '18 at 4:33














0












0








0








In the OBJFileLoader found at https://www.pygame.org/wiki/OBJFileLoader, what am I supposed to put in place of "filename".



In the Class OBJ, in the line: def init(self, filename, swapyz=False):
when I put the full path to the .obj file, it says incorrect syntax.



New Error Message:



Traceback (most recent call last):
File "C:/Users/Winter/PycharmProjects/Plane/Plane.py", line 26, in <module>
obj = OBJ('C:/Users/Winter/Desktop/TAL16OBJ.obj')
File "C:UsersWinterPycharmProjectsPlaneMain.py", line 86, in __init__
mtl = self.mtl[material]
AttributeError: 'OBJ' object has no attribute 'mtl'


This is the current code:



import pygame
from OpenGL.GL import *

def MTL(filename):
contents = {}
mtl = None
for line in open("C:/Users/Winter/Desktop/TAL16OBJ.mtl", "r"):
if line.startswith('#'): continue
values = line.split()
if not values: continue
if values[0] == 'newmtl':
mtl = contents[values[1]] = {}
elif mtl is None:
raise ValueError("mtl file doesn't start with newmtl stmt")
elif values[0] == 'map_Kd':
# load the texture referred to by this declaration
mtl[values[0]] = values[1]
surf = pygame.image.load(mtl['map_Kd'])
image = pygame.image.tostring(surf, 'RGBA', 1)
ix, iy = surf.get_rect().size
texid = mtl['texture_Kd'] = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texid)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ix, iy, 0, GL_RGBA,
GL_UNSIGNED_BYTE, image)
else:
mtl[values[0]] = list(map(float, values[1:]))
return contents

class OBJ:
def __init__(self, filename, swapyz=False):
"""Loads a Wavefront OBJ file. """
self.vertices =
self.normals =
self.texcoords =
self.faces =

material = None
for line in open("C:/Users/Winter/Desktop/TAL16OBJ.obj", "r"):
if line.startswith('#'): continue
values = line.split()
if not values: continue
if values[0] == 'v':
v = list(map(float, values[1:4]))
if swapyz:
v = v[0], v[2], v[1]
self.vertices.append(v)
elif values[0] == 'vn':
v = list(map(float, values[1:4]))
if swapyz:
v = v[0], v[2], v[1]
self.normals.append(v)
elif values[0] == 'vt':
self.texcoords.append(list(map(float, values[1:3])))
elif values[0] in ('usemtl', 'usemat'):
material = values[1]
elif values[0] == 'mtllib':
self.mtl = MTL(values[1])
elif values[0] == 'f':
face =
texcoords =
norms =
for v in values[1:]:
w = v.split('/')
face.append(int(w[0]))
if len(w) >= 2 and len(w[1]) > 0:
texcoords.append(int(w[1]))
else:
texcoords.append(0)
if len(w) >= 3 and len(w[2]) > 0:
norms.append(int(w[2]))
else:
norms.append(0)
self.faces.append((face, norms, texcoords, material))

self.gl_list = glGenLists(1)
glNewList(self.gl_list, GL_COMPILE)
glEnable(GL_TEXTURE_2D)
glFrontFace(GL_CCW)
for face in self.faces:
vertices, normals, texture_coords, material = face

mtl = self.mtl[material]
if 'texture_Kd' in mtl:
# use diffuse texmap
glBindTexture(GL_TEXTURE_2D, mtl['texture_Kd'])
else:
# just use diffuse colour
glColor(*mtl['Kd'])

glBegin(GL_POLYGON)
for i in range(len(vertices)):
if normals[i] > 0:
glNormal3fv(self.normals[normals[i] - 1])
if texture_coords[i] > 0:
glTexCoord2fv(self.texcoords[texture_coords[i] - 1])
glVertex3fv(self.vertices[vertices[i] - 1])
glEnd()
glDisable(GL_TEXTURE_2D)
glEndList()

from Main import *
import sys
import pygame
from pygame.locals import *
from pygame.constants import *
from OpenGL.GL import *
from OpenGL.GLU import *

pygame.init()
viewport = (800,600)
hx = viewport[0]/2
hy = viewport[1]/2
srf = pygame.display.set_mode(viewport, OPENGL | DOUBLEBUF)

glLightfv(GL_LIGHT0, GL_POSITION, (-40, 200, 100, 0.0))
glLightfv(GL_LIGHT0, GL_AMBIENT, (0.2, 0.2, 0.2, 1.0))
glLightfv(GL_LIGHT0, GL_DIFFUSE, (0.5, 0.5, 0.5, 1.0))
glEnable(GL_LIGHT0)
glEnable(GL_LIGHTING)
glEnable(GL_COLOR_MATERIAL)
glEnable(GL_DEPTH_TEST)
glShadeModel(GL_SMOOTH) # most obj files expect to be smooth-shaded

# LOAD OBJECT AFTER PYGAME INIT
obj = OBJ("C:/Users/Winter/Desktop/TAL16OBJ.obj")

clock = pygame.time.Clock()

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
width, height = viewport
gluPerspective(90.0, width/float(height), 1, 100.0)
glEnable(GL_DEPTH_TEST)
glMatrixMode(GL_MODELVIEW)

rx, ry = (0,0)
tx, ty = (0,0)
zpos = 5
rotate = move = False
while 1:
clock.tick(30)
for e in pygame.event.get():
if e.type == QUIT:
sys.exit()
elif e.type == KEYDOWN and e.key == K_ESCAPE:
sys.exit()
elif e.type == MOUSEBUTTONDOWN:
if e.button == 4: zpos = max(1, zpos-1)
elif e.button == 5: zpos += 1
elif e.button == 1: rotate = True
elif e.button == 3: move = True
elif e.type == MOUSEBUTTONUP:
if e.button == 1: rotate = False
elif e.button == 3: move = False
elif e.type == MOUSEMOTION:
i, j = e.rel
if rotate:
rx += i
ry += j
if move:
tx += i
ty -= j

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()

# RENDER OBJECT
glTranslate(tx/20., ty/20., - zpos)
glRotate(ry, 1, 0, 0)
glRotate(rx, 0, 1, 0)
glCallList(obj.gl_list)









share|improve this question
















In the OBJFileLoader found at https://www.pygame.org/wiki/OBJFileLoader, what am I supposed to put in place of "filename".



In the Class OBJ, in the line: def init(self, filename, swapyz=False):
when I put the full path to the .obj file, it says incorrect syntax.



New Error Message:



Traceback (most recent call last):
File "C:/Users/Winter/PycharmProjects/Plane/Plane.py", line 26, in <module>
obj = OBJ('C:/Users/Winter/Desktop/TAL16OBJ.obj')
File "C:UsersWinterPycharmProjectsPlaneMain.py", line 86, in __init__
mtl = self.mtl[material]
AttributeError: 'OBJ' object has no attribute 'mtl'


This is the current code:



import pygame
from OpenGL.GL import *

def MTL(filename):
contents = {}
mtl = None
for line in open("C:/Users/Winter/Desktop/TAL16OBJ.mtl", "r"):
if line.startswith('#'): continue
values = line.split()
if not values: continue
if values[0] == 'newmtl':
mtl = contents[values[1]] = {}
elif mtl is None:
raise ValueError("mtl file doesn't start with newmtl stmt")
elif values[0] == 'map_Kd':
# load the texture referred to by this declaration
mtl[values[0]] = values[1]
surf = pygame.image.load(mtl['map_Kd'])
image = pygame.image.tostring(surf, 'RGBA', 1)
ix, iy = surf.get_rect().size
texid = mtl['texture_Kd'] = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texid)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ix, iy, 0, GL_RGBA,
GL_UNSIGNED_BYTE, image)
else:
mtl[values[0]] = list(map(float, values[1:]))
return contents

class OBJ:
def __init__(self, filename, swapyz=False):
"""Loads a Wavefront OBJ file. """
self.vertices =
self.normals =
self.texcoords =
self.faces =

material = None
for line in open("C:/Users/Winter/Desktop/TAL16OBJ.obj", "r"):
if line.startswith('#'): continue
values = line.split()
if not values: continue
if values[0] == 'v':
v = list(map(float, values[1:4]))
if swapyz:
v = v[0], v[2], v[1]
self.vertices.append(v)
elif values[0] == 'vn':
v = list(map(float, values[1:4]))
if swapyz:
v = v[0], v[2], v[1]
self.normals.append(v)
elif values[0] == 'vt':
self.texcoords.append(list(map(float, values[1:3])))
elif values[0] in ('usemtl', 'usemat'):
material = values[1]
elif values[0] == 'mtllib':
self.mtl = MTL(values[1])
elif values[0] == 'f':
face =
texcoords =
norms =
for v in values[1:]:
w = v.split('/')
face.append(int(w[0]))
if len(w) >= 2 and len(w[1]) > 0:
texcoords.append(int(w[1]))
else:
texcoords.append(0)
if len(w) >= 3 and len(w[2]) > 0:
norms.append(int(w[2]))
else:
norms.append(0)
self.faces.append((face, norms, texcoords, material))

self.gl_list = glGenLists(1)
glNewList(self.gl_list, GL_COMPILE)
glEnable(GL_TEXTURE_2D)
glFrontFace(GL_CCW)
for face in self.faces:
vertices, normals, texture_coords, material = face

mtl = self.mtl[material]
if 'texture_Kd' in mtl:
# use diffuse texmap
glBindTexture(GL_TEXTURE_2D, mtl['texture_Kd'])
else:
# just use diffuse colour
glColor(*mtl['Kd'])

glBegin(GL_POLYGON)
for i in range(len(vertices)):
if normals[i] > 0:
glNormal3fv(self.normals[normals[i] - 1])
if texture_coords[i] > 0:
glTexCoord2fv(self.texcoords[texture_coords[i] - 1])
glVertex3fv(self.vertices[vertices[i] - 1])
glEnd()
glDisable(GL_TEXTURE_2D)
glEndList()

from Main import *
import sys
import pygame
from pygame.locals import *
from pygame.constants import *
from OpenGL.GL import *
from OpenGL.GLU import *

pygame.init()
viewport = (800,600)
hx = viewport[0]/2
hy = viewport[1]/2
srf = pygame.display.set_mode(viewport, OPENGL | DOUBLEBUF)

glLightfv(GL_LIGHT0, GL_POSITION, (-40, 200, 100, 0.0))
glLightfv(GL_LIGHT0, GL_AMBIENT, (0.2, 0.2, 0.2, 1.0))
glLightfv(GL_LIGHT0, GL_DIFFUSE, (0.5, 0.5, 0.5, 1.0))
glEnable(GL_LIGHT0)
glEnable(GL_LIGHTING)
glEnable(GL_COLOR_MATERIAL)
glEnable(GL_DEPTH_TEST)
glShadeModel(GL_SMOOTH) # most obj files expect to be smooth-shaded

# LOAD OBJECT AFTER PYGAME INIT
obj = OBJ("C:/Users/Winter/Desktop/TAL16OBJ.obj")

clock = pygame.time.Clock()

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
width, height = viewport
gluPerspective(90.0, width/float(height), 1, 100.0)
glEnable(GL_DEPTH_TEST)
glMatrixMode(GL_MODELVIEW)

rx, ry = (0,0)
tx, ty = (0,0)
zpos = 5
rotate = move = False
while 1:
clock.tick(30)
for e in pygame.event.get():
if e.type == QUIT:
sys.exit()
elif e.type == KEYDOWN and e.key == K_ESCAPE:
sys.exit()
elif e.type == MOUSEBUTTONDOWN:
if e.button == 4: zpos = max(1, zpos-1)
elif e.button == 5: zpos += 1
elif e.button == 1: rotate = True
elif e.button == 3: move = True
elif e.type == MOUSEBUTTONUP:
if e.button == 1: rotate = False
elif e.button == 3: move = False
elif e.type == MOUSEMOTION:
i, j = e.rel
if rotate:
rx += i
ry += j
if move:
tx += i
ty -= j

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()

# RENDER OBJECT
glTranslate(tx/20., ty/20., - zpos)
glRotate(ry, 1, 0, 0)
glRotate(rx, 0, 1, 0)
glCallList(obj.gl_list)






python graphics pygame .obj






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 '18 at 14:30









Nicol Bolas

291k34481659




291k34481659










asked Nov 29 '18 at 4:24









Manly WinterManly Winter

136




136













  • Please show exactly how you call it

    – JETM
    Nov 29 '18 at 4:25











  • def __init__(self, C:/Users/Winter/Desktop/TAL16OBJ.obj, swapyz=False):

    – Manly Winter
    Nov 29 '18 at 4:25






  • 1





    filename expects a string. That is, the path should be in quotes

    – JETM
    Nov 29 '18 at 4:27











  • Still the same result.

    – Manly Winter
    Nov 29 '18 at 4:30











  • This is not how to use a class. You might want to look at a python beginners guide.

    – tkausl
    Nov 29 '18 at 4:33



















  • Please show exactly how you call it

    – JETM
    Nov 29 '18 at 4:25











  • def __init__(self, C:/Users/Winter/Desktop/TAL16OBJ.obj, swapyz=False):

    – Manly Winter
    Nov 29 '18 at 4:25






  • 1





    filename expects a string. That is, the path should be in quotes

    – JETM
    Nov 29 '18 at 4:27











  • Still the same result.

    – Manly Winter
    Nov 29 '18 at 4:30











  • This is not how to use a class. You might want to look at a python beginners guide.

    – tkausl
    Nov 29 '18 at 4:33

















Please show exactly how you call it

– JETM
Nov 29 '18 at 4:25





Please show exactly how you call it

– JETM
Nov 29 '18 at 4:25













def __init__(self, C:/Users/Winter/Desktop/TAL16OBJ.obj, swapyz=False):

– Manly Winter
Nov 29 '18 at 4:25





def __init__(self, C:/Users/Winter/Desktop/TAL16OBJ.obj, swapyz=False):

– Manly Winter
Nov 29 '18 at 4:25




1




1





filename expects a string. That is, the path should be in quotes

– JETM
Nov 29 '18 at 4:27





filename expects a string. That is, the path should be in quotes

– JETM
Nov 29 '18 at 4:27













Still the same result.

– Manly Winter
Nov 29 '18 at 4:30





Still the same result.

– Manly Winter
Nov 29 '18 at 4:30













This is not how to use a class. You might want to look at a python beginners guide.

– tkausl
Nov 29 '18 at 4:33





This is not how to use a class. You might want to look at a python beginners guide.

– tkausl
Nov 29 '18 at 4:33












1 Answer
1






active

oldest

votes


















0














The AttributeError: 'OBJ' object has no attribute 'mtl' was raised because the material was missing in the .obj file. There should be a line like this at the top of the .obj file



mtllib NameOfYourMTLfile.mtl 


that indicates which material/.mtl file should be used for the model. The model was probably not exported correctly.





You have to call pygame.display.flip() at the bottom of the while loop to update the pygame window.



while 1:
# ... other code omitted.
# Call this at the bottom of the loop.
pygame.display.flip()




Also, in the MTL function and the OBJ class you should pass the filename variable/parameter to open instead of this string literal "C:/Users/Winter/Desktop/TAL16OBJ.mtl".



def MTL(filename):
contents = {}
mtl = None
for line in open(filename, "r"): # open(filename)


class OBJ:
def __init__(self, filename, swapyz=False):
"""Loads a Wavefront OBJ file. """
self.vertices =
self.normals =
self.texcoords =
self.faces =

material = None
for line in open(filename, "r"): # open(filename)





share|improve this answer


























  • You. are. a. Genius. I've been working on this for hours. Thank you so much!!!

    – Manly Winter
    Nov 29 '18 at 7:44












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53531811%2fwhat-am-i-supposed-to-place-in-filename-in-the-objfileloader%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The AttributeError: 'OBJ' object has no attribute 'mtl' was raised because the material was missing in the .obj file. There should be a line like this at the top of the .obj file



mtllib NameOfYourMTLfile.mtl 


that indicates which material/.mtl file should be used for the model. The model was probably not exported correctly.





You have to call pygame.display.flip() at the bottom of the while loop to update the pygame window.



while 1:
# ... other code omitted.
# Call this at the bottom of the loop.
pygame.display.flip()




Also, in the MTL function and the OBJ class you should pass the filename variable/parameter to open instead of this string literal "C:/Users/Winter/Desktop/TAL16OBJ.mtl".



def MTL(filename):
contents = {}
mtl = None
for line in open(filename, "r"): # open(filename)


class OBJ:
def __init__(self, filename, swapyz=False):
"""Loads a Wavefront OBJ file. """
self.vertices =
self.normals =
self.texcoords =
self.faces =

material = None
for line in open(filename, "r"): # open(filename)





share|improve this answer


























  • You. are. a. Genius. I've been working on this for hours. Thank you so much!!!

    – Manly Winter
    Nov 29 '18 at 7:44
















0














The AttributeError: 'OBJ' object has no attribute 'mtl' was raised because the material was missing in the .obj file. There should be a line like this at the top of the .obj file



mtllib NameOfYourMTLfile.mtl 


that indicates which material/.mtl file should be used for the model. The model was probably not exported correctly.





You have to call pygame.display.flip() at the bottom of the while loop to update the pygame window.



while 1:
# ... other code omitted.
# Call this at the bottom of the loop.
pygame.display.flip()




Also, in the MTL function and the OBJ class you should pass the filename variable/parameter to open instead of this string literal "C:/Users/Winter/Desktop/TAL16OBJ.mtl".



def MTL(filename):
contents = {}
mtl = None
for line in open(filename, "r"): # open(filename)


class OBJ:
def __init__(self, filename, swapyz=False):
"""Loads a Wavefront OBJ file. """
self.vertices =
self.normals =
self.texcoords =
self.faces =

material = None
for line in open(filename, "r"): # open(filename)





share|improve this answer


























  • You. are. a. Genius. I've been working on this for hours. Thank you so much!!!

    – Manly Winter
    Nov 29 '18 at 7:44














0












0








0







The AttributeError: 'OBJ' object has no attribute 'mtl' was raised because the material was missing in the .obj file. There should be a line like this at the top of the .obj file



mtllib NameOfYourMTLfile.mtl 


that indicates which material/.mtl file should be used for the model. The model was probably not exported correctly.





You have to call pygame.display.flip() at the bottom of the while loop to update the pygame window.



while 1:
# ... other code omitted.
# Call this at the bottom of the loop.
pygame.display.flip()




Also, in the MTL function and the OBJ class you should pass the filename variable/parameter to open instead of this string literal "C:/Users/Winter/Desktop/TAL16OBJ.mtl".



def MTL(filename):
contents = {}
mtl = None
for line in open(filename, "r"): # open(filename)


class OBJ:
def __init__(self, filename, swapyz=False):
"""Loads a Wavefront OBJ file. """
self.vertices =
self.normals =
self.texcoords =
self.faces =

material = None
for line in open(filename, "r"): # open(filename)





share|improve this answer















The AttributeError: 'OBJ' object has no attribute 'mtl' was raised because the material was missing in the .obj file. There should be a line like this at the top of the .obj file



mtllib NameOfYourMTLfile.mtl 


that indicates which material/.mtl file should be used for the model. The model was probably not exported correctly.





You have to call pygame.display.flip() at the bottom of the while loop to update the pygame window.



while 1:
# ... other code omitted.
# Call this at the bottom of the loop.
pygame.display.flip()




Also, in the MTL function and the OBJ class you should pass the filename variable/parameter to open instead of this string literal "C:/Users/Winter/Desktop/TAL16OBJ.mtl".



def MTL(filename):
contents = {}
mtl = None
for line in open(filename, "r"): # open(filename)


class OBJ:
def __init__(self, filename, swapyz=False):
"""Loads a Wavefront OBJ file. """
self.vertices =
self.normals =
self.texcoords =
self.faces =

material = None
for line in open(filename, "r"): # open(filename)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 29 '18 at 7:43

























answered Nov 29 '18 at 7:38









skrxskrx

15.8k31835




15.8k31835













  • You. are. a. Genius. I've been working on this for hours. Thank you so much!!!

    – Manly Winter
    Nov 29 '18 at 7:44



















  • You. are. a. Genius. I've been working on this for hours. Thank you so much!!!

    – Manly Winter
    Nov 29 '18 at 7:44

















You. are. a. Genius. I've been working on this for hours. Thank you so much!!!

– Manly Winter
Nov 29 '18 at 7:44





You. are. a. Genius. I've been working on this for hours. Thank you so much!!!

– Manly Winter
Nov 29 '18 at 7:44




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53531811%2fwhat-am-i-supposed-to-place-in-filename-in-the-objfileloader%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks