The best VPN 2023

The Best VPS 2023

The Best C# Book

Python Coding for Minecraft

Python scripts can generate neat in-world things, and there are many examples on the web. With a few lines you can draw a giant glass sphere, and with a bit more work make agiant Sierpinski triangle in the skyand evenimport obj files like a space shuttle. I myself made fun scripts to draw awater-filled glass donutand agigantic Klein bottle, toturn everything around into TNTand to control Minecraft with your brain using a MindFlex EEG toy. There is a wholebook introducing programming using python scripts for Minecraft, and you can even make simple Minecraft-based games. I will also show how to do simple (and sometimes more elaborate) turtle-based drawings in Minecraft, while you can ride along with the drawing as the turtle.

Using theleft(),right(),up()anddown()methods for the turtle is easy. But for some drawings we need more sophisticated 3D control. For instance, you might want to draw a donut tilted 45 degrees up. Its tempting to take the glass donut script from the previous step, and add before the loop:

I use the same technique, but with a more complicated formula, in mydonut.pyscript (yes, this is more complicated than the turtle-based method):

set horizontal angle to x (clockwise in degrees; 0 = South, 90 = West, 180 = North, 270 = East); resets roll angle

def tree(counter,branchLen): if counter == 0: return t.go(branchLen) for i in range(4): t.pitch(30) tree(counter-1,branchLen*0.75) t.pitch(-30) t.roll(90) t.back(branchLen)

Let me go through an example like that ( to draw a knot. Start with a standard header like:

t = 0 while t 2*pi: x = x0+int( scale * cos(2*t) * (3 + cos(5*t)) ) y = y0+int( scale * sin(2*t) * (3 + cos(5*t)) ) z = z0+int( scale * sin(5*t) ) if (x,y,z) not in done: mc.setBlock(x,y,z,block.GOLD_BLOCK) done.add((x,y,z)) t += 2*pi / 10000

set vertical angle to x (in degrees; upward is positive; 90 = vertically up; -90 = vertically down); resets roll angle

This Instructable shows how to install and use a mod I wrote that lets you control Minecraft with python scripts. Ill focus on Windows, though OS X and Linux should work just as well.

1. Download your choice of Python installed fromhere.

, but the living ones are best as others are unable to levitate; and the Ender Dragon cannot be controlled). The entity remains once the script is finished. To make it disappear, call turtle(None) or turtle(PLAYER).

Create a new world (its too easy to mess things up with the python scripts). My daughter suggested I use Creative and Superflat.

set pen position to (x,y,z); you can also access the position as

Run the Forge installer. Default settings should work.

A particularly simple way to draw in Minecraft is to use a turtle-based system. I made a very simple Turtle class (inspired by Martin OHanlonsMinecraftTurtle class) that I use to introduce middle- and high-schoolers to Minecraft python scripting.

Finally we need to tell the system what each of the symbols in the strings mean. For different L-systems, we will assign different meanings to them (rotations by different angles, for instance). So we need a second python dictionary specifying what is done for each symbol. If you dont specify an action for a symbol, the symbol is ignored when it is time for the turtle to draw the output (but it might be important for the generation). The meanings are given by a dictionary that specifies a function to call for each symbol. One-line functions can be specified with thelambdaoperator. In this case, all the functions are one-liners:

Instead, what we need to do is yaw/pitch/roll rotatins of the turtle. See the picture from NASA (US government works are public domain) explaining how the three angles go, and just imagine that the airplane is your turtle. (Unfortunately, because of Minecraft limitations, the roll angle doesnt affect how the turtle looks or the point of view from which youre viewing.) What we need to do to draw our sideways donut is first roll our turtle by 45 degrees witht.roll(45), and then when drawing the donut uset.yaw(-angle)instead oft.left(angle). The revised code is in the screenshot.

t.pitch(90) rules = L:[^FL][^FL][^FL] axiom = FL dictionary = F: lambda: t.go(10), ^: lambda: t.pitch(20), : lambda: t.roll(120), [: lambda: t.push(), ]: lambda: t.pop() lsystem.lsystem(axiom,rules,dictionary,5)

You move along with the turtle as it draws, which is fun, and the turtle starts off where the player is, pointing horizontally.

Actually, callinggridalign()can sometimes be a good idea even if not all of your angles are right angles. You will probably get some round-off problems in a large image, but it can still look better. See the spacefilling curve example (rendered in purple stained glass!).

This only draws data that isnt already drawn. Note that we need to round off the x, y and z coordi

I learned this technique from the samplent7s_sphere.pyscript. Start with the standard header and init Minecraft connection code:

3. Click through to the Customize Python dialog, and make sure to scroll down to Add python.exe to path, click on it and select Will be installed on local hard drive. If you dont add Python to the path, you wont be able to launch scripts with/pythonfrom within Minecraft.

Now we do our recursive tree function. The basic idea behind this is that a branch is just a smaller tree. So we make a function that takes a counter and a branch length. The counter specifies how many iterations the tree will go. For instance, counter=1 means just the trunk will be drawn, and counter=1 means the trunk and one set of branches will be drawn.

Finally, I just need to invoke this code. I will make a tree with a counter of 6 and an initial branch length of 20. And for good measure, Il make it out of wood, and of course vertically upward:

You need to decide if you want Python 2.7 or Python 3.x. TheAdventures in Minecraftbook uses 2.7, and most of the scripts floating around the web are for 2.7, but I have converted a lot of scripts for 3.x.

But that produces an unexpected result–see the picture. The reason for this is that the turtle will be climbing up all the time, while the d.left() method rotates it around the vertical axis, and so you get a spiral.

You can load the scripts into IDLE. A fun script to modify is my water-filled donut script ( For instance, changeWATERtoGRASSin the second last line to make a silly grass-filled donut. You can run this with/py donutfrom Minecraft, or just by pressingF5in IDLE.

the player doesnt follow the turtle

This rule has a 55% chance of replacing an A with a three-branch structure, and a 25% chance of replacing it with a two-branch structure. This isntveryrandom–more randomness would make things even more lifelike. I attach a screenshot of a fairly sparse forest randomly generated using this rule (and random placement of trees, subject to a minimum distance).

with a ranging from -1 to 1 and b ranging from 0 to 2*pi (yet another reason why this can be in the pi/e contest?). You can think of b as defining the angle around the circuit, and a moving one from one edge to the other.

Using scripts like this, you need to ensure that in your loops over a and b, the steps are sufficiently small that there are no gaps in the surface. Unless thats the effect youre after.

Whats nice about the yaw/pitch/roll rotations is that they are always relative to where the turtle is pointing, while left() and right() are relative to the Minecraft world.

One can also draw a surface by parametrizing it with two parameters, say a and b, and then looping over a range of these parameters, setting blocks where needed. In my Klein bottle and Mobius strip scripts, I have a general draw_surface() method that lets one do this. For instance, the Mobius strip (see mymobius.pyscript) is defined by the three formulae:

So I wrote theRaspberry Jam Modfor Minecraft 1.8 (now ported to 1.8.8, 1.8.9 and 1.9 as well) that emulates most of the Raspberry Pi Minecraft protocol (about the same as the Raspberry Juice plugin provides) and lets Raspberry Pi python scripts run with full desktop Minecraft. (I later found out that someone wrote themcpiapi modfor Minecraft 1.7.10 a couple of weeks earlier.) I wrote this Instructable initially for Python 2.7, but I think most of my samples will work for 3.x.

The coordinates forsetBlock()andgetPos()are measured from the players spawn point (which is thus (0,0,0)).

and then starting withmc = minecraft.Minecraft()or evenmc = minecraft.Minecraft.create().

Next we define the axiom, or starting point:

I wrote a very simplelsystem.pymodule. To implement the snowflake, start with boilerplate:

This is a simple example of an L-system. The idea behind an L-system is that we start with a string of characters (in this caseF++F++F), and then a bunch of rules how to change the characters (in this case, the one rule to replaceFwithF-F++F-F) in each iteration. We apply this a bunch of times and we get a rather complicated string. For instance, after two iterations in the snowflake case, we get:

L-systems dont have to be two-dimensional. You can include symbols that do yaw, pitch and roll rotations. For designing trees, a useful trick is to have stack commands: [ to save the current drawing state to a stack and ] to restore it. This will use the turtle modulespush()andpop()methods. For instance, heres a snippet of code for drawing a simple tree (

For instance, you can do a rectangle sloped at 45 degrees made of glass at level 0 at the spawn point with:

1. Create amodsfolder in your Minecraft folder. (To do that, pressWindows-R, type%appdata%\.minecraft, pressenter. You will see everything in your Minecraft folder. If you already have amodssubfolder, you dont need to do anything. Otherwise, make one. On Windows 8, click onNew folder, and then type in the namemodsand pressenter.)

Draw a triangle. Now take each line of the triangle and replace it by a line with a triangular bump on it. Repeat. Like on the animated image (public domain, based onWikipedia), you get aKoch snowflake.

This can be modeled by a super-simple turtle-graphics program. Imagine that F means draw forward, and + and – turn by 60 degrees, counterclockwise and clockwise, respectively. Then the initial triangle can be drawn by:

For fun, I adapted Simon Tathams really neatpolyhedron generation codeto use the Minecraft drawing class. His code starts with a number of random points on a sphere, and then simulates them repelling themselves until they stabilize into a configuration that is often regular. Then he has two neat methods of making a polyhedron out of these points, a face method and a vertex method. I put this This script takes commandline arguments. For instance, to draw an icosahedron use 12 points and the vertex construction. Directly from Minecraft:

I assume you have Minecraft installed.

You can create Python scripts for Minecraft with a text editor, the IDLE environment which comes with Python, or with Visual Studio Python Tools on Windows. The last is actually the nicest in some ways, so Ill have some optional steps on how to do that.

spawns a horse to use as a turtle (all the entities available are listed in

We first initialize and set the position of the knot relative to the player. Note that the player position need not be an integer (you might not be standing exactly aligned with a block) and should be turned into an integer.

You need to run Minecraft 1.8/1.8.8/1.8.9/1.9 once (your choice of version). To do that, start the Minecraft Launcher, and after logging in, click on New Profile. Set the profile name to anything you want (e.g., Test of 1.8) and then go to Use version and select Release 1.8.x/1.9. Then click on Save Profile, make sure the new profile is selected in the launcher, and click on Play. Start a world and make sure it works.

You can also play with python code for controlling the turtle right from the Minecraft chat window.

We now need to generate our knot. I used the cinquefoil formulas fromhere. This requires looping a parameter t from 0 to 2*pi, with small enough steps to ensure we dont have gaps. I used 10000 steps. Since this is done in-memory, and computers are fast, and overlapping blocks are only sent once to Minecraft, its easier to easier to do more steps than to think how many is enough. Its important for the coordinates that go in the dictionary to be integers so we can tell that the same block is being drawing (a block at (1.1,1.2,1.4) and a block at (1,1,1) are the same thing in Minecraft).

This is particularly nice for running python scripts interactively. In the screenshot, I am playing with Michael OHanlons 3D turtle drawing (and, yes, thats Deep Space Nine in the background).

Start up a Minecraft world in creative mode, and make your player fly (double-tap spacebar). Initialize the script with:

length = 3*3*3*3 mc.setBlocks(playerPos.x,playerPos.y,playerPos.z, playerPos.x+length-1,playerPos.y+length-1,playerPos.z+length-1,block.WOOL_PURPLE)

set stroke width to x (default is 1; wider strokes slow down drawing, and if follow mode is on, you may end up trapped inside the stroke, so at the end you should call

There is a lot of really good information in thisfree PDF book.

Finally, here is a much more advanced example. Lets draw a tree. This involves making a recursive function. Start with this to ensure that the drawing is done as fast as possible

: start and finish drawing a filled convex polygon

For instance you can draw a horizontal square with:

The easiest way to get started programming Minecraft in python is to start with one of the simpler sample scripts. I recommend making a shortcut on your desktop to the scripts folder (%appdata%\.minecraft\mcpipy).

(This initializes the Minecraft connection and brings math and block names into the namespace.) Then you have several convenience methods:

Then every python line you enter into chat will get executed by the python interpreter. To enter a line into chat, pressT, type the line and press[enter]. To exit turtleconsole, just type/py [enter]or enterquit() [enter]into chat.

For an dodecahedron, changevertextoface. You can also add one more argument for size. The polyhedron will be made of glass with stone edges and will be centered around the player.

In Minecraft, type/py donutand pressenter.

If all goes well, a giant glass donut will be drawn around you, and then it will be filled with water.

and then save, and restart Minecraft.

Think of theLas a leaf (though this simple code doesnt actually draw the leaf–that would need to be added to the dictionary). We start withFL, which is a trunk plus a leaf. Then we replace each leaf by[^FL][^FL][^FL]. This is a set of three branches, each tilted by 20 degrees from the trunk, 120 degrees apart. The brackets ensure that after each new^FLis drawn, were back where we were before it. This repeats, so that the leaves on the branches are replaced by triples of branches, and so on.

If you want to create a diamond block right under the player, you can also do:

Its sometimes useful to set a whole rectangular prism of blocks at one time: use mc.setBlocks(). For instance, this excerpt from mymengersponge.pyscript draws an 81×81 cube at the player position (the script then recursively deletes–i.e., replaces withAIR–subblocks to form the fractal image in the screenshot):

Step 2: Windows Installer [automatic Install]

If you get aCannot run program pythonerror, you dont have your python directory in your system path. You may want to add it manually to the path, or just reinstall python, following the directions in Step 3 of my python install step.

for x in range(-R-r,R+r): for y in range(-R-r,R+r): xy_dist = math.sqrt(x**2 + y**2) if (xy_dist 0): ringx = x / xy_dist * R nearest point on major ring ringy = y / xy_dist * R ring_dist_sq = (x-ringx)**2 + (y-ringy)**2 for z in range(-R-r,R+r): if (ring_dist_sq + z**2 = r**2): mc.setBlock(mcx+x, mcy+z, mcz+y, mcblock, mcmeta)

This moves your turtle to an integer grid location, and aligns its heading to a grid direction. After this, the turtle will remain exactly grid aligned provided that you move it only by integer amounts (e.g., 7, not 7.1, and not even 7. or 7.0 as these are floating point in Python), and you rotate only by integer amounts that are multiples of 90 degrees (e.g., -180, or 90, but not 90.0 or 45). The dragon curve code also gives an example of a forward function that is a bit more complicated–instead of just a line, it draws a wall with an opening in it.

You can then send a Hello world! message to the user with:

Now we make an empty set nameddoneto store the coordinates weve already drawn:

(For a list of all the block names other than DIAMOND_ORE, seemcpi\block.pyin the scripts folder. You can also directly use Minecraft block numbers.)

You can change materials withpenblock(). For instance, a golden vertical star:

This summer I plan to teach coding and basic 3D geometry to gifted middle- and high-schoolers using Minecraft, Raspberry Jam Mod, Python and Visual Studio.

You can find parametric equation for knots on the net. This time, were going to do things slightly different from before. Before, we had loops driving calls to mc.setBlock() directly. But in our surface plots, such as the Klein bottle, often the same block would get drawn multiple times, which is slow and inefficient. A better way is to keep track of the set of exact blocks that were already drawn to avoid redoing the same thing.

While the inequality technique works best for solid shapes, you can use it for hollow shapes in two ways. One way is to use two inequalities, for instance in the case of the sphere one to make sure that were within the outer radius of the center and another to make sure were not closer than the inner radius. The other way is just to draw another object with smaller dimensions made out of air inside the larger solid object, much as in mydonut.pyscript, I initially draw a glass donut, and then replace the inside of it with water.

The triangular bump line can be drawn by:

move x blocks backward (drawing if pen is down)

rotate up by angle (in degrees); beginners should not mix the left/right and up/down rotations or they may get unexpected results (technical note: the same as

If you have Windows, once you have Forge, you can install everything using aWindows installer. This will install RaspberryJamMod, a Python interpreter, IDLE for Python and sample scripts. If you dont have Windows, or want more control, look at the next two steps (but youll have to adapt them if you dont have Windows).

I also made a simple class for drawing with Cartesian coordinates. You can initialize with:

I assume that you have basic facility with creating folders and downloading, unzipping, and copying files on Windows (or your operating system of choice).

For details and examples,

There is also a console for the full non-Turtle includes a pre-initializedmclocal variable). (Just dont put in any postToChat() calls, or they will confuse the interpreter.)

There is one special trick for some L-systems. These L-systems are grid-aligned, with all the rotations being 90 degrees. The square curve ( and dragon curve ( are nice examples. The trick is to call, somewhere near the beginning of your code:

: rotate turtle via pitch/yaw/roll angles (see next step of Instructable)

Start Minecraft, making sure that you use theForgeprofile.

If you want to do this with Minecraft Pocket Edition on Android instead, I have anInstructablefor that, too.

move x blocks forward (drawing if pen is down)

After four, we get the image above. (Toreallyget a fractal, the lines should shrink with each iteration, but that wont work in Minecraft.)

This draws a sphere of the specified radius above the player, and a little offset in the z-direction.

The context includes a local variabletinitialized to a Turtle() object, so you dont need to run any initialization code, just things like:

You can also do grid-aligned 3D things. For instance,hilbert.pyhas a 3D Hilbert curve.

There are basically two different techniques for drawing mathematically defined objects with a python script in Minecraft.

Another useful feature of the turtle drawing class is being able to draw not just lines but polygonal surfaces. To do this, just dot.startface(), draw your surface, and then dot.endface(). Once youre in face mode, each time you draw a line, the code actually draws a triangle: one vertex is the point the turtle was whent.startface()was called and the line is the opposite edge. This draws a pentagon tilted on its side by 45 degrees:

(I.e., the distance to (x0,y0,z0) is at most r.) So to draw a sphere, just loop through all points (x,y,z) in a cube of side-length 2*r+1 surrounding (x0,y0,z0), and draw a block if the above inequality holds.

Technical note: Starting withfrom mine import *imports a lot of stuff (Minecraft, block definitions, entity definitions, math) into our namespace. This is handy for focusing on the essentials for beginners (remember that I am going to be using this to teach coding to beginners). More advanced users may prefer being more pythonic by keeping the namespaces separate and putting in the header:

The mcpiapi mod, unlike my Raspberry Jam Mod, doesnt support setting the players direction. This means that the player wont face the same direction as the turtle. (And there is some chance of other problems with turtle scripts.)

Or to make a simple new script, create ahelloworld.pyfile with your favorite text editor (even Notepad) in the scripts directory. Put at the top:

Running a new script with/pystops any earlier scripts running (if you dont like that, use/apyinstead of/py). You can stop a script thats running by just typing/pywithout any arguments.

4. Open the downloaded zip file (in Chrome, by clicking on it at the bottom of the window). It has amcpipyfolder. Copy themcpipyfolder into your Minecraft folder. (To do that, click once on themcpipyfolder in the zip file, and pressctrl-c, then navigate to the%appdata%\.minecraftfolder as in step 1, and pressctrl-v).

Finally, you might want to introduce some randomization into the L-system rules. So far our rules were deterministic: a single string was given that replaces a symbol, e.g.,F: F-F++F-F. But instead of a simple replacement string, one can give a Python list of pairs(p,string), wherepis a probability (from 0 to 1) andstringis the string to use with that probability. The probabilities for a given source symbol had better not add up to more than 1, but they can add up to less than one–in that case, there is a chance that there will be no replacement. For instance, heres a slightly randomized version of geeky.bloggers tree:

A more realistic tree could have more complex code for[. It could make the succeeding branches shorter and thinner, and change their material as we get closer to the leaves (and then restore on]). I include such a tree as the demo code, based on rules (with some tweaks) fromthe Geeky Blogger.

I.e., go forward, turn right by 120 degrees, then go forward, turn right by 120 degrees, go forward.

One way is to define a solid object by an inequality. For instance, a sphere centered on (x0,y0,z0) with radius r can be defined by the inequality:

Minecraft normally pauses the game and goes to the Game menu when you alt-tab out of it or otherwise lose focus. This makes it harder to see what your python scripts are doing if youre launching them from outside of Minecraft. To do that, you can edit the options.txt file in your Minecraft directory (%appdata%\.minecrafton Windows), change the

Exit Minecraft and Minecraft Launcher.

set compass, vertical and roll angles; omitted arguments are taken to be zero

Technical note:If you already have a Minecraft objectmc, initialize withd = Drawing(mc). In either case, you can access the Minecraft object

And you can change the pen width withpenwidth(x). For instance, this draws a very simple glass donut (actually, a thickened 90-gon, but who can tell the difference?):

seven times. Or just once in a loop:

This imports the needed library code. It also conveniently imports all the functions from the math library as well as names for all the blocks. Connect to Minecraft with:

In Minecraft, run theturtleconsole.pyscript by typing:/py turtleconsole [enter].

This fractal tree doesnt look very realistic. We should make the trunk thick and the branches get thinner and thinner as they go outward (say, get half as thick, until they reach a single block), and we should switch fromWOODtoLEAVESblocks as we go out. But most of all, real trees arent that regular. We need some randomness. The simplest form of randomness is just to give each branch a chance of failing to grow, say a 20% chance. I implemented all of these in script. The resulting tree looks surprisingly realistic for something produced by such a relatively simple piece of code. A funny side-effect of how I implemented it is that there is a 20% chance that nothing will be drawn–i.e., the tree has a chance of failing to grow, which is also realistic. And each time you run the script, you get something different.

To run your script, save it and type/py helloworldin your Minecraft world and pressenter.

(I wrapped all the Minecraft connection code inside the Turtle class and it also brings math into the namespace for convenience.) You can now do things liket.go(15)to draw a line while moving 15 blocks forward,t.right(45)to rotate 45 degrees to the right,t.down(30)to rotate 30 degrees down, and thus draw simple pictures.

rules = A: [(0.55,^f[^^fA][^^fA][^^fA]), (0.25,^f[^^fA][^^fA])]

Now we need to define the rules. I define the rules by a python dictionary:

rotate down by angle (in degrees) (technical note: the same as

First, the code checks if our counter has run down to zero. If so, we dont have anything to draw. Then we draw a trunk. Then we draw four branches sticking out of it with a simple loop. To design this bit of code, I just imagine myself as the turtle, having just moved up along the trunk. To draw a branch, I tilt my self up by 30 degrees (i.e.,t.pitch(30)) and draw a new tree of smaller size: its counter is less by one, and its trunk will be 3/4 of the length of my current trunk. I think tilt myself back down by 30 degrees. I rotate myself by 90 degrees and repeat the exercise. Finally, once Im done with the branches, I go back down the trunk.

each movement or rotation of the turtle is delayed by x seconds; default is 0.05; set to zero for much faster drawing (the player will still move with each line drawn, but instantaneously from beginning to end)

So heres how we can generate a snowflake. We take the initial turtle programF++F++Fto draw a triangle. EachFin it represents a line. So replace eachFbyF-F++F-F. If we keep on going, we generate the Koch snowflake.

set material for drawing to block x; for a list of the predefined blocks, see

DownloadForge installerfor your version.

radius = 8 playerPos = mc.player.getPos() for x in range(radius*-1,radius): for y in range(radius*-1, radius): for z in range(radius*-1,radius): if x**2 + y**2 + z**2 radius**2: mc.setBlock(playerPos.x + x, playerPos.y + y + radius, playerPos.z – z – 10, block.GLASS)

Start Minecraft. You will now have a new Forge profile.

if x is PLAYER, this is the same as follow(), and if x is None, this is the same as nofollow(). If you are using Raspberry Jam Mod you can also use other entities than yourself as the turtle. For instance,

For a while now you couldwrite python scripts for Minecraft on the Raspberry Pi. I wanted my kids to be able to do that, but we dont have a Pi, plus it would be nice to do this with the full desktop Minecraft. You could run your own server with theRaspberry Juice pluginwhich enables most of the python scripts to work. But not everyone wants to install and configure a server.

: save and restore drawing state (heading, material, face status, thickness; but not speed or turtle type); useful forL-systems

Technical note:If you already have a Minecraft objectmc, initialize witht = Turtle(mc). If you omit the Minecraft object when initializing the turtle, its created for you, and in either case, you can access the Minecraft object

If you get something like aScript not founderror, this probably means that you dont have the sample scripts installed in the%appdata%\.minecraft\mcpipyfolder.

: align the current position and turtle direction to the grid (the best approximation to the turtles current direction is chosen)

The Forge manages Minecraft mods, and is needed for the Raspberry Jam Mod.

3. Download the latest zip file containing sample Python scripts and the mcpi library from myRaspberry Jam Mod github release. (The scripts are supposed to work on both Python 2.7 and 3.x.)

Finally we invoke the L-system, specifying how many iterations (in this case 4):

2. Download themods.zipfile from the latest version of the Raspberry Jam Mod. Put the contents of the zip file (currently consisting of a number of folders with names like1.8and1.10.2) inside themodsfolder you had just made.

Leave a Comment