Jump to content

  • Curse Sites
Become a Premium Member! Help
Latest News Article

Content generation with Python


  • Please log in to reply
17 replies to this topic

#1

gommle
    gommle

    Tree Puncher

  • Members
  • 23 posts

Posted 09 October 2010 - 01:01 AM

I've made a little script in Python for making mathematical shapes.

Basically it helps you use a function block_type(x,y,z) which outputs block type from coordinates. The script then saves this to a MCEdit schematics-file which can be inserted into a level with MCEdit.

The core is pymclevel by codewarrior. I've only added a little bit on top of this.

Here's the script (save as mineplot.py):
"""
-- string name, function myfunc, tuple ranges ---

Work in progress: 3D function plotter for Minecraft
Made by Gommle, inspired by Voxelizer.

Contact: [email protected]

Dependencies:
pymclevel by codewarrior ( DL: http://github.com/codewarrior0/pymclevel )
numpy (DL: http://www.scipy.org/Download )

Major credits to codewarrior for making this possible.
"""

from pymclevel import *

"""
See
http://www.minecraftwiki.net/wiki/Data_values
""" 

def plot(name, myfunc, ranges):
		# Useful stuff
		(xr, yr, zr) = ranges
		w = xr[1]-xr[0] + 1 # width
		h = zr[1]-zr[0] + 1 # height
		l = yr[1]-yr[0] + 1 # lengt


		size = (w, h, l); #NB: USES X, Z, Y
		schematic = MCSchematic(shape=size, mats='Alpha');
		types = {}
		blocks = 0
		for x in range(xr[0], xr[1]+1):
				for y in range(yr[0], yr[1]+1):
						for z in range(zr[0], zr[1]+1):
								block_type = myfunc(x, y, z) #
								if block_type != 0:
										blocks += 1
										schematic.setBlockAt(x - xr[0], z - zr[0], y - yr[0], block_type) #NB: USES X, Z, Y
								
								
								

		filename = name + '.schematic'
		schematic.saveToFile(filename)
		print "Wrote " + str(blocks) + " blocks to " + filename

Put the folder pymclevel in the same folder as mineplot.py.

Here is an example of an advanced function, which makes a HUGE decorated torus: (save as mp_something.py in the same folder as mineplot.py)
import mineplot
from math import sqrt, pow, fabs, sin

# Variables
name = "mp_toroid_space_station" #Without file ending
R = 20 # Main radius
r = 20 # Tube radius


def myfunc(x,y,z):
		btype = 0 # air is default block
		
		# Distance from center of torus. Remove z for horizontal distance.
		dist = sqrt(x**2 + y**2 + z**2)
		
		if abs((x**2 + y**2 + z**2 + R**2 - r**2)**2 - 4*R**2*(x**2 + y**2)) < 200000:
				
				# Z-options
				if z == -4:
						btype = 45 # brick
				elif z == -17: # Bottom floor
						btype = 2 # grass
				elif z > -4: # Upper part
						btype = 20 # glass
				elif z < -14: # Two levels above and including bottom floor
						btype = 49 # obsidian
				else: # Bottom part
						btype = 45 # brick

				# Distance options
				# Make a hollow sphere in the middle section
				if dist < 10:
						btype = 0 # air
		else:
				
				if z == 0: # Make a balcony in the middle of the torus
						if dist < 0.5*(R+r) or (dist > 0.7*(R+r) and dist < R+r):
						# From both sides, but not all the way over
								if round(dist) % 4 == 0: # Every other row is gold
										btype = 41 # gold
								else:
										btype = 20 # glass
				else:
						btype = 0
		return btype
	
# Coordinate ranges
xr = [-(R+r), R+r] # Main radius + tube radius
yr = xr
zr = [-r, r] # Tube radius
ranges = [xr, yr, zr]

mineplot.plot(name, myfunc, ranges)

Oh, and a little heads up for something that is based on this:
array2minecraft

Register or log in to remove.

#2

gommle
    gommle

    Tree Puncher

  • Members
  • 23 posts

Posted 09 October 2010 - 01:02 AM

Screenshots: Posted Image
Posted Image

Here is a simpler function, which makes a toroid:
import mineplot
from math import sqrt, pow, fabs, sin

# Variables
name = "mp_simple_solid_torus" #Without file ending

R = 10 # Main radius
r = 5 # Tube radius

def myfunc(x,y,z):
		btype = 0 # air is default block
		if (x**2 + y**2 + z**2 + R**2 - r**2)**2 - 4*R**2*(x**2 + y**2) < 200:
				btype = 1 # rock or something
		return btype
			
# Coordinate ranges
xr = [-(R+r), R+r] # Main radius + tube radius
yr = xr
zr = [-r, r] # Tube radius
ranges = [xr, yr, zr]

mineplot.plot(name, myfunc, ranges)


And simpler still:
import mineplot
from math import sqrt, pow, fabs, sin

# Variables
name = "mp_simple_ball" #Without file ending

r = 5 # Tube radius

def myfunc(x,y,z):
		btype = 0 # air is default block
		dist = sqrt(x**2 + y**2 + z**2) # Distance from center
		
		if dist < r:
				btype = 1 # rock or something
		return btype
			
# Coordinate ranges
xr = yr = zr = [-r, r]
ranges = [xr, yr, zr]

mineplot.plot(name, myfunc, ranges)


A huge ball:
Posted Image

Another torus, a long with a sphere where the bottom half is filled with ground.
Posted Image

I hope you guys enjoy this  :Diamond:

#3

Intradox
  • Location: USA
  • Minecraft: Intradox

Posted 09 October 2010 - 04:24 AM

amazing imo. Really good job!  And +1 for python :D
Posted Image

#4

Canes_The_U

Posted 09 October 2010 - 04:29 AM

Im speechless. Literally.

Posted Image


#5

MrTorus
    MrTorus

    Obsidian Miner

  • Members
  • 1486 posts
  • Location: Brazil :(

Posted 09 October 2010 - 05:36 AM

Oh well. Now I feel a little bad from building my giant torus by hand. So much wasted time. :iapprove:

You should make it use UV-mapped textures too, based on functions. This is what my voxelizer script (also in Python) does:

Posted Image
Posted Image

But it just generates the blueprints of each layer and the previews.
Please, read this thread:
MINECRAFT FACTS: BIG LIST OF WHAT NOTCH HAS ACTUALLY SAID ABOUT THE PLANNED FEATURES OF MINECRAFT

"If something ever doesn't feel fun, I'll remove it." - Notch


#6

codewarrior
  • Location: Big Island

Posted 09 October 2010 - 11:03 AM

I like what you've done! Here, let me show you something vaguely familiar...

Posted Image

This was fun to make. Because I wrote the program entirely in interactive mode, I lost the source. Let me see if I can do it again...
MCEdit: Minecraft World Editor

"We will absolutely not keep in mind what external mapeditors will have to do to read data from the disk, that makes no sense whatsoever." - Grum

#7

gommle
    gommle

    Tree Puncher

  • Members
  • 23 posts

Posted 09 October 2010 - 10:17 PM

Any requests for schematics?

#8

blaster
    blaster

    Creeper Destroyer

  • Members
  • 6310 posts
  • Location: The land of four dimentions

Posted 09 October 2010 - 10:51 PM

codewarrior said:

I like what you've done! Here, let me show you something vaguely familiar...

Posted Image

This was fun to make. Because I wrote the program entirely in interactive mode, I lost the source. Let me see if I can do it again...
Is that Rule 110 I see there, you should make one for Rule 30 too!
The rebellion has already begun. Otter Forums

#9

kilagain
    kilagain

    Diamond Miner

  • Members
  • 809 posts
  • Location: New Zealand!

Posted 10 October 2010 - 12:59 AM

Dudecon made some awesome python scripts back in the days of indev, like castle generators and other awesomeness, including the big tree's we see today in Alpha. Maybe you can contact him to allow his scripts for Alpha use...
HλLF A LIFE
(My works: Art, Music, MINECRAFT and other stuff)

#10

gommle
    gommle

    Tree Puncher

  • Members
  • 23 posts

Posted 10 October 2010 - 02:19 PM

MrTorus said:

Posted Image

What's the equation/equations for that knot?

#11

MrTorus
    MrTorus

    Obsidian Miner

  • Members
  • 1486 posts
  • Location: Brazil :(

Posted 10 October 2010 - 04:21 PM

gommle said:

MrTorus said:

Posted Image

What's the equation/equations for that knot?

It's in cylindrical coordinates:

p = R+G*cos(b*u)+r*cos(v)
z = G*sin(b*u)+r*sin(v)
q = a*u
x = p*cos(q)
y = p*sin(q)

Then you use x, y, and z.

It's a torus knot, so it's a tube of radius G around a curve in the surface of an invisible torus defined by:
R = radius of the ring
r = radius of the torus' tube

Since it's a trefoil knot, you have: a = 2, b = 3.

Changing these will let you get any other torus knot.
Please, read this thread:
MINECRAFT FACTS: BIG LIST OF WHAT NOTCH HAS ACTUALLY SAID ABOUT THE PLANNED FEATURES OF MINECRAFT

"If something ever doesn't feel fun, I'll remove it." - Notch


#12

kdfb
    kdfb

    Out of the Water

  • Members
  • 8 posts

Posted 18 November 2010 - 04:09 PM

MrTorus, I don't suppose I could bother you for the MCEdit schematics for that trefoil knot?

I've been fiddling with Python for the better part of an hour now, and can't seem to make your scripts work.

It's a combination I'm sure of Python 2.6  vs Python 3.1, x64 architecture vs x86, etc.

If you could just run the script and upload the MCEdit file somewhere it would be much appreciated.
I really want to put it underground and make it a sort of maze.

Many thanks!

#13

gommle
    gommle

    Tree Puncher

  • Members
  • 23 posts

Posted 19 November 2010 - 02:11 PM

Here's a trefoil knot: viewtopic.php?f=25&t=53892&p=865753#p1335618

It has air inside and around tho. Use the fill tool after placing it underground to remove air.

#14

TonyRockyHorror

Posted 19 November 2010 - 03:40 PM

gommle said:

A huge moon:
Posted Image

That's no moon...

#15

Pruitt
    Pruitt

    Carpenter

  • Members
  • 47 posts

Posted 19 November 2010 - 04:25 PM

gommle said:

Any requests for schematics?

Sure! heh heh... :P

#16

kdfb
    kdfb

    Out of the Water

  • Members
  • 8 posts

Posted 21 November 2010 - 12:44 AM

gommle said:

Here's a trefoil knot: viewtopic.php?f=25&t=53892&p=865753#p1335618

It has air inside and around tho. Use the fill tool after placing it underground to remove air.

Perfect!

Thanks so much gommle.

#17

kdfb
    kdfb

    Out of the Water

  • Members
  • 8 posts

Posted 21 November 2010 - 02:06 AM

Actually I think I jumped in too soon.

While that does work admirably, it's a little on the big side?

I hate doing this, but again, I've been futzing with Python installs for the longest time.

I've installed every combination of Python 2.6, Python 3.1, Windows x32, and Windowsx64, and I'm still getting version errors.

At least I got past the numpy step... but still.

If any kind soul could link me to an MCEdit version of that trefoil knot that will fit inside my mountain (32x32), I would be eternally obliged.

#18

gommle
    gommle

    Tree Puncher

  • Members
  • 23 posts

Posted 21 November 2010 - 03:37 PM

http://dl.dropbox.co... ... .schematic

There you go.