Posted on Leave a comment

Minecraft Python Script to Create a Hollow Sphere

For a few years now I casually use a bit of lazy script to do boring things in Minecraft for me. Like, when the kids want to empty out an ocean or dig a big hole. One day I decided to build an enormous sphere just because.

You can read my other article on how to set this up first.

I’ll do this in sections so you know what’s going on. There are definitely many optimisations to make, so be kind. I’m perfectly aware that the distance can be calculated with sqrt(x^2 + y^2 + z^2). Obviously there’s heaps of mods that can do this sort of thing too. But this is more funner.

Can you make some minor changes to build an enormous dome over a city? Or to create an underwater Atlantis?

#!/usr/bin/python

# This script builds a sphere at the specified centre co-ordinates

import pprint
import os
import math

#diameter of sphere - radius is obviously half of this - always make it odd to keep things easy
n = 101

#centre coordinates
Start_X = 200
Start_Y = 130
Start_Z = 300

#sphere material
sphere_block = "stained_glass 3"
inside_block = "air 0"

##########
# START
##########

# the radius is how many blocks radiate from the centre.
# I choose to always make the sphere an odd diameter so that there is always a centre block and then an equal number of blocks radiating out
radius = (n-1) / 2

# when I put my co-ordinates in it is easier to specify where I want the centre to be
# therefore I fudge the co-ordinates a bit to offset this
Start_X = Start_X - radius
Start_Y = Start_Y - radius
Start_Z = Start_Z - radius

# create a massive in-memory 3d array that will contain the sphere
# fill it full of 0s to represent empty air initially
sphere = [[[0 for k in xrange(n)] for j in xrange(n)] for i in xrange(n)]
# iterate through all elements in the array and if the (rounded) distance from the centre to the element is less than or equal to the radius then it must be inside the sphere
# mark all the elements inside as a 1
for x in range(0, n):
for y in range(0, n):
for z in range(0, n):
xradius = abs(radius - x)
yradius = abs(radius - y)
zradius = abs(radius - z)
myradius = math.sqrt((xradius * xradius) + (yradius * yradius))
myradius = math.sqrt((myradius * myradius) + (zradius * zradius))
if round(myradius) <= radius:
# 0 is outside the sphere
sphere[x][y][z] = 1

# iterate through all the array elements and find ones that don’t touch any outside blocks. They are therefore inside blocks
# no need to iterate through the array elects on the “outside” of the cube as by definition they cannot be inside blocks - so that at 1 and loop through to n-1
# note that we only have to check 6 locations for each array element - each of the sides and above and below it
for x in range(1, n-1):
for y in range(1, n-1):
for z in range(1, n-1):
if (sphere[x+1][y][z] != 0) and (sphere[x-1][y][z] != 0) and (sphere[x][y+1][z] != 0) and (sphere[x][y-1][z] != 0) and (sphere[x][y][z+1] != 0) and (sphere[x][y][z-1] != 0):
# 2 is inside the sphere
sphere[x][y][z] = 2

# we are done, now just loop through and output all the elements that equal 1. Remember 0 is outside and 2 is inside.
# you could obviously output the elements equalling 2 as well if you want a solid sphere, or want to fill it with something cool like water or lava.
for x in range(0, n):
for y in range(0, n):
for z in range(0, n):
if sphere[x][y][z] == 1:
cmd = "screen -x minecraft -X stuff '/setblock " + str(Start_X+x) + " " + str(Start_Y+y) + " " + str(Start_Z+z) + " " + sphere_block + " replace\x0D'"
os.system(cmd)

 

Leave a Reply

Your email address will not be published.