Skip to content

Instantly share code, notes, and snippets.

@jeandrek
Last active December 30, 2025 07:44
Show Gist options
  • Select an option

  • Save jeandrek/56b04c480aa2ca29d36a2434f23f9a3a to your computer and use it in GitHub Desktop.

Select an option

Save jeandrek/56b04c480aa2ca29d36a2434f23f9a3a to your computer and use it in GitHub Desktop.
f = open('dog.obj','r')
verts = []
uvs = []
normals = []
faces = []
for line in f:
if line[:2] == 'v ':
verts.append(tuple(map(lambda s:float(s),line.split()[1:])))
elif line[:3] == 'vt ':
uvs.append(tuple(map(lambda s:float(s),line.split()[1:])))
elif line[:3] == 'vn ':
normals.append(tuple(map(lambda s:float(s),line.split()[1:])))
elif line[:2] == 'f ':
faces.append(list(map(lambda s:tuple(map(lambda t:int(t),s.split('/'))),line.split()[1:])))
f.close()
print(f"""{len(verts)} vertices
{len(normals)} normals
{len(uvs)} tex coords
{len(faces)} faces""")
with open('vertices.txt','w') as verts_file:
with open('texcoords.txt', 'w') as uvs_file:
with open('normals.txt', 'w') as normals_file:
for face in faces:
for (i, j, k) in face:
vert = verts[i - 1]
uv = uvs[j - 1]
normal = normals[k - 1]
for coord in vert:
verts_file.write(f"{coord}\n")
for coord in normal:
normals_file.write(f"{coord}\n")
for coord in uv:
uvs_file.write(f"{coord}\n")
print("Written to vertices.txt/normals.txt/texcoords.txt")
import sys
value_size = 3
def rle_compress(data):
result = bytearray()
run_val = int.from_bytes(data[0:value_size])
run_length = 0
index = 0
while index < len(data):
val = int.from_bytes(data[index:index + value_size])
if val == run_val and run_length < 0xff:
run_length += 1
else:
result.append(run_length)
result += run_val.to_bytes(value_size)
run_val = val
run_length = 1
index += value_size
result.append(run_length)
result += run_val.to_bytes(value_size)
return bytes(result)
for path in sys.argv[1:]:
with open(path, 'rb') as f:
data = f.read()
result = rle_compress(data)
index = 0
while index < len(result):
print(hex(int.from_bytes(result[index:index + 4])))
index += 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment