Skip to content

Instantly share code, notes, and snippets.

@Eiyeron
Last active December 1, 2020 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Eiyeron/dfde6ebc5c6ed96fa3b03eb018cef092 to your computer and use it in GitHub Desktop.
Save Eiyeron/dfde6ebc5c6ed96fa3b03eb018cef092 to your computer and use it in GitHub Desktop.
New Pico-8 exporter (works with meshes bound in [(-1;-1;-1),(1;1;1)] )
function parse_vert(str, i)
local s = sub(str, (i-1)*5+1, (i-1)*5+5)
local v = sub(s, 1, 1)
local res = v.."0x0."..sub(s,2)
return tonum(res)
end
function load_verts(str)
local verts = {}
for v=0,#str/15-1 do
local x, y, z = parse_vert(str, v*3+1), parse_vert(str, v*3+2), parse_vert(str, v*3+3)
add(verts, {x, y, z})
end
return verts
end
function load_edges(str)
local edges = {}
for v=0,#str/6-1 do
add(edges, {tonum(sub(str,v*6+1,v*6+3)), tonum(sub(str,v*6+4,v*6+6))})
end
return edges
end
import bpy
def hex_repr(v):
vf = int(v*65535)
if not (vf >= -0xFFFF and vf <= 0xFFFF):
raise AssertionError(f"{v} doesn't fall in the range")
if vf < 0:
vf = -vf
return f"-{vf:04x}"
return f" {vf:04x}"
def do_magic():
current_object = bpy.context.view_layer.objects.active
if current_object.type != "MESH":
print("Make sure that the selected object is a mesh")
return
mesh = current_object.data
result = ""
print(f"{len(mesh.vertices)} vertices")
for vertex in mesh.vertices:
x, y, z = vertex.co
line = "{}{}{}".format(hex_repr(x), hex_repr(y), hex_repr(z))
result += line + ""
with open("c:/tmp/out.vert", "w") as f:
f.write(result)
result = ""
print(f"{len(mesh.edges)} edges")
for edge in mesh.edges:
a, b = edge.vertices
a += 1
b += 1
result += f"{a:03d}{b:03d}"
with open("c:/tmp/out.edge", "w") as f:
f.write(result)
do_magic()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment