Skip to content

Instantly share code, notes, and snippets.

@eiszfuchs
Created November 19, 2010 15:41
Show Gist options
  • Save eiszfuchs/706663 to your computer and use it in GitHub Desktop.
Save eiszfuchs/706663 to your computer and use it in GitHub Desktop.
converts chunks of minecraft into json, then imports into blender.
# this script will currently EAT YOU(r cpu forever)!
import json
import bpy
# materials
grass = bpy.data.materials.new('test_mat')
# TODO: more materials, textures!
grass.diffuse_color = (0,1,0)
grass.use_shadeless = False
f = open('./test.json', 'r')
chunk = json.loads(f.read());
print("--- start!")
east = 16 # x
north = 16 # y
upwards = 128 # z
lo_cut = 65
hi_cut = 0
total = east*north*(upwards-lo_cut-hi_cut)
i = 0
for z in range(lo_cut, upwards-hi_cut):
for y in range(north):
for x in range(east):
id = chunk["blocks"][x][y][z]
if id > 0:
# TODO: chunk offset!
bpy.ops.mesh.primitive_cube_add(location=(x/5, y/5, z/5))
obj = bpy.context.active_object
obj.scale = (0.1, 0.1, 0.1)
# TODO: assign materials here!
if not obj.material_slots:
bpy.ops.object.material_slot_add()
if id == 2:
obj.material_slots[0].material = grass
# TODO: group meshes into chunks?
i = i+1
progress = i // (total / 1000) / 10
print(str(progress) + "%")
print("--- done!")
package convert_json;
import java.io.*;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
// https://jnbt.svn.sourceforge.net/svnroot/jnbt/src
import org.jnbt.*;
// http://www.json.org/java/
import org.json.JSONException;
import org.json.JSONObject;
/**
*
* @author eiszfuchs
*/
public class Main {
public static class FileTraversal {
public final void traverse( final File f ) throws IOException {
if (f.isDirectory()) {
onDirectory(f);
final File[] childs = f.listFiles();
for( File child : childs ) {
traverse(child);
}
return;
}
onFile(f);
}
public void onDirectory( final File d ) {
}
public void onFile( final File f ) {
}
}
public static class Global {
// Global variables, e.g.
// public static int xmin = 0;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
new FileTraversal() {
@Override
public void onFile(final File f) {
try {
String filename = f.getName();
System.out.println(filename);
NBTInputStream ns;
ns = new NBTInputStream(new FileInputStream(f));
CompoundTag master = (CompoundTag) ns.readTag();
Map<String, Tag> items = master.getValue();
CompoundTag level = (CompoundTag) items.get("Level");
Map<String, Tag> levelitems = level.getValue();
int east = 16; // x
int north = 16; // y
int upwards = 128; // z
// Blocks
byte[] fblocks = ((ByteArrayTag) levelitems.get("Blocks")).getValue();
byte[][][] blocks = new byte[east][north][upwards];
for (int z = 0; z < upwards; z++) {
for (int y = 0; y < north; y++) {
for (int x = 0; x < east; x++) {
int blockID = z+(x*upwards+(y*upwards*east)); // god-like!
// THIS SUCKED:
// blocks[x][y][z] = fblocks[i];
blocks[x][y][z] = fblocks[blockID];
}
}
}
// Data
byte[] fdata = ((ByteArrayTag) levelitems.get("Data")).getValue();
byte[][][] datas = new byte[east][north][upwards];
String binary = "";
String temp = "";
for (byte s_data : fdata) {
temp = Integer.toBinaryString(s_data);
while(temp.length() < 8) {
temp = '0' + temp;
}
binary = binary + temp;
}
int i = 0;
for (int z = 0; z < upwards; z++) {
for (int y = 0; y < north; y++) {
for (int x = 0; x < east; x++) {
datas[x][y][z] = Byte.parseByte(binary.substring(i*4, (i*4)+3), 2);
i++;
}
}
}
// output
try {
JSONObject json_object = new JSONObject();
json_object.put("blocks", blocks);
json_object.put("datas", datas);
String json_output = json_object.toString();
BufferedWriter out = new BufferedWriter(new FileWriter("./test.json"));
out.write(json_output);
out.close();
} catch (JSONException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
ns.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
// }.traverse(new File("./world/"));
}.traverse(new File(args[0]));
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment