Advertisement
Kitomas

primitive version of my proprietary 3d format

Jun 28th, 2023
1,127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | Source Code | 0 0
  1. from sys import argv
  2. from struct import pack
  3. from math import ceil,log
  4. if len(argv) < 2: print("no file name given!"); exit()
  5. vertices,faces=[],[]; byteBuff=b''; lineNum=1; amountMax=0
  6. fileI=open(argv[1],"r")
  7. for l in fileI:
  8.     line=l[:-1].split(" ")
  9.     if line[0] == "v":
  10.         byteBuff=b''
  11.         for v in range(1,len(line)): byteBuff+=pack("f", float(line[v]))
  12.         vertices.append(byteBuff)
  13.     elif line[0] == "f":
  14.         faces.append([]); last=len(faces)-1
  15.         for index in range(1,len(line)): faces[last].append( int(line[index].split("/")[0])-1 )
  16.         if len(faces[last]) != 3: print(("line {}: face is not a triangle").format(lineNum)); exit()
  17.     lineNum+=1
  18. fileI.close(); fileO=open(argv[1].split(".")[0]+".rtr","wb")
  19. for face in faces: amountMax=max(amountMax or 0,face[0],face[1],face[2])
  20. bc=ceil(log(amountMax,2)/8)
  21. fileO.write(bc.to_bytes(1,"little")+len(vertices).to_bytes(4,"little")+len(faces).to_bytes(4,"little"))
  22. for vertex in vertices: fileO.write(vertex)
  23. for face in faces: fileO.write(face[0].to_bytes(bc,"little")+face[1].to_bytes(bc,"little")+face[2].to_bytes(bc,"little"))
  24. fileO.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement