forked from freethenation/OpenCLNoise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraw_to_mc_schematic.py
executable file
·83 lines (65 loc) · 2 KB
/
raw_to_mc_schematic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/python
from nbt import *
import numpy
from openclnoise.vec import vec # Our own local copy! :)
import sys,os
if len(sys.argv) < 3:
print "Usage: raw_to_mc_schematic.py <input.raw> <output.schematic>"
sys.exit(1)
inf = sys.argv[1]
if not os.path.exists(inf):
print "Can't find",inf
sys.exit(1)
size = os.stat(inf).st_size
print "Reading",inf
with open(inf) as inp:
dims = numpy.empty((3,),dtype=numpy.uint64)
dims.data = inp.read(24)
w,h,d = dims
elmsize = (size - 24) / (w*h*d)
datatype = vec.float4 if (elmsize == 4*4) else vec.uchar4
print "Found block {0} x {1} x {2}, size {3} bytes ({4}b / elm)".format(w,h,d,size,elmsize)
data = numpy.empty((dims[0],dims[1],dims[2]),dtype=datatype)
data.data = inp.read()
print data
comp = 0.50
if datatype == vec.float4:
print "Data is in float4s, comparing to {0}".format(comp)
else:
comp = 255 * comp
# dirtlower = 0.47
# dirtupper = 0.50
print "Data is in byte4s, comparing to {0}".format(comp)
#print data
w,h,d = dims
narr = numpy.empty(w*h*d,dtype=numpy.uint8)
for x in xrange(w):
for y in xrange(h):
for z in xrange(d):
mcd = y + z * h + x * d * h
point = (data[x,y,z])[0]
if point > comp:
narr[mcd] = 1
alpha = (data[x,y,z])[3]
if abs(alpha - 128) <= 8:
narr[mcd] = 3
else:
narr[mcd] = 0 # Insert air
s = TAG_Compound(name="Schematic")
s["Width"] = TAG_Short(w)
s["Length"] = TAG_Short(d)
s["Height"] = TAG_Short(h)
s["Materials"] = TAG_String("Alpha")
s["Blocks"] = TAG_Byte_Array()
s["Blocks"].value = narr
#s["Blocks"].value.shape = (w,d,h)
s["Data"] = TAG_Byte_Array()
s["Data"].value = numpy.zeros(w*d*h, dtype=numpy.uint8)
#s["Data"].value.shape = (w,d,h)
s["Entities"] = TAG_List()
s["TileEntities"] = TAG_List()
# Put wood at the bottom :)
#s["Blocks"].value[0, :, :] = 5
outf = sys.argv[2]
print "Writing",outf
s.save(outf)