ply parser supports textures now

This commit is contained in:
Thomas FORGIONE 2017-01-13 11:18:15 +01:00
parent 6ecc92786b
commit 68b4f6b615
No known key found for this signature in database
GPG Key ID: 2A210FFC062E00C3
1 changed files with 32 additions and 4 deletions

View File

@ -1,4 +1,6 @@
from ..basemodel import ModelParser, Exporter, Vertex, Face, FaceVertex import os
import PIL
from ..basemodel import ModelParser, Exporter, Vertex, Face, FaceVertex, TexCoord, Material
def is_ply(filename): def is_ply(filename):
return filename[-4:] == '.ply' return filename[-4:] == '.ply'
@ -9,6 +11,7 @@ class PLYParser(ModelParser):
super().__init__(up_conversion) super().__init__(up_conversion)
self.counter = 0 self.counter = 0
self.elements = [] self.elements = []
self.materials = []
self.inner_parser = PLYHeaderParser(self) self.inner_parser = PLYHeaderParser(self)
def parse_line(self, string): def parse_line(self, string):
@ -39,6 +42,16 @@ class PLYHeaderParser:
elif split[0] == 'end_header': elif split[0] == 'end_header':
self.parent.inner_parser = PLYContentParser(self.parent) self.parent.inner_parser = PLYContentParser(self.parent)
elif split[0] == 'comment' and split[1] == 'TextureFile':
material = Material('mat' + str(len(self.parent.materials)))
self.parent.materials.append(material)
try:
material.map_Kd = PIL.Image.open(os.path.join(os.path.dirname(self.parent.path), split[2]))
except:
pass
class PLYElement: class PLYElement:
def __init__(self, name, number): def __init__(self, name, number):
@ -72,14 +85,29 @@ class PLYContentParser:
elif self.current_element.name == 'face': elif self.current_element.name == 'face':
faceVertexArray = [] faceVertexArray = []
current_material = None
# Analyse element # Analyse element
offset = 0
for property in self.current_element.properties: for property in self.current_element.properties:
if property[0] == 'vertex_indices': if property[0] == 'vertex_indices':
for i in range(int(split[0])): for i in range(int(split[offset])):
faceVertexArray.append(FaceVertex(int(split[i+1]))) faceVertexArray.append(FaceVertex(int(split[i+offset+1])))
offset += int(split[0]) + 1
elif property[0] == 'texcoord':
offset += 1
for i in range(3):
# Create corresponding tex_coords
tex_coord = TexCoord().from_array(split[offset:offset+2])
offset += 2
self.parent.add_tex_coord(tex_coord)
faceVertexArray[i].tex_coord = len(self.parent.tex_coords) - 1
elif property[0] == 'texnumber':
current_material = self.parent.materials[int(split[offset])]
self.parent.add_face(Face(*faceVertexArray)) face = Face(*faceVertexArray)
face.material = current_material
self.parent.add_face(face)
self.counter += 1 self.counter += 1