<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Modprobe &#187; python</title>
	<atom:link href="http://modprobe.complex.ch/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://modprobe.complex.ch</link>
	<description>Funky laboratories</description>
	<lastBuildDate>Thu, 26 Aug 2010 13:22:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Sierpinsky triangle python script for blender</title>
		<link>http://modprobe.complex.ch/2010/02/28/sierpinsky-triangle-python-script-for-blender/</link>
		<comments>http://modprobe.complex.ch/2010/02/28/sierpinsky-triangle-python-script-for-blender/#comments</comments>
		<pubDate>Sun, 28 Feb 2010 18:07:17 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[blender]]></category>
		<category><![CDATA[makerbot]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[sierpinsky]]></category>

		<guid isPermaLink="false">http://modprobe.complex.ch/?p=690</guid>
		<description><![CDATA[After printing a sierpinsky triangle on Theo&#8217;s makerbot, I thought it might interest some of you to see how I generated the geometry in blender. However, before starting I should warn you that my knowledge of Blender is virtually non-existent. The blender specific code used here to generate faces is probably not ideal and you&#8217;re [...]]]></description>
			<content:encoded><![CDATA[<p>After printing a sierpinsky triangle on Theo&#8217;s makerbot, I thought it might interest some of you to see how I generated the geometry in blender.</p>
<p>However, before starting I should warn you that my knowledge of Blender is virtually non-existent. The blender specific code used here to generate faces is probably not ideal and you&#8217;re very welcome to improve that part.</p>
<p>So, what do we want to do? Make triangles, lots of them. We probably need a function to generate triangles. Let&#8217;s stay simple and assume that a triangle is composed of 3 points. We are in a 3d space so each point will have 3 components. We shall refer to these points as Vertices. There are many ways to represent this type of data in Python. For something different, let&#8217;s try namedtuples. They provide an elegant extension to the base tuple type. They work as follows:</p>
<pre name="code" class="python">
from collections import namedtuple

Vertex = namedtuple('Vertex', 'x, y, z')  # define vertex 'type'

v1 = Vertex(0.0, 0.0, 0.0) # define a Vertex tuple, with positional arguments

v2 = Vertex(x=0.0, y=0.0, z=0.0) #define a Vertex with named arguments

x, y, z = v2    # unpack as regular tupple

x = v.x   # accessible with named parameter
</pre>
<p>We can now define our triangle function</p>
<pre name="code" class="python">

#import blender bindings
import Blender
from Blender import NMesh
from Blender.BGL import *
from Blender.Draw import *

def triangle(a, b, c):
    """generate triangle geometry
       we expect a b c to be namedtuples of type Vertex"""

    ######### Creates a new mesh
    poly = NMesh.GetRaw()

    ### fill vertices
    v = NMesh.Vert(a.x, a.y, a.z)
    poly.verts.append(v)

    v = NMesh.Vert(b.x, b.y, b.z)
    poly.verts.append(v)

    v = NMesh.Vert(c.x, c.y, c.z)
    poly.verts.append(v)

    ## create a face
    f = NMesh.Face()
    f.v.append(poly.verts[0])
    f.v.append(poly.verts[1])
    f.v.append(poly.verts[2])
    poly.faces.append(f)

     ######### Creates a new Object with the new Mesh
    polyObj = NMesh.PutRaw(poly)

    Blender.Redraw()
</pre>
<p>My poor knowledge of blender doesn&#8217;t allow me to say much about this code. We basically use the api to generate a three point polygon.  As said, please improve or correct this part.<br />
Time for a first test! </p>
<pre name="code" class="python">
#define the three vertices of a triangle
a = Vertex(0.0, 0.0, 0.0)
b = Vertex(25.0, 50.0, 0.0)
c = Vertex(50.0, 0.0, 0.0)

triangle(a, b, c)
</pre>
<p>We define three vertices for our test triangle and call the triangle function. The next step is to load the code in Blender&#8217;s text editor and to press Alt-p to run the code.</p>
<div id="attachment_699" class="wp-caption alignleft" style="width: 495px"><a href="http://modprobe.complex.ch/wp-content/uploads/2010/02/Screenshot.png"><img src="http://modprobe.complex.ch/wp-content/uploads/2010/02/Screenshot.png" alt="" title="single triangle in blender" width="485" height="364" class="size-full wp-image-699" /></a><p class="wp-caption-text">(sorry for the crappy window in the middle)</p></div>
<p>Ok, so let&#8217;s get to the meat of the problem, i.e. the recursive subdivision. I use the basic algorithm from <a href="http://www.amazon.com/Interactive-Computer-Graphics-Top-Down-Approach/dp/0321535863/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1267378668&#038;sr=8-1-spell">here</a>, and adapted it to python:</p>
<pre name="code" class="python">
def divideTriangle(a, b, c, step):
    """ recursive divide until step == 0"""

    if step > 0:
        #compute midpoints of sides

        midpointof = lambda v1, v2: Vertex(x = (v1.x + v2.x) * 0.5,
                                           y = (v1.y + v2.y) * 0.5,
                                           z = (v1.z + v2.z) * 0.5)

        ab = midpointof(a, b)
        ac = midpointof(a, c)
        bc = midpointof(b, c)

        # divide all but center triangle

        divideTriangle(a, ab, ac, step - 1)
        divideTriangle(c, ac, bc, step - 1)
        divideTriangle(b, bc, ab, step - 1)
    else:
        #stop recursion and generate geometry
        triangle(a, b, c)
</pre>
<p>I tried to be as expressive as possible, but I have the feeling that there is something fishy in the midpoint lambda. I guess it could be simplified. But it will do for now. Let&#8217;s put it all together, and see what we get in blender.</p>
<pre name="code" class="python">
from collections import namedtuple

import Blender
from Blender import NMesh
from Blender.BGL import *
from Blender.Draw import *

Vertex = namedtuple('Vertex', 'x, y, z')  

def triangle(a, b, c):
    """a b c are of type Vertex"""

    ######### Creates a new mesh
    poly = NMesh.GetRaw()

    ### fill vertices

    v = NMesh.Vert(a.x, a.y, a.z)
    poly.verts.append(v)

    v = NMesh.Vert(b.x, b.y, b.z)
    poly.verts.append(v)

    v = NMesh.Vert(c.x, c.y, c.z)
    poly.verts.append(v)

    ## create a face
    f = NMesh.Face()
    f.v.append(poly.verts[0])
    f.v.append(poly.verts[1])
    f.v.append(poly.verts[2])
    poly.faces.append(f)

     ######### Creates a new Object with the new Mesh
    polyObj = NMesh.PutRaw(poly)

    Blender.Redraw()

def divideTriangle(a, b, c, step):
    """ recursive divide until step == 0"""

    if step > 0:
        #compute midpoints of sides

        midpointof = lambda v1, v2: Vertex(x = (v1.x + v2.x) * 0.5,
                                           y = (v1.y + v2.y) * 0.5,
                                           z = (v1.z + v2.z) * 0.5)

        ab = midpointof(a, b)
        ac = midpointof(a, c)
        bc = midpointof(b, c)

        # divide all but center triangle

        divideTriangle(a, ab, ac, step - 1)
        divideTriangle(c, ac, bc, step - 1)
        divideTriangle(b, bc, ab, step - 1)
    else:
        triangle(a, b, c)

#main

a = Vertex(0.0, 0.0, 0.0)
b = Vertex(25.0, 50.0, 0.0)
c = Vertex(50.0, 0.0, 0.0)

divideTriangle(a, b, c, 5)
</pre>
<p>and in blender:<br />
<div id="attachment_700" class="wp-caption alignleft" style="width: 495px"><a href="http://modprobe.complex.ch/wp-content/uploads/2010/02/Screenshot-2.png"><img src="http://modprobe.complex.ch/wp-content/uploads/2010/02/Screenshot-2.png" alt="" title="Sierpinsky triangle with 5 levels of subdivision" width="485" height="364" class="size-full wp-image-700" /></a><p class="wp-caption-text">Sierpinsky triangle with 5 levels of subdivision</p></div></p>
<p>All done : )<br />
I leave the extruded version as an exercise for the reader.</p>
]]></content:encoded>
			<wfw:commentRss>http://modprobe.complex.ch/2010/02/28/sierpinsky-triangle-python-script-for-blender/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>make me&#8230; a Sierpinski Triangle !</title>
		<link>http://modprobe.complex.ch/2010/02/18/make-me-a-sierpinski-triangle/</link>
		<comments>http://modprobe.complex.ch/2010/02/18/make-me-a-sierpinski-triangle/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 21:57:51 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[makerbot]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://modprobe.complex.ch/?p=656</guid>
		<description><![CDATA[Printing Thursday rules ! Here is a Sierpinski triangle printed with Theo&#8217;s makerbot. The file was procedurally generated in Blender via its python api. The triangle has a 6 cm base and is 1 cm deep. Printing time 1h10&#8230;]]></description>
			<content:encoded><![CDATA[<p>Printing Thursday rules !</p>
<p>Here is a <a href="http://en.wikipedia.org/wiki/Sierpinski_triangle" target="_blank">Sierpinski triangle</a> printed with Theo&#8217;s makerbot.</p>
<p>The file was procedurally generated in <a href="http://www.blender.org/" target="_blank">Blender</a> via its python api.</p>
<p>The triangle has a 6 cm base and is 1 cm deep. Printing time 1h10&#8230;</p>
<p><a href="http://modprobe.complex.ch/wp-content/uploads/2010/02/sierpinski.jpg"><img class="alignleft size-full wp-image-661" title="sierpinski" src="http://modprobe.complex.ch/wp-content/uploads/2010/02/sierpinski.jpg" alt="" width="485" height="364" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://modprobe.complex.ch/2010/02/18/make-me-a-sierpinski-triangle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
