PyWavefront
PyWavefront copied to clipboard
Can't parse texture?
For some reason our script can parse the .mtl file & .png attached to it? It'd be very helpful if someone can look on this and explain what issue do we have here.
import argparse
import ctypes
import random
import sys
import math
import time
import os
sys.path.append('..')
import pyglet
from pyglet.gl import glMatrixMode, GL_PROJECTION, glLoadIdentity, gluPerspective, GL_MODELVIEW, \
glClearColor, glEnable, GL_LIGHT0, glTranslated, glRotatef, GL_LIGHTING, glLightfv, GL_POSITION, \
GLfloat, GL_SPECULAR, GL_DIFFUSE, GL_LIGHT1
from pywavefront import visualization
import pywavefront
def create_model(filename, perspective_angle, rotatex, rotatey, rotatez, zoom_scale):
meshes = pywavefront.Wavefront(filename, create_materials=True)
window = pyglet.window.Window()
lightfv = ctypes.c_float * 4
n = filename.split('/')[-1]
path = filename.replace(n, '')
with open(f'{path}/params.txt', 'w+') as f:
f.writelines([
f'perspective_angle {perspective_angle}\n',
f'rotatex {rotatex}\n',
f'rotatey {rotatey}\n',
f'rotatez {rotatez}',
f'scale {zoom_scale}'
])
@window.event
def on_resize(width, height):
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(perspective_angle, float(width) / height, 0.001, 10000.)
glMatrixMode(GL_MODELVIEW)
return True
global zoom, outName
max_r2 = 0
for v in meshes.vertices:
x, y, z = v
r2 = y ** 2 + z ** 2
if r2 > max_r2:
max_r2 = r2
r2 = x ** 2 + z ** 2
if r2 > max_r2:
max_r2 = r2
scale_z = abs(1 / math.tan(math.radians(perspective_angle / 2)))
zoom = -math.sqrt(max_r2) * scale_z
print(zoom)
outName = f"{filename.split('/')[-1][:-4]}.png"
@window.event
def on_draw():
# global rotatex, rotatey, outName
glClearColor(210, 210, 210, 220)
window.clear()
glLoadIdentity()
glTranslated(0.0, -0.0, zoom * zoom_scale)
glRotatef(rotatex, 1.0, 0.0, 0.0)
glRotatef(rotatey, 0.0, 1.0, 0.0)
glRotatef(rotatez, 0.0, 0.0, 1.0)
# glEnable(GL_LIGHTING)
visualization.draw(meshes)
pyglet.image.get_buffer_manager().get_color_buffer().save(outName)
exit()
pyglet.app.run()
if __name__ == "__main__":
with open('test_model.txt') as f:
model_name = f.readlines()[0]
model_name = 'Rem870'
dir_ = f'models/{model_name}/'
assert os.path.exists(dir_) is True, f'{dir_} from test_model.txt not found!'
files = os.listdir(dir_)
assert len([f for f in files if f.endswith('obj')]) != 0, f'{dir_} has no .obj file!'
for f in files:
if f.endswith('obj'):
filename = f'{dir_}/{f}'
create_model(filename=filename, rotatex=0, rotatey=0, rotatez=0, perspective_angle=30, zoom_scale=1.0)
newmtl rem870_tex Ka 0.20 0.20 0.20 Kd 0.59 0.59 0.59 Ks 1.00 1.00 1.00 illum 4 map_Kd rem870_tex.png
And thats our MTL file here