Skip to content

Instantly share code, notes, and snippets.

@greut
Last active March 1, 2017 08:58
Show Gist options
  • Save greut/36215ceb4bddcc8de41f2819d21bb1e4 to your computer and use it in GitHub Desktop.
Save greut/36215ceb4bddcc8de41f2819d21bb1e4 to your computer and use it in GitHub Desktop.
Blender 3-D L-System
import bpy
from math import cos, sin, radians
grammaire = {
"A": "B[+A][-A][<A]>A",
"B": "BB"
}
def génération(word, depth):
if depth <= 0:
return word
return "".join(grammaire.get(c, c) for c in génération(word, depth - 1))
def dessin(word, size, angle):
x, y, z = (0, 0, 0)
a, b = (0, 0)
states = []
for i,c in enumerate(word):
if c == '+':
a += angle
if a > 180:
a -= 360
elif c == '-':
a -= angle
if a < -180:
a += 360
elif c == '<':
b += angle
if b > 90:
b -= 180
elif c == '>':
b -= angle
if b < -90:
b += 180
elif c == '[':
states.append(((x, y, z), (a, b)))
elif c == ']':
((x, y, z), (a, b)) = states.pop()
else:
me = bpy.data.meshes.new(f"Mesh{i}")
ob = bpy.data.objects.new(f"{i}", me)
ob.location = (0, 0, 0)
bpy.context.scene.objects.link(ob)
dx = size * cos(radians(b)) * sin(radians(a))
dy = size * cos(radians(b)) * cos(radians(a))
dz = size * sin(radians(b))
me.from_pydata([(x, y, z),(x+dx, y+dy, z+dz)], [(0, 1)], [])
me.update(calc_edges=True)
x += dx
y += dy
z += dz
dessin(génération("A", 4), 1, 62)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment