-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmesh_to_triangle_soup.py
More file actions
41 lines (37 loc) · 1.24 KB
/
mesh_to_triangle_soup.py
File metadata and controls
41 lines (37 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python
import sys, random
vertices = []
faces = []
default_perturbation = 0.0
if len(sys.argv) == 4:
perturbation = float(sys.argv[3])
elif len(sys.argv) == 3:
perturbation = default_perturbation
print(f"Using default perturbation amount: {perturbation}")
sys.exit()
else:
print(f"Usage: python distort_obj.py <input.obj> <output.obj> [<perturbation> (default: {default_perturbation})]")
sys.exit()
with open(sys.argv[1], "r") as file:
lines = file.read().split("\n")
outputobj = ""
for line in lines:
if line.startswith("v"):
parts = line.split(" ")
vertices.append([float(x) for x in parts[1:]])
if line.startswith("f"):
parts = line.split(" ")
faces.append([int(x) for x in parts[1:]])
for face in faces:
for idx in face:
vertex = [x+random.random()*perturbation for x in vertices[idx-1]]
outputobj += "v " + " ".join([str(x) for x in vertex]) + "\n"
v_idx = 1
for i in range(len(faces)):
outputobj += "f"
for idx in faces[i]:
outputobj += f" {v_idx:d}"
v_idx += 1
outputobj += "\n"
with open(sys.argv[2], "w") as outfile:
outfile.write(outputobj)