Skip to content

Instantly share code, notes, and snippets.

@Desour
Created September 30, 2022 17:07
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 Desour/91111d08b0f630211f70b111b30d027f to your computer and use it in GitHub Desktop.
Save Desour/91111d08b0f630211f70b111b30d027f to your computer and use it in GitHub Desktop.
A small script to convert a minetest nodebox to an obj wavefront file.
-- by DS, MIT
-- Note: texture coordinates are currently completely wrong and broken
-- Please insert your nodebox here:
local input_nodebox = {
type = "fixed",
fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
}
local output_filename = "out.obj"
local boxes
if input_nodebox.type == "regular" then
boxes = {{-0.5, -0.5, -0.5, 0.5, 0.5, 0.5}}
elseif input_nodebox.type == "fixed" then
if type(input_nodebox.fixed[1]) == "number" then
boxes = {input_nodebox.fixed}
else
boxes = input_nodebox.fixed
end
else
error("nodebox type not supported")
end
local verts = {}
local vtexs = {}
local faces = {}
for i = 1, #boxes do
local box = boxes[i]
local verts_b = i*8-7
local vtexs_b = i*12-11
verts[verts_b+0+0+0] = {box[1], box[2], box[3]}
verts[verts_b+1+0+0] = {box[4], box[2], box[3]}
verts[verts_b+0+2+0] = {box[1], box[5], box[3]}
verts[verts_b+1+2+0] = {box[4], box[5], box[3]}
verts[verts_b+0+0+4] = {box[1], box[2], box[6]}
verts[verts_b+1+0+4] = {box[4], box[2], box[6]}
verts[verts_b+0+2+4] = {box[1], box[5], box[6]}
verts[verts_b+1+2+4] = {box[4], box[5], box[6]}
vtexs[vtexs_b+0+0+0] = {box[3], box[2]} -- x-dir (textures are upright => -y world is -y uv)
vtexs[vtexs_b+1+0+0] = {box[6], box[2]}
vtexs[vtexs_b+0+2+0] = {box[3], box[5]}
vtexs[vtexs_b+1+2+0] = {box[6], box[5]}
vtexs[vtexs_b+0+0+4] = {box[3], box[1]} -- y-dir (this is probably broken, idk how minetest orders it)
vtexs[vtexs_b+1+0+4] = {box[6], box[1]}
vtexs[vtexs_b+0+2+4] = {box[3], box[4]}
vtexs[vtexs_b+1+2+4] = {box[6], box[4]}
vtexs[vtexs_b+0+0+8] = {box[1], box[2]} -- z-dir (upright again)
vtexs[vtexs_b+1+0+8] = {box[4], box[2]}
vtexs[vtexs_b+0+2+8] = {box[1], box[5]}
vtexs[vtexs_b+1+2+8] = {box[4], box[5]}
local flen = #faces
faces[flen+ 1] = {verts_b+0+0+0, verts_b+0+0+4, verts_b+0+2+4, vtexs_b+0+0+0, vtexs_b+1+0+0, vtexs_b+1+2+0} -- from -x
faces[flen+ 2] = {verts_b+0+0+0, verts_b+0+2+4, verts_b+0+2+0, vtexs_b+0+0+0, vtexs_b+1+2+0, vtexs_b+0+2+0}
faces[flen+ 3] = {verts_b+0+0+0, verts_b+1+0+0, verts_b+1+0+4, vtexs_b+0+0+8, vtexs_b+0+0+4, vtexs_b+0+0+4} -- from -y
faces[flen+ 4] = {verts_b+0+0+0, verts_b+1+0+4, verts_b+0+0+4, vtexs_b+0+0+8, vtexs_b+0+0+4, vtexs_b+0+0+4}
faces[flen+ 5] = {verts_b+0+0+0, verts_b+1+2+0, verts_b+1+0+0, vtexs_b+0+0+4, vtexs_b+1+2+8, vtexs_b+1+0+8} -- from -z
faces[flen+ 6] = {verts_b+0+0+0, verts_b+0+2+0, verts_b+1+2+0, vtexs_b+0+0+4, vtexs_b+0+2+8, vtexs_b+1+2+8}
faces[flen+ 7] = {verts_b+1+2+4, verts_b+1+0+0, verts_b+1+2+0, vtexs_b+1+2+0, vtexs_b+0+0+0, vtexs_b+0+2+0} -- from +x
faces[flen+ 8] = {verts_b+1+2+4, verts_b+1+0+4, verts_b+1+0+0, vtexs_b+1+2+0, vtexs_b+1+0+0, vtexs_b+0+0+0}
faces[flen+ 9] = {verts_b+1+2+4, verts_b+0+2+0, verts_b+0+2+4, vtexs_b+1+2+4, vtexs_b+0+2+4, vtexs_b+0+2+4} -- from +y
faces[flen+10] = {verts_b+1+2+4, verts_b+1+2+0, verts_b+0+2+0, vtexs_b+1+2+4, vtexs_b+0+2+4, vtexs_b+0+2+4}
faces[flen+11] = {verts_b+1+2+4, verts_b+0+2+4, verts_b+0+0+4, vtexs_b+1+2+8, vtexs_b+0+2+8, vtexs_b+0+0+8} -- from +z
faces[flen+12] = {verts_b+1+2+4, verts_b+0+0+4, verts_b+1+0+4, vtexs_b+1+2+8, vtexs_b+0+0+8, vtexs_b+1+0+8}
end
local file = assert(io.open(output_filename, "w"))
do
local verts_s = {}
for i, v in ipairs(verts) do
-- use %s because it produces shorter strings than %f
verts_s[i] = string.format("v %s %s %s\n", v[1], v[2], v[3])
end
file:write(table.concat(verts_s))
end
do
local vtexs_s = {}
for i, vt in ipairs(vtexs) do
-- use %s because it produces shorter strings than %f
vtexs_s[i] = string.format("vt %s %s\n", vt[1], vt[2])
end
file:write("\n", table.concat(vtexs_s))
end
do
local faces_s = {}
for i, f in ipairs(faces) do
faces_s[i] = string.format("f %d/%d %d/%d %d/%d\n", f[1], f[4], f[2], f[5], f[3], f[6])
end
file:write("\n", table.concat(faces_s))
end
file:close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment