summaryrefslogtreecommitdiff
path: root/megapixels/commands
diff options
context:
space:
mode:
authoradamhrv <adam@ahprojects.com>2019-01-28 18:11:52 +0100
committeradamhrv <adam@ahprojects.com>2019-01-28 18:11:52 +0100
commitf8b279b2f0be793c5f877cac6373332954c6a5de (patch)
tree60be7caeb0e8c96c59718300c68d5b1cdecde6f4 /megapixels/commands
parentdd2c36288aa1e8af14588f9258f6785879b8638c (diff)
parentb0eb2d9672044a1b64a2a1f21540f9ef1bd7b571 (diff)
Merge branch 'master' of github.com:adamhrv/megapixels_dev
Diffstat (limited to 'megapixels/commands')
-rw-r--r--megapixels/commands/misc/obj2ply.py79
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..8d0cec60
--- /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.option('-c', '--float_colors/--int_colors', 'float_colors', default=False, help='pass if RGB colors are floats, not ints, in the obj')
+@click.option('-u', '--unwind/--wind', 'unwind', default=False, help='pass to reverse winding order on faces (if surface normals are upside down)')
+@click.option('-y', '--flip_y/--no_flip_y', 'flip_y', default=False, help='flip Y axis')
+@click.option('-i', '--input_fn', required=True, help='input OBJ filename')
+@click.option('-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")