Cleaning, MTLParser
This commit is contained in:
parent
ba5ff60448
commit
aac0d0b1e9
|
@ -43,6 +43,15 @@ class Face:
|
||||||
self.c = FaceVertex().from_array(arr[2])
|
self.c = FaceVertex().from_array(arr[2])
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
class Material:
|
||||||
|
def __init__(self, name):
|
||||||
|
self.name = name
|
||||||
|
self.Ka = None
|
||||||
|
self.Kd = None
|
||||||
|
self.Ks = None
|
||||||
|
self.map_Kd = None
|
||||||
|
|
||||||
|
|
||||||
class ModelParser:
|
class ModelParser:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -55,6 +64,7 @@ class ModelParser:
|
||||||
self.vertex_vbo = None
|
self.vertex_vbo = None
|
||||||
self.tex_coord_vbo = None
|
self.tex_coord_vbo = None
|
||||||
self.normal_vbo = None
|
self.normal_vbo = None
|
||||||
|
self.path = None
|
||||||
|
|
||||||
def add_vertex(self, vertex):
|
def add_vertex(self, vertex):
|
||||||
self.vertices.append(vertex)
|
self.vertices.append(vertex)
|
||||||
|
@ -73,11 +83,18 @@ class ModelParser:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def parse_file(self, path):
|
def parse_file(self, path):
|
||||||
|
self.path = path
|
||||||
with open(path) as f:
|
with open(path) as f:
|
||||||
for line in f.readlines():
|
for line in f.readlines():
|
||||||
line = line.rstrip()
|
line = line.rstrip()
|
||||||
self.parse_line(line)
|
self.parse_line(line)
|
||||||
|
|
||||||
|
for material in self.mtl.materials:
|
||||||
|
print(material.name)
|
||||||
|
print('\tKa ' + str(material.Ka))
|
||||||
|
print('\tKd ' + str(material.Kd))
|
||||||
|
print('\tKs ' + str(material.Ks))
|
||||||
|
|
||||||
def draw(self):
|
def draw(self):
|
||||||
|
|
||||||
import OpenGL.GL as gl
|
import OpenGL.GL as gl
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
from .basemodel import ModelParser, Exporter, Vertex, TexCoord, Normal, FaceVertex, Face
|
from .basemodel import ModelParser, Exporter, Vertex, TexCoord, Normal, FaceVertex, Face, Material
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
|
import os.path
|
||||||
|
import PIL.Image
|
||||||
|
|
||||||
|
|
||||||
def is_obj(filename):
|
def is_obj(filename):
|
||||||
return filename[-4:] == '.obj'
|
return filename[-4:] == '.obj'
|
||||||
|
@ -10,7 +13,7 @@ class OBJParser(ModelParser):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.materials = []
|
self.mtl = None
|
||||||
|
|
||||||
def parse_line(self, string):
|
def parse_line(self, string):
|
||||||
if string == '':
|
if string == '':
|
||||||
|
@ -22,6 +25,10 @@ class OBJParser(ModelParser):
|
||||||
|
|
||||||
if first == 'usemtl':
|
if first == 'usemtl':
|
||||||
self.currentMaterial = split[0]
|
self.currentMaterial = split[0]
|
||||||
|
elif first == 'mtllib':
|
||||||
|
path = os.path.join(os.path.dirname(self.path), split[0])
|
||||||
|
self.mtl = MTLParser(self)
|
||||||
|
self.mtl.parse_file(path)
|
||||||
elif first == 'v':
|
elif first == 'v':
|
||||||
self.add_vertex(Vertex().from_array(split))
|
self.add_vertex(Vertex().from_array(split))
|
||||||
elif first == 'vn':
|
elif first == 'vn':
|
||||||
|
@ -38,6 +45,42 @@ class OBJParser(ModelParser):
|
||||||
|
|
||||||
self.add_face(Face().from_array(splits))
|
self.add_face(Face().from_array(splits))
|
||||||
|
|
||||||
|
class MTLParser:
|
||||||
|
|
||||||
|
def __init__(self, parent):
|
||||||
|
self.parent = parent
|
||||||
|
self.materials = []
|
||||||
|
self.current_mtl = None
|
||||||
|
|
||||||
|
def parse_line(self, string):
|
||||||
|
|
||||||
|
if string == '':
|
||||||
|
return
|
||||||
|
|
||||||
|
split = string.split()
|
||||||
|
first = split[0]
|
||||||
|
split = split[1:]
|
||||||
|
|
||||||
|
if first == 'newmtl':
|
||||||
|
self.current_mtl = Material(split[0])
|
||||||
|
self.materials.append(self.current_mtl)
|
||||||
|
elif first == 'Ka':
|
||||||
|
self.current_mtl.Ka = Vertex().from_array(split)
|
||||||
|
elif first == 'Kd':
|
||||||
|
self.current_mtl.Kd = Vertex().from_array(split)
|
||||||
|
elif first == 'Ks':
|
||||||
|
self.current_mtl.Ks = Vertex().from_array(split)
|
||||||
|
elif first == 'map_Kd':
|
||||||
|
self.map_Kd = PIL.Image.open(os.path.join(os.path.dirname(self.parent.path), split[0]))
|
||||||
|
|
||||||
|
|
||||||
|
def parse_file(self, path):
|
||||||
|
with open(path) as f:
|
||||||
|
for line in f.readlines():
|
||||||
|
line = line.rstrip()
|
||||||
|
self.parse_line(line)
|
||||||
|
|
||||||
|
|
||||||
class OBJExporter(Exporter):
|
class OBJExporter(Exporter):
|
||||||
def __init__(self, model):
|
def __init__(self, model):
|
||||||
super().__init__(model)
|
super().__init__(model)
|
||||||
|
|
Loading…
Reference in New Issue