diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2019-01-27 16:48:47 +0100 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2019-01-27 16:48:47 +0100 |
| commit | fb8a697cece8fc9f3b07f314d0988b6e354664bf (patch) | |
| tree | ea5a3bf4d630f6470b06d9b342cce9965728759d /megapixels/commands/misc/obj2ply.py | |
| parent | b0b06be0defe97ef19cf4d0f3328db40d299e110 (diff) | |
splash page init, add obj2ply script
Diffstat (limited to 'megapixels/commands/misc/obj2ply.py')
| -rw-r--r-- | megapixels/commands/misc/obj2ply.py | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/megapixels/commands/misc/obj2ply.py b/megapixels/commands/misc/obj2ply.py new file mode 100644 index 00000000..e3e18e54 --- /dev/null +++ b/megapixels/commands/misc/obj2ply.py @@ -0,0 +1,79 @@ +#!/usr/bin/python + +""" +Convert an OBJ 3D model (with vertex color) to a PLY file, which can be read by the draco_encoder. +""" + +import click + +@click.command() +@click.add_argument('--float_colors', action='store_true', help='pass if RGB colors are floats, not ints, in the obj') +@click.add_argument('--unwind', action='store_true', help='pass to reverse winding order on faces (if surface normals are upside down)') +@click.add_argument('--flip_y', action='store_true', help='flip Y axis') +@click.add_argument('-i', '--input_fn', required=True, help='input OBJ filename') +@click.add_argument('-o', '--output_fn', help='output PLY filename') +@click.pass_context +def cli(ctx, float_colors, unwind, flip_y, input_fn, output_fn): + """ + click command for converting OBJ to PLY + """ + + ply_header = """ply + format ascii 1.0 + element vertex {} + property float x + property float y + property float z + property uchar red + property uchar green + property uchar blue + element face {} + property list uchar int vertex_index + end_header + """ + + if output_fn is None: + output_fn = input_fn.replace('.obj', '.ply') + + with open(input_fn, 'r') as f: + i = 0 + vertexes = [] + faces = [] + for line in f.readlines(): + N = line.strip().split(' ') + if N[0] == 'v': + if flip_y: + N[2] = str(float(N[2]) * -1) + if float_colors: + vertexes.append([ + N[1], + N[2], + N[3], + str(int(255 * float(N[4]))), + str(int(255 * float(N[5]))), + str(int(255 * float(N[6]))), + ]) + else: + vertexes.append(N[1:]) + if N[0] == 'f': + if unwind: + faces.append([ + "3", + str(int(N[3]) - 1), + str(int(N[2]) - 1), + str(int(N[1]) - 1), + ]) + else: + faces.append([ + "3", + str(int(N[1]) - 1), + str(int(N[2]) - 1), + str(int(N[3]) - 1), + ]) + + with open(output_fn, 'w') as out_file: + out_file.write(ply_header.format(len(vertexes), len(faces))) + for v in vertexes: + out_file.write(" ".join(v) + "\n") + for f in faces: + out_file.write(" ".join(f) + "\n") |
