Advertisement
Guest User

Untitled

a guest
Aug 27th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.41 KB | None | 0 0
  1. # author : Jorge Aldo Gomes de Freitas Junior (jorgealdojr@hotmail.com)
  2. # released under the same license as sproxel, whatever it is...
  3.  
  4. import sproxel
  5. import os
  6.  
  7. plugin_info=dict(
  8.     name='Minetest NodeBox Export Plugin'
  9.     )
  10.  
  11. def register():
  12.     sproxel.register_exporter(TilesExporter)
  13.  
  14. def unregister():
  15.     sproxel.unregister_exporter(TilesExporter)
  16.    
  17. def writeppm(fn, xmax, ymax, buffer):
  18.     f = open(fn, 'wb')
  19.     f.write("P3\n")
  20.     f.write("# " + fn + "\n")
  21.     f.write(str(xmax))
  22.     f.write(" ")
  23.     f.write(str(ymax))
  24.     f.write("\n")
  25.     f.write("255\n")
  26.     for x in xrange(0, xmax):
  27.         for y in xrange(0, ymax):
  28.             f.write(str(int(buffer[x][y][0] * 255)))
  29.             f.write(" ")
  30.             f.write(str(int(buffer[x][y][1] * 255)))
  31.             f.write(" ")
  32.             f.write(str(int(buffer[x][y][2] * 255)))
  33.             f.write(" ")
  34.         f.write("\n")
  35.     f.close
  36.     return
  37.  
  38. class TilesExporter(object):
  39.     @staticmethod
  40.     def name():
  41.         return 'Minetest nodebox file'
  42.  
  43.     @staticmethod
  44.     def filter():
  45.         return '*.*'
  46.  
  47.     @staticmethod
  48.     def doExport(fn, prj, cur_sprite):
  49.    
  50.         fn = os.path.basename(fn)
  51.  
  52.         if not os.path.exists(fn): os.makedirs(fn)
  53.         if not os.path.exists(fn + "/textures"): os.makedirs(fn + "/textures")
  54.        
  55.         # node texture names
  56.         luaname = fn + "/init.lua"
  57.         topname = fn + "/textures/" + fn + "_texturetop.png"
  58.         bottomname = fn + "/textures/" + fn + "_texturebottom.png"
  59.         frontname = fn + "/textures/" + fn + "_texturefront.png"
  60.         backname = fn + "/textures/" + fn + "_textureback.png"
  61.         leftname = fn + "/textures/" + fn + "_textureleft.png"
  62.         rightname = fn + "/textures/" + fn + "_textureright.png"
  63.  
  64.         # sprite constants
  65.         # get object size
  66.         bounds = cur_sprite.bounds
  67.         minx = bounds[0][0]
  68.         miny = bounds[0][1]
  69.         minz = bounds[0][2]
  70.         maxx = bounds[1][0]
  71.         maxy = bounds[1][1]
  72.         maxz = bounds[1][2]
  73.         sizex = maxx - minx + 1
  74.         sizey = maxy - miny + 1
  75.         sizez = maxz - minz + 1
  76.        
  77.         # nodebox constants
  78.         minnodeboxx = -0.5
  79.         minnodeboxy = -0.5
  80.         minnodeboxz = -0.5
  81.         maxnodeboxx = 0.5
  82.         maxnodeboxy = 0.5
  83.         maxnodeboxz = 0.5
  84.         sizenodeboxx = maxnodeboxx - minnodeboxx
  85.         sizenodeboxy = maxnodeboxy - minnodeboxy
  86.         sizenodeboxz = maxnodeboxz - minnodeboxz
  87.         stepnodeboxx = sizenodeboxx / sizex
  88.         stepnodeboxy = sizenodeboxy / sizey
  89.         stepnodeboxz = sizenodeboxz / sizez
  90.  
  91.         # prealloc textures
  92.        
  93.         mttoptexture = [[[0, 0, 0] for i in range(0, sizex + 1)] for j in range(0, sizez + 1)]
  94.         mtbottomtexture = [[[0, 0, 0] for i in range(0, sizex + 1)] for j in range(0, sizez + 1)]
  95.         mtfronttexture = [[[0, 0, 0] for i in range(0, sizex + 1)] for j in range(0, sizey + 1)]
  96.         mtbacktexture = [[[0, 0, 0] for i in range(0, sizex + 1)] for j in range(0, sizey + 1)]
  97.         mtlefttexture = [[[0, 0, 0] for i in range(0, sizey + 1)] for j in range(0, sizez + 1)]
  98.         mtrighttexture = [[[0, 0, 0] for i in range(0, sizey + 1)] for j in range(0, sizez + 1)]
  99.        
  100.         # generate toptexture
  101.         for x in xrange(minx, maxx + 1):
  102.             for z in xrange(minz, maxz + 1):
  103.                 for y in xrange(miny, maxy + 1):
  104.                     r, g, b, a = cur_sprite.getColor(x, y, z)
  105.                     if not r + g + b == 0:
  106.                         mttoptexture[x][z] = [r, g, b]
  107.  
  108.         # generate bottomtexture
  109.         for x in xrange(minx, maxx + 1):
  110.             for z in xrange(minz, maxz + 1):
  111.                 for y in xrange(maxy, miny - 1, -1):
  112.                     r, g, b, a = cur_sprite.getColor(x, y, z)
  113.                     if not r + g + b == 0:
  114.                         mtbottomtexture[x][z] = [r, g, b]
  115.  
  116.         # generate fronttexture
  117.         for x in xrange(minx, maxx + 1):
  118.             for y in xrange(miny, maxy + 1):
  119.                 for z in xrange(minz, maxz + 1):
  120.                     r, g, b, a = cur_sprite.getColor(x, y, z)
  121.                     if not r + g + b == 0:
  122.                         mtfronttexture[x][y] = [r, g, b]
  123.  
  124.         # generate backtexture
  125.         for x in xrange(minx, maxx + 1):
  126.             for y in xrange(miny, maxy + 1):
  127.                 for z in xrange(maxz, minz - 1, -1):
  128.                     r, g, b, a = cur_sprite.getColor(x, y, z)
  129.                     if not r + g + b == 0:
  130.                         mtbacktexture[x][y] = [r, g, b]
  131.  
  132.         # generate lefttexture
  133.         for y in xrange(miny, maxy + 1):
  134.             for z in xrange(minz, maxz + 1):
  135.                 for x in xrange(minx, maxx + 1):
  136.                     r, g, b, a = cur_sprite.getColor(x, y, z)
  137.                     if not r + g + b == 0:
  138.                         mtlefttexture[z][y] = [r, g, b]
  139.  
  140.         # generate righttexture
  141.         for y in xrange(miny, maxy + 1):
  142.             for z in xrange(minz, maxz + 1):
  143.                 for x in xrange(maxx, minx - 1, -1):
  144.                     r, g, b, a = cur_sprite.getColor(x, y, z)
  145.                     if not r + g + b == 0:
  146.                         mtrighttexture[z][y] = [r, g, b]
  147.  
  148.         # write header
  149.         f = open(luaname, 'wb')
  150.         f.write("minetest.register_node('" + fn + ":yournode', {\n")
  151.         f.write("  description = ('yournode'),\n")
  152.         f.write("  drawtype = 'nodebox',\n")
  153.         f.write("  tiles = {\n")
  154.         f.write("    '" + fn + "_texturetop.ppm',\n")
  155.         f.write("    '" + fn + "_texturebottom.ppm',\n")
  156.         f.write("    '" + fn + "_textureleft.ppm',\n")
  157.         f.write("    '" + fn + "_textureright.ppm',\n")
  158.         f.write("    '" + fn + "_texturefront.ppm',\n")
  159.         f.write("    '" + fn + "_textureback.ppm',\n")
  160.         f.write("  },\n")
  161.         f.write("  selection_box = {\n")
  162.         f.write("    type = 'fixed',\n")
  163.         f.write("    fixed = { -1/3, -1/3, -1/3, 1/3, 1/3, 1/3 }\n")
  164.         f.write("  },\n")
  165.         f.write("  node_box = {\n")
  166.         f.write("    type = 'fixed',\n")
  167.         f.write("    fixed = {\n")
  168.        
  169.         # write nodebox data
  170.         for x in xrange(minx, maxx + 1):
  171.             for z in xrange(minz, maxz + 1):
  172.                 for y in xrange(miny, maxy + 1):
  173.                     r, g, b, a = cur_sprite.getColor(x, y, z)
  174.                     if not r + g + b == 0:
  175.                         f.write("        {")
  176.                         f.write(str((x * stepnodeboxx) + minnodeboxx))
  177.                         f.write(", ")
  178.                         f.write(str((y * stepnodeboxy) + minnodeboxy))
  179.                         f.write(", ")
  180.                         f.write(str((z * stepnodeboxz) + minnodeboxz))
  181.                         f.write(", ")
  182.                         f.write(str(((x + 1) * stepnodeboxx) + minnodeboxx))
  183.                         f.write(", ")
  184.                         f.write(str(((y + 1) * stepnodeboxy) + minnodeboxy))
  185.                         f.write(", ")
  186.                         f.write(str(((z + 1) * stepnodeboxz) + minnodeboxz))
  187.                         f.write("},\n")
  188.  
  189.         # write footer
  190.         f.write("      },\n")
  191.         f.write("    },\n")
  192.         f.write("})\n")
  193.         f.close()
  194.        
  195.         # create textures
  196.  
  197.         # write toptexture
  198.         writeppm(topname, sizex, sizez, mttoptexture)
  199.  
  200.         # write bottomtexture
  201.         writeppm(bottomname, sizex, sizez, mtbottomtexture)
  202.  
  203.         # write fronttexture
  204.         writeppm(frontname, sizex, sizey, mtfronttexture)
  205.  
  206.         # write backtexture
  207.         writeppm(backname, sizex, sizey, mtbacktexture)
  208.  
  209.         # write lefttexture
  210.         writeppm(leftname, sizey, sizez, mtlefttexture)
  211.  
  212.         # write righttexture
  213.         writeppm(rightname, sizey, sizez, mtrighttexture)
  214.  
  215.         return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement