Skip to content

Instantly share code, notes, and snippets.

@format37
Forked from blairmacintyre/print.js
Last active April 1, 2021 21:18
Show Gist options
  • Save format37/0221c2d65bec34235b3d278b67800970 to your computer and use it in GitHub Desktop.
Save format37/0221c2d65bec34235b3d278b67800970 to your computer and use it in GitHub Desktop.
Worldedit Minecraft to openscad exporter for 3d printing
importPackage(Packages.com.sk89q.worldedit.blocks);
importPackage(Packages.com.sk89q.worldedit);
//joins up blocks in the dimension dim
function squash(blockarray, dim, sx, sy, sz, arraysize){
var i = new Array();
var dims = new Array();
for (var y = 0; y <= sy; y++){
for (var x = 0; x <= sx; x++){
for (var z = 0; z <= sz; z++){
var thisblock = blockarray[y][x][z];
if(thisblock != false){
i[0] = y;
i[1] = x;
i[2] = z;
for (var m = i[dim] + 1; m <= arraysize; m++){
i[dim] = m;
var nextblock = blockarray[i[0]][i[1]][i[2]];
if(nextblock == false){
break;
}
else{
dims[0] = thisblock[0];
dims[1] = thisblock[1];
dims[2] = thisblock[2];
dims[dim] = 1;
//if they are the same size
if(nextblock[0] == dims[0] && nextblock[1] == dims[1] && nextblock[2] == dims[2]){
thisblock[dim]++;
blockarray[i[0]][i[1]][i[2]] = false;
}
else{
break;
}
}
}
}
}
}
}
}
/*
printToStl = function name(area){
var session = context.remember();
var size = area[3];
var origin = new Vector(area[0], area[1], area[2]);
*/
printToStl = function name(session, origin, sx, sy, sz) {
var count = 0;
var finalcount = 0;
var email = "format37@gmail.com";
var blocks1 = new Array();
var blocklist1 = new Array();
var blocks2 = new Array();
var blocklist2 = new Array();
player.print("Starting print");
var block_fill=''
//copy blocks into a js array
for (var y = 0; y <= sy; y++){
blocks1[y] = new Array();
blocks2[y] = new Array();
for (var x = 0; x <= sx; x++){
blocks1[y][x] = new Array();
blocks2[y][x] = new Array();
for (var z = 0; z <= sz; z++) {
blocks1[y][x][z] = new Array();
blocks2[y][x][z] = new Array();
var pt = origin.add(x, y, z);
//var id = session.getBlockType(pt);
var id = session.getBlock(pt);
if (String(id)=='minecraft:air') {
blocks1[y][x][z] = false;
blocks2[y][x][z] = false;
block_fill=block_fill+'O';//+String(id);
//player.print(id);
}
else {
//player.print(String(id));
blocks1[y][x][z] = new Array();
blocks1[y][x][z][0] = 1;
blocks1[y][x][z][1] = 1;
blocks1[y][x][z][2] = 1;
blocks2[y][x][z] = false;
block_fill=block_fill+'*';//+String(id);
//player.print(id);
count++;
}
/*
if (id == BlockID.BRICK || id == BlockID.CLOTH || id == BlockID.COBBLESTONE
|| id == BlockID.MOSSY_COBBLESTONE || id == BlockID.REDSTONE_ORE
|| id == BlockID.REDSTONE_BLOCK
|| id == BlockID.DIAMOND_BLOCK || id == BlockID.DIAMOND_ORE
|| id == BlockID.EMERALD_BLOCK || id == BlockID.EMERALD_ORE
|| id == BlockID.GOLD_BLOCK || id == BlockID.GOLD_ORE
|| id == BlockID.COAL_ORE
|| id == BlockID.GLASS || id == BlockID.STONE || id == BlockID.TNT
|| id == BlockID.QUARTZ_BLOCK || id == BlockID.QUARTZ_ORE
|| id == BlockID.LAPIS_LAZULI_BLOCK || id == BlockID.LAPIS_LAZULI_ORE
|| id == BlockID.BEDROCK || id == BlockID.OBSIDIAN
|| id == BlockID.NETHER_BRICK
|| id == BlockID.IRON_BLOCK || id == BlockID.IRON_ORE || id == BlockID.STONE_BRICK
|| id == BlockID.LEAVES) {
blocks1[y][x][z] = new Array();
blocks1[y][x][z][0] = 1;
blocks1[y][x][z][1] = 1;
blocks1[y][x][z][2] = 1;
blocks2[y][x][z] = false;
count++;
} else if (id == BlockID.GRASS || id == BlockID.DIRT || id == BlockID.SAND
|| id == BlockID.SANDSTONE || id == BlockID.SLOW_SAND
|| id == BlockID.GRAVEL || id == BlockID.CLAY || id == BlockID.SOIL
|| id == BlockID.ICE || id == BlockID.SNOW || id == BlockID.SNOW_BLOCK
|| id == BlockID.LOG || id == BlockID.WOOD) {
blocks1[y][x][z] = false;
blocks2[y][x][z] = new Array();
blocks2[y][x][z][0] = 1;
blocks2[y][x][z][1] = 1;
blocks2[y][x][z][2] = 1;
count++;
} else {
blocks1[y][x][z] = false;
blocks2[y][x][z] = false;
}
if (id == 63) {
var block = session.getBlock(pt);
var t = block.getText();
t.map(function(e){email = email + String(e);})
}
*/
}
}
}
//player.print(block_fill);
//squash them
squash(blocks1, 0, sx, sy, sz, sy);
squash(blocks1, 1, sx, sy, sz, sx);
squash(blocks1, 2, sx, sy, sz, sz);
squash(blocks2, 0, sx, sy, sz, sy);
squash(blocks2, 1, sx, sy, sz, sx);
squash(blocks2, 2, sx, sy, sz, sz);
//write them into a list
for (var y = 0; y <= sy; y++){
for (var x = 0; x <= sx; x++){
for (var z = 0; z <= sz; z++){
var thisblock = blocks1[y][x][z];
if(thisblock){
//note swapping axes
blocklist1.push([-(x + thisblock[1]), z, y, thisblock[1], thisblock[2], thisblock[0]]);
finalcount++;
}
thisblock = blocks2[y][x][z];
if(thisblock){
//note swapping axes
blocklist2.push([-(x + thisblock[1]), z, y, thisblock[1], thisblock[2], thisblock[0]]);
finalcount++;
}
}
}
}
if(count == 0){
player.print("First build a model with sand or clay in the middle, There are both in the chest opposite");
}
else{
if(email == ""){
player.print("You have to put a sign with your email address in the print area with your model, There are signs in the chest");
}
else{
player.print(count + " blocks as " + finalcount + " primitives sent to OpenSCAD");
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
var src = "scale = 2.5;for(c = [" + blocklist1.map(function(b){return "[" + b + "]";}) + "]){translate(v = [(c[0] * scale) - 0.005, (c[1] * scale) - 0.005, (c[2] * scale) - 0.005]) cube(size = [scale * (c[3] + 0.01), scale * (c[4] + 0.01), scale * (c[5] + 0.01)], center = false);}"
// var path = "../scans/inbox/" + uuid + "_scan.scad"
var path = "models/file1" + uuid + "_scan.scad"
//var path = "file1" + uuid + "_scan.scad"
var out = new java.io.PrintWriter(new java.io.File(path), "UTF-8");
var text = new java.lang.String( src || "" );
out.print(text);
out.flush();
out.close();
src = "scale = 2.5;for(c = [" + blocklist2.map(function(b){return "[" + b + "]";}) + "]){translate(v = [(c[0] * scale) - 0.005, (c[1] * scale) - 0.005, (c[2] * scale) - 0.005]) cube(size = [scale * (c[3] + 0.01), scale * (c[4] + 0.01), scale * (c[5] + 0.01)], center = false);}"
// var path = "../scans/inbox/" + uuid + "_scan.scad"
path = "models/file2" + uuid + "_scan.scad"
//path = "file2" + uuid + "_scan.scad"
out = new java.io.PrintWriter(new java.io.File(path), "UTF-8");
text = new java.lang.String( src || "" );
out.print(text);
out.flush();
out.close();
//path = "../scans/inbox/" + uuid + "_email.txt"
path = "models/file" + uuid + "_email.txt"
//path = "file" + uuid + "_email.txt"
out = new java.io.PrintWriter(new java.io.File(path), "UTF-8");
text = new java.lang.String( email || "" );
out.print(text);
out.flush();
out.close();
player.print("Printing - Please be patient");
player.print("Your model will be emailed to " + email);
}
}
}
//context.checkArgs(1, -1, "<block> [width] [length]");
var sess = context.remember();
var sel = context.getSession().getSelection(sess.world);
var origin = sel.getMinimumPoint();
var sx = sel.getWidth() - 1;
var sy = sel.getHeight() - 1;
var sz = sel.getLength() - 1;
player.print("Building model starting at " + origin + ", of size (" + sx + "," + sy + "," + sz + ",");
printToStl(sess, origin, sx, sy, sz);
/*dual colors*/
importPackage(Packages.com.sk89q.worldedit.blocks);
importPackage(Packages.com.sk89q.worldedit);
//joins up blocks in the dimension dim
function squash(blockarray, dim, sx, sy, sz, arraysize){
var i = new Array();
var dims = new Array();
for (var y = 0; y <= sy; y++){
for (var x = 0; x <= sx; x++){
for (var z = 0; z <= sz; z++){
var thisblock = blockarray[y][x][z];
if(thisblock != false){
i[0] = y;
i[1] = x;
i[2] = z;
for (var m = i[dim] + 1; m <= arraysize; m++){
i[dim] = m;
var nextblock = blockarray[i[0]][i[1]][i[2]];
if(nextblock == false){
break;
}
else{
dims[0] = thisblock[0];
dims[1] = thisblock[1];
dims[2] = thisblock[2];
dims[dim] = 1;
//if they are the same size
if(nextblock[0] == dims[0] && nextblock[1] == dims[1] && nextblock[2] == dims[2]){
thisblock[dim]++;
blockarray[i[0]][i[1]][i[2]] = false;
}
else{
break;
}
}
}
}
}
}
}
}
printToStl = function name(session, origin, sx, sy, sz) {
var count = 0;
var finalcount = 0;
var email = "format37@gmail.com";
var blocks1 = new Array();
var blocklist1 = new Array();
var blocks2 = new Array();
var blocklist2 = new Array();
player.print("Starting print");
var block_fill=''
//copy blocks into a js array
for (var y = 0; y <= sy; y++){
blocks1[y] = new Array();
blocks2[y] = new Array();
for (var x = 0; x <= sx; x++){
blocks1[y][x] = new Array();
blocks2[y][x] = new Array();
for (var z = 0; z <= sz; z++) {
blocks1[y][x][z] = new Array();
blocks2[y][x][z] = new Array();
var pt = origin.add(x, y, z);
var id = session.getBlock(pt);
if (String(id)=='minecraft:cobblestone') {
blocks1[y][x][z] = new Array();
blocks1[y][x][z][0] = 1;
blocks1[y][x][z][1] = 1;
blocks1[y][x][z][2] = 1;
blocks2[y][x][z] = false;
block_fill=block_fill+'*';
count++;
}
else {
blocks1[y][x][z] = false;
blocks2[y][x][z] = false;
block_fill=block_fill+'O';
}
}
}
}
//squash them
squash(blocks1, 0, sx, sy, sz, sy);
squash(blocks1, 1, sx, sy, sz, sx);
squash(blocks1, 2, sx, sy, sz, sz);
squash(blocks2, 0, sx, sy, sz, sy);
squash(blocks2, 1, sx, sy, sz, sx);
squash(blocks2, 2, sx, sy, sz, sz);
//write them into a list
for (var y = 0; y <= sy; y++){
for (var x = 0; x <= sx; x++){
for (var z = 0; z <= sz; z++){
var thisblock = blocks1[y][x][z];
if(thisblock){
//note swapping axes
blocklist1.push([-(x + thisblock[1]), z, y, thisblock[1], thisblock[2], thisblock[0]]);
finalcount++;
}
thisblock = blocks2[y][x][z];
if(thisblock){
//note swapping axes
blocklist2.push([-(x + thisblock[1]), z, y, thisblock[1], thisblock[2], thisblock[0]]);
finalcount++;
}
}
}
}
if(count == 0){
player.print("First build a model with sand or clay in the middle, There are both in the chest opposite");
}
else{
if(email == ""){
player.print("You have to put a block.minecraft.oak_wall_sign with your email address in the print area with your model, There are signs in the chest");
}
else{
player.print(count + " blocks as " + finalcount + " primitives sent to OpenSCAD");
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
var src = "scale = 2.5;for(c = [" + blocklist1.map(function(b){return "[" + b + "]";}) + "]){translate(v = [(c[0] * scale) - 0.005, (c[1] * scale) - 0.005, (c[2] * scale) - 0.005]) cube(size = [scale * (c[3] + 0.01), scale * (c[4] + 0.01), scale * (c[5] + 0.01)], center = false);}"
// var path = "../scans/inbox/" + uuid + "_scan.scad"
//var path = "models/file1" + uuid + "_scan.scad"
var path = "/mnt/storage/share/alex/3d/models/scad/minecraft/file1"+uuid+"_scan.scad"
//var path = "file1" + uuid + "_scan.scad"
var out = new java.io.PrintWriter(new java.io.File(path), "UTF-8");
var text = new java.lang.String( src || "" );
out.print(text);
out.flush();
out.close();
src = "scale = 2.5;for(c = [" + blocklist2.map(function(b){return "[" + b + "]";}) + "]){translate(v = [(c[0] * scale) - 0.005, (c[1] * scale) - 0.005, (c[2] * scale) - 0.005]) cube(size = [scale * (c[3] + 0.01), scale * (c[4] + 0.01), scale * (c[5] + 0.01)], center = false);}"
// var path = "../scans/inbox/" + uuid + "_scan.scad"
//path = "models/file2" + uuid + "_scan.scad"
path = "/mnt/storage/share/alex/3d/models/scad/minecraft/file2"+uuid+"_scan.scad"
//path = "file2" + uuid + "_scan.scad"
out = new java.io.PrintWriter(new java.io.File(path), "UTF-8");
text = new java.lang.String( src || "" );
out.print(text);
out.flush();
out.close();
//path = "../scans/inbox/" + uuid + "_email.txt"
path = "/mnt/storage/share/alex/3d/models/scad/minecraft/file" + uuid + "_email.txt"
//path = "file" + uuid + "_email.txt"
out = new java.io.PrintWriter(new java.io.File(path), "UTF-8");
text = new java.lang.String( email || "" );
out.print(text);
out.flush();
out.close();
player.print("Printing - Please be patient");
player.print("Your model will be emailed to " + email);
}
}
}
var sess = context.remember();
var sel = context.getSession().getSelection(sess.world);
var origin = sel.getMinimumPoint();
var sx = sel.getWidth() - 1;
var sy = sel.getHeight() - 1;
var sz = sel.getLength() - 1;
player.print("Building model starting at " + origin + ", of size (" + sx + "," + sy + "," + sz + ",");
printToStl(sess, origin, sx, sy, sz);
/*dual color*/
importPackage(Packages.com.sk89q.worldedit.blocks);
importPackage(Packages.com.sk89q.worldedit);
//joins up blocks in the dimension dim
function squash(blockarray, dim, sx, sy, sz, arraysize){
var i = new Array();
var dims = new Array();
for (var y = 0; y <= sy; y++){
for (var x = 0; x <= sx; x++){
for (var z = 0; z <= sz; z++){
var thisblock = blockarray[y][x][z];
if(thisblock != false){
i[0] = y;
i[1] = x;
i[2] = z;
for (var m = i[dim] + 1; m <= arraysize; m++){
i[dim] = m;
var nextblock = blockarray[i[0]][i[1]][i[2]];
if(nextblock == false){
break;
}
else{
dims[0] = thisblock[0];
dims[1] = thisblock[1];
dims[2] = thisblock[2];
dims[dim] = 1;
//if they are the same size
if(nextblock[0] == dims[0] && nextblock[1] == dims[1] && nextblock[2] == dims[2]){
thisblock[dim]++;
blockarray[i[0]][i[1]][i[2]] = false;
}
else{
break;
}
}
}
}
}
}
}
}
printToStl = function name(session, origin, sx, sy, sz) {
var count = 0;
var finalcount = 0;
var email = "format37@gmail.com";
var blocks1 = new Array();
var blocklist1 = new Array();
var blocks2 = new Array();
var blocklist2 = new Array();
player.print("Starting print");
var block_fill=''
//copy blocks into a js array
for (var y = 0; y <= sy; y++){
blocks1[y] = new Array();
blocks2[y] = new Array();
for (var x = 0; x <= sx; x++){
blocks1[y][x] = new Array();
blocks2[y][x] = new Array();
for (var z = 0; z <= sz; z++) {
blocks1[y][x][z] = new Array();
blocks2[y][x][z] = new Array();
var pt = origin.add(x, y, z);
var id = session.getBlock(pt);
if (String(id)=='minecraft:dirt' || String(id)=='minecraft:grass_block') {
blocks1[y][x][z] = new Array();
blocks1[y][x][z][0] = 1;
blocks1[y][x][z][1] = 1;
blocks1[y][x][z][2] = 1;
blocks2[y][x][z] = false;
block_fill=block_fill+'*';
count++;
}
else {
blocks1[y][x][z] = false;
blocks2[y][x][z] = false;
block_fill=block_fill+'O';
}
}
}
}
//squash them
squash(blocks1, 0, sx, sy, sz, sy);
squash(blocks1, 1, sx, sy, sz, sx);
squash(blocks1, 2, sx, sy, sz, sz);
squash(blocks2, 0, sx, sy, sz, sy);
squash(blocks2, 1, sx, sy, sz, sx);
squash(blocks2, 2, sx, sy, sz, sz);
//write them into a list
for (var y = 0; y <= sy; y++){
for (var x = 0; x <= sx; x++){
for (var z = 0; z <= sz; z++){
var thisblock = blocks1[y][x][z];
if(thisblock){
//note swapping axes
blocklist1.push([-(x + thisblock[1]), z, y, thisblock[1], thisblock[2], thisblock[0]]);
finalcount++;
}
thisblock = blocks2[y][x][z];
if(thisblock){
//note swapping axes
blocklist2.push([-(x + thisblock[1]), z, y, thisblock[1], thisblock[2], thisblock[0]]);
finalcount++;
}
}
}
}
if(count == 0){
player.print("First build a model with sand or clay in the middle, There are both in the chest opposite");
}
else{
if(email == ""){
player.print("You have to put a block.minecraft.oak_wall_sign with your email address in the print area with your model, There are signs in the chest");
}
else{
player.print(count + " blocks as " + finalcount + " primitives sent to OpenSCAD");
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
var src = "scale = 2.5;for(c = [" + blocklist1.map(function(b){return "[" + b + "]";}) + "]){translate(v = [(c[0] * scale) - 0.005, (c[1] * scale) - 0.005, (c[2] * scale) - 0.005]) cube(size = [scale * (c[3] + 0.01), scale * (c[4] + 0.01), scale * (c[5] + 0.01)], center = false);}"
// var path = "../scans/inbox/" + uuid + "_scan.scad"
//var path = "models/file1" + uuid + "_scan.scad"
var path = "/mnt/storage/share/alex/3d/models/scad/minecraft/file1"+uuid+"_scan.scad"
//var path = "file1" + uuid + "_scan.scad"
var out = new java.io.PrintWriter(new java.io.File(path), "UTF-8");
var text = new java.lang.String( src || "" );
out.print(text);
out.flush();
out.close();
src = "scale = 2.5;for(c = [" + blocklist2.map(function(b){return "[" + b + "]";}) + "]){translate(v = [(c[0] * scale) - 0.005, (c[1] * scale) - 0.005, (c[2] * scale) - 0.005]) cube(size = [scale * (c[3] + 0.01), scale * (c[4] + 0.01), scale * (c[5] + 0.01)], center = false);}"
// var path = "../scans/inbox/" + uuid + "_scan.scad"
//path = "models/file2" + uuid + "_scan.scad"
path = "/mnt/storage/share/alex/3d/models/scad/minecraft/file2"+uuid+"_scan.scad"
//path = "file2" + uuid + "_scan.scad"
out = new java.io.PrintWriter(new java.io.File(path), "UTF-8");
text = new java.lang.String( src || "" );
out.print(text);
out.flush();
out.close();
//path = "../scans/inbox/" + uuid + "_email.txt"
path = "/mnt/storage/share/alex/3d/models/scad/minecraft/file" + uuid + "_email.txt"
//path = "file" + uuid + "_email.txt"
out = new java.io.PrintWriter(new java.io.File(path), "UTF-8");
text = new java.lang.String( email || "" );
out.print(text);
out.flush();
out.close();
player.print("Printing - Please be patient");
player.print("Your model will be emailed to " + email);
}
}
}
var sess = context.remember();
var sel = context.getSession().getSelection(sess.world);
var origin = sel.getMinimumPoint();
var sx = sel.getWidth() - 1;
var sy = sel.getHeight() - 1;
var sz = sel.getLength() - 1;
player.print("Building model starting at " + origin + ", of size (" + sx + "," + sy + "," + sz + ",");
printToStl(sess, origin, sx, sy, sz);
@format37
Copy link
Author

format37 commented Apr 1, 2021

  1. change /mnt/storage/share/alex/3d/models/scad/minecraft/file to yours
  2. put files to worldedit scripts path:
    minecraftbe/xandar/plugins/WorldEdit/craftscripts
  3. build ur model with cobblestone and dirt
  4. type /cs print_a
  5. type /cs print_b
  6. take your files from path

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment