Created
May 29, 2024 03:28
-
-
Save jj11hh/8d140ab1fea02da78c371d503e085c2b to your computer and use it in GitHub Desktop.
A script to convert CSV dump from Renderdoc into OBJ mesh format.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from argparse import ArgumentParser | |
| def main(): | |
| parser = ArgumentParser(description="Script to convert CSV to OBJ file") | |
| parser.add_argument("input", type=str, help="Input CSV file path") | |
| parser.add_argument("output", type=str, help="Output OBJ file path") | |
| parser.add_argument("--flip-y", action="store_true", help="Flip Y") | |
| parser.add_argument("--flip-uv", action="store_true", help="Flip UV") | |
| args = parser.parse_args() | |
| with open(args.input, "r") as f: | |
| lines = f.readlines() | |
| header = lines[0].strip() | |
| lines = lines[1:] # remove first line | |
| fields = {k: v for v, k in enumerate(header.split(", "))} | |
| field_idx = fields["IDX"] | |
| field_position_x = fields["position.x"] | |
| field_position_y = fields["position.y"] | |
| field_position_z = fields["position.z"] | |
| field_uv_x = fields["texcoord0.x"] | |
| field_uv_y = fields["texcoord0.y"] | |
| vertices = {} | |
| indices = [] | |
| for line in lines: | |
| values = line.split(", ") | |
| vid = int(values[field_idx]) | |
| if args.flip_y: | |
| pos = float(values[field_position_x]), 1.0-float(values[field_position_y]), float(values[field_position_z]) | |
| else: | |
| pos = float(values[field_position_x]), float(values[field_position_y]), float(values[field_position_z]) | |
| if args.flip_uv: | |
| uv = float(values[field_uv_x]), 1.0 - float(values[field_uv_y]) | |
| else: | |
| uv = float(values[field_uv_x]), float(values[field_uv_y]) | |
| vertices[vid] = (pos, uv) | |
| indices.append(vid) | |
| with open(args.output, "w") as f: | |
| for vid in range(len(vertices)): | |
| pos, _ = vertices[vid] | |
| f.write(f"v {pos[0]} {pos[1]} {pos[2]}\n") | |
| for vid in range(len(vertices)): | |
| _, uv = vertices[vid] | |
| f.write(f"vt {uv[0]} {uv[1]}\n") | |
| for tri in range(len(indices) // 3): | |
| v0 = indices[tri * 3 + 0] + 1 | |
| v1 = indices[tri * 3 + 1] + 1 | |
| v2 = indices[tri * 3 + 2] + 1 | |
| f.write(f"f {v0}/{v0} {v1}/{v1} {v2}/{v2}\n") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment