Export ply supports texture
This commit is contained in:
+10
-10
@@ -42,7 +42,7 @@ class OBJParser(TextModelParser):
|
||||
self.mtl = MTLParser(self)
|
||||
self.mtl.parse_file(path)
|
||||
else:
|
||||
print('Warning : ' + path + 'not found ', file=sys.stderr)
|
||||
print('Warning : ' + path + ' not found ', file=sys.stderr)
|
||||
elif first == 'v':
|
||||
self.add_vertex(Vertex().from_array(split))
|
||||
elif first == 'vn':
|
||||
@@ -55,7 +55,11 @@ class OBJParser(TextModelParser):
|
||||
for i in range(len(splits)):
|
||||
for j in range(len(splits[i])):
|
||||
if splits[i][j] is not '':
|
||||
splits[i][j] = int(splits[i][j]) - 1
|
||||
splits[i][j] = int(splits[i][j])
|
||||
if splits[i][j] > 0:
|
||||
splits[i][j] -= 1
|
||||
else:
|
||||
splits[i][j] = len(self.vertices) + splits[i][j]
|
||||
|
||||
# if Face3
|
||||
if len(split) == 3:
|
||||
@@ -83,7 +87,6 @@ class MTLParser:
|
||||
:param parent: the OBJParser this MTLParser refers to
|
||||
"""
|
||||
self.parent = parent
|
||||
self.materials = []
|
||||
self.current_mtl = None
|
||||
|
||||
def parse_line(self, string):
|
||||
@@ -101,7 +104,7 @@ class MTLParser:
|
||||
|
||||
if first == 'newmtl':
|
||||
self.current_mtl = Material(split[0])
|
||||
self.materials.append(self.current_mtl)
|
||||
self.parent.materials.append(self.current_mtl)
|
||||
elif first == 'Ka':
|
||||
self.current_mtl.Ka = Vertex().from_array(split)
|
||||
elif first == 'Kd':
|
||||
@@ -109,11 +112,8 @@ class MTLParser:
|
||||
elif first == 'Ks':
|
||||
self.current_mtl.Ks = Vertex().from_array(split)
|
||||
elif first == 'map_Kd':
|
||||
try:
|
||||
import PIL.Image
|
||||
self.current_mtl.map_Kd = PIL.Image.open(os.path.join(os.path.dirname(self.parent.path), split[0]))
|
||||
except:
|
||||
pass
|
||||
self.current_mtl.relative_path_to_texture = split[0]
|
||||
self.current_mtl.absolute_path_to_texture = os.path.join(os.path.dirname(self.parent.path), split[0])
|
||||
|
||||
|
||||
def parse_file(self, path):
|
||||
@@ -123,7 +123,7 @@ class MTLParser:
|
||||
self.parse_line(line)
|
||||
|
||||
def __getitem__(self, key):
|
||||
for material in self.materials:
|
||||
for material in self.parent.materials:
|
||||
if material.name == key:
|
||||
return material
|
||||
|
||||
|
||||
+25
-9
@@ -21,7 +21,7 @@ def _ply_type_size(type):
|
||||
"""Returns the size of a ply property
|
||||
|
||||
:param type: a string that is in a ply element
|
||||
""""
|
||||
"""
|
||||
if type == 'char' or type == 'uchar':
|
||||
return 1
|
||||
elif type == 'short' or type == 'ushort':
|
||||
@@ -86,7 +86,6 @@ class PLYParser(ModelParser):
|
||||
super().__init__(up_conversion)
|
||||
self.counter = 0
|
||||
self.elements = []
|
||||
self.materials = []
|
||||
self.inner_parser = PLYHeaderParser(self)
|
||||
self.beginning_of_line = ''
|
||||
self.header_finished = False
|
||||
@@ -156,11 +155,8 @@ class PLYHeaderParser:
|
||||
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 ImportError:
|
||||
pass
|
||||
material.relative_path_to_texture = split[2]
|
||||
material.absolute_path_to_texture = os.path.join(os.path.dirname(self.parent.path), split[2])
|
||||
|
||||
class PLYElement:
|
||||
def __init__(self, name, number):
|
||||
@@ -258,6 +254,7 @@ class PLY_ASCII_ContentParser:
|
||||
|
||||
elif property[0] == 'texnumber':
|
||||
current_material = self.parent.materials[int(split[offset])]
|
||||
offset += 1
|
||||
|
||||
face = Face(*faceVertexArray)
|
||||
face.material = current_material
|
||||
@@ -454,13 +451,20 @@ class PLYExporter(Exporter):
|
||||
# Header
|
||||
string = "ply\nformat ascii 1.0\ncomment Automatically gnerated by model-converter\n"
|
||||
|
||||
for material in self.model.materials:
|
||||
string += "comment TextureFile " + material.relative_path_to_texture + "\n"
|
||||
|
||||
# Types : vertices
|
||||
string += "element vertex " + str(len(self.model.vertices)) +"\n"
|
||||
string += "property float x\nproperty float y\nproperty float z\n"
|
||||
|
||||
# Types : faces
|
||||
string += "element face " + str(len(faces)) + "\n"
|
||||
string += "property list uint8 int32 vertex_indices\n"
|
||||
string += "property list uchar int vertex_indices\n"
|
||||
|
||||
if len(self.model.tex_coords) > 0:
|
||||
string += "property list uchar float texcoord\n"
|
||||
string += "property int texnumber\n"
|
||||
|
||||
# End header
|
||||
string += "end_header\n"
|
||||
@@ -470,7 +474,19 @@ class PLYExporter(Exporter):
|
||||
string += str(vertex.x) + " " + str(vertex.y) + " " + str(vertex.z) + "\n"
|
||||
|
||||
for face in faces:
|
||||
string += "3 " + str(face.a.vertex) + " " + str(face.b.vertex) + " " + str(face.c.vertex) + "\n"
|
||||
string += "3 " + str(face.a.vertex) + " " + str(face.b.vertex) + " " + str(face.c.vertex)
|
||||
|
||||
if len(self.model.tex_coords) > 0:
|
||||
string += " 6 " \
|
||||
+ str(self.model.tex_coords[face.a.tex_coord].x) + " " \
|
||||
+ str(self.model.tex_coords[face.a.tex_coord].y) + " " \
|
||||
+ str(self.model.tex_coords[face.b.tex_coord].x) + " " \
|
||||
+ str(self.model.tex_coords[face.b.tex_coord].y) + " " \
|
||||
+ str(self.model.tex_coords[face.c.tex_coord].x) + " " \
|
||||
+ str(self.model.tex_coords[face.c.tex_coord].y) + " " \
|
||||
+ str(self.model.get_material_index(face.material))
|
||||
|
||||
string += "\n"
|
||||
|
||||
return string
|
||||
|
||||
|
||||
Reference in New Issue
Block a user