Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # author : Jorge Aldo Gomes de Freitas Junior (jorgealdojr@hotmail.com)
- # released under the same license as sproxel, whatever it is...
- import sproxel
- import os
- plugin_info=dict(
- name='Minetest NodeBox Export Plugin'
- )
- def register():
- sproxel.register_exporter(TilesExporter)
- def unregister():
- sproxel.unregister_exporter(TilesExporter)
- def writeppm(fn, xmax, ymax, buffer):
- f = open(fn, 'wb')
- f.write("P3\n")
- f.write("# " + fn + "\n")
- f.write(str(xmax))
- f.write(" ")
- f.write(str(ymax))
- f.write("\n")
- f.write("255\n")
- for x in xrange(0, xmax):
- for y in xrange(0, ymax):
- f.write(str(int(buffer[x][y][0] * 255)))
- f.write(" ")
- f.write(str(int(buffer[x][y][1] * 255)))
- f.write(" ")
- f.write(str(int(buffer[x][y][2] * 255)))
- f.write(" ")
- f.write("\n")
- f.close
- return
- class TilesExporter(object):
- @staticmethod
- def name():
- return 'Minetest nodebox file'
- @staticmethod
- def filter():
- return '*.*'
- @staticmethod
- def doExport(fn, prj, cur_sprite):
- fn = os.path.basename(fn)
- if not os.path.exists(fn): os.makedirs(fn)
- if not os.path.exists(fn + "/textures"): os.makedirs(fn + "/textures")
- # node texture names
- luaname = fn + "/init.lua"
- topname = fn + "/textures/" + fn + "_texturetop.png"
- bottomname = fn + "/textures/" + fn + "_texturebottom.png"
- frontname = fn + "/textures/" + fn + "_texturefront.png"
- backname = fn + "/textures/" + fn + "_textureback.png"
- leftname = fn + "/textures/" + fn + "_textureleft.png"
- rightname = fn + "/textures/" + fn + "_textureright.png"
- # sprite constants
- # get object size
- bounds = cur_sprite.bounds
- minx = bounds[0][0]
- miny = bounds[0][1]
- minz = bounds[0][2]
- maxx = bounds[1][0]
- maxy = bounds[1][1]
- maxz = bounds[1][2]
- sizex = maxx - minx + 1
- sizey = maxy - miny + 1
- sizez = maxz - minz + 1
- # nodebox constants
- minnodeboxx = -0.5
- minnodeboxy = -0.5
- minnodeboxz = -0.5
- maxnodeboxx = 0.5
- maxnodeboxy = 0.5
- maxnodeboxz = 0.5
- sizenodeboxx = maxnodeboxx - minnodeboxx
- sizenodeboxy = maxnodeboxy - minnodeboxy
- sizenodeboxz = maxnodeboxz - minnodeboxz
- stepnodeboxx = sizenodeboxx / sizex
- stepnodeboxy = sizenodeboxy / sizey
- stepnodeboxz = sizenodeboxz / sizez
- # prealloc textures
- mttoptexture = [[[0, 0, 0] for i in range(0, sizex + 1)] for j in range(0, sizez + 1)]
- mtbottomtexture = [[[0, 0, 0] for i in range(0, sizex + 1)] for j in range(0, sizez + 1)]
- mtfronttexture = [[[0, 0, 0] for i in range(0, sizex + 1)] for j in range(0, sizey + 1)]
- mtbacktexture = [[[0, 0, 0] for i in range(0, sizex + 1)] for j in range(0, sizey + 1)]
- mtlefttexture = [[[0, 0, 0] for i in range(0, sizey + 1)] for j in range(0, sizez + 1)]
- mtrighttexture = [[[0, 0, 0] for i in range(0, sizey + 1)] for j in range(0, sizez + 1)]
- # generate toptexture
- for x in xrange(minx, maxx + 1):
- for z in xrange(minz, maxz + 1):
- for y in xrange(miny, maxy + 1):
- r, g, b, a = cur_sprite.getColor(x, y, z)
- if not r + g + b == 0:
- mttoptexture[x][z] = [r, g, b]
- # generate bottomtexture
- for x in xrange(minx, maxx + 1):
- for z in xrange(minz, maxz + 1):
- for y in xrange(maxy, miny - 1, -1):
- r, g, b, a = cur_sprite.getColor(x, y, z)
- if not r + g + b == 0:
- mtbottomtexture[x][z] = [r, g, b]
- # generate fronttexture
- for x in xrange(minx, maxx + 1):
- for y in xrange(miny, maxy + 1):
- for z in xrange(minz, maxz + 1):
- r, g, b, a = cur_sprite.getColor(x, y, z)
- if not r + g + b == 0:
- mtfronttexture[x][y] = [r, g, b]
- # generate backtexture
- for x in xrange(minx, maxx + 1):
- for y in xrange(miny, maxy + 1):
- for z in xrange(maxz, minz - 1, -1):
- r, g, b, a = cur_sprite.getColor(x, y, z)
- if not r + g + b == 0:
- mtbacktexture[x][y] = [r, g, b]
- # generate lefttexture
- for y in xrange(miny, maxy + 1):
- for z in xrange(minz, maxz + 1):
- for x in xrange(minx, maxx + 1):
- r, g, b, a = cur_sprite.getColor(x, y, z)
- if not r + g + b == 0:
- mtlefttexture[z][y] = [r, g, b]
- # generate righttexture
- for y in xrange(miny, maxy + 1):
- for z in xrange(minz, maxz + 1):
- for x in xrange(maxx, minx - 1, -1):
- r, g, b, a = cur_sprite.getColor(x, y, z)
- if not r + g + b == 0:
- mtrighttexture[z][y] = [r, g, b]
- # write header
- f = open(luaname, 'wb')
- f.write("minetest.register_node('" + fn + ":yournode', {\n")
- f.write(" description = ('yournode'),\n")
- f.write(" drawtype = 'nodebox',\n")
- f.write(" tiles = {\n")
- f.write(" '" + fn + "_texturetop.ppm',\n")
- f.write(" '" + fn + "_texturebottom.ppm',\n")
- f.write(" '" + fn + "_textureleft.ppm',\n")
- f.write(" '" + fn + "_textureright.ppm',\n")
- f.write(" '" + fn + "_texturefront.ppm',\n")
- f.write(" '" + fn + "_textureback.ppm',\n")
- f.write(" },\n")
- f.write(" selection_box = {\n")
- f.write(" type = 'fixed',\n")
- f.write(" fixed = { -1/3, -1/3, -1/3, 1/3, 1/3, 1/3 }\n")
- f.write(" },\n")
- f.write(" node_box = {\n")
- f.write(" type = 'fixed',\n")
- f.write(" fixed = {\n")
- # write nodebox data
- for x in xrange(minx, maxx + 1):
- for z in xrange(minz, maxz + 1):
- for y in xrange(miny, maxy + 1):
- r, g, b, a = cur_sprite.getColor(x, y, z)
- if not r + g + b == 0:
- f.write(" {")
- f.write(str((x * stepnodeboxx) + minnodeboxx))
- f.write(", ")
- f.write(str((y * stepnodeboxy) + minnodeboxy))
- f.write(", ")
- f.write(str((z * stepnodeboxz) + minnodeboxz))
- f.write(", ")
- f.write(str(((x + 1) * stepnodeboxx) + minnodeboxx))
- f.write(", ")
- f.write(str(((y + 1) * stepnodeboxy) + minnodeboxy))
- f.write(", ")
- f.write(str(((z + 1) * stepnodeboxz) + minnodeboxz))
- f.write("},\n")
- # write footer
- f.write(" },\n")
- f.write(" },\n")
- f.write("})\n")
- f.close()
- # create textures
- # write toptexture
- writeppm(topname, sizex, sizez, mttoptexture)
- # write bottomtexture
- writeppm(bottomname, sizex, sizez, mtbottomtexture)
- # write fronttexture
- writeppm(frontname, sizex, sizey, mtfronttexture)
- # write backtexture
- writeppm(backname, sizex, sizey, mtbacktexture)
- # write lefttexture
- writeppm(leftname, sizey, sizez, mtlefttexture)
- # write righttexture
- writeppm(rightname, sizey, sizez, mtrighttexture)
- return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement