tag_tag

Arbre fractal

Par position/rotation (polygonal) :

fractalTree.py
from maya.cmds import polyCube,xform
from math import cos,sin
 
def tree(x,y,z,a,b,l,w,r,s,it,lvl=0):
    if(lvl<it):
        polyCube(h=l,w=w,d=w)
        xform(relative=1,
              translation=(x,y,z),
              rotation=(-50*b,0,90+a*50),
              pivots=(0,l*.5,0)
              )
        x2 = x + l*cos(a)*cos(b)
        y2 = y + l*cos(a)*sin(b)
        z2 = z + l*sin(a)
        tree(x2,y2,z2,a+s,b,l*r,w*r,r,s,it,lvl+1)
        tree(x2,y2,z2,a-s,b,l*r,w*r,r,s,it,lvl+1)
        tree(x2,y2,z2,a,b+s,l*r,w*r,r,s,it,lvl+1)
        tree(x2,y2,z2,a,b-s,l*r,w*r,r,s,it,lvl+1)
 
tree(0,0,0,     # position
     0,0,       # angle (alpha,beta)
     1.0,.1,    # size (length,width)
     .7,        # size factor
     1,         # spreading (0<x<pi/2)
     5)         # iterations
 

Par position (courbes) :

fractalTreeCV.py
from maya.cmds import curve
from math import cos,sin
 
class FractalTree():
    '''Tree in one curve (inexact but light)'''
    def __init__(self):
        self.pts = []
        self.it = 7      # tree iterations
        self.r = .7      # branch length factor
        self.s = .5      # branch spreading (0<x<pi/2)
        self.d3 = True   # 3D or 2D tree
        self.branch()
        curve(p=self.pts,d=1)
    def branch(self,
               x=0,y=0,z=0, # branch position
               a=0,b=0,     # branch angle
               l=10,        # branch length
               lvl=0        # branch iteration
               ):
        if(lvl<self.it):
            self.pts.append((x,y,z))
            x2 = x + l*cos(a)*cos(b)
            y2 = y + l*cos(a)*sin(b)
            z2 = z + l*sin(a)
            self.branch(x2,y2,z2,a+self.s,b,l*self.r,lvl+1)
            self.branch(x2,y2,z2,a-self.s,b,l*self.r,lvl+1)
            if(self.d3):
                self.branch(x2,y2,z2,a,b+self.s,l*self.r,lvl+1)
                self.branch(x2,y2,z2,a,b-self.s,l*self.r,lvl+1)
 
tree = FractalTree()
fractalTreeMultiCV.py
from maya.cmds import curve
from math import cos,sin
 
class FractalTree():
    '''Every branch is a curve (exact, 20.000+ curves in it-8)'''
    def __init__(self):
        self.it = 6      # tree iterations
        self.r = .7      # branch length factor
        self.s = .5      # branch spreading (0<x<pi/2)
        self.d3 = True   # 3D or 2D tree
        self.branch()
    def branch(self,
               x=0,y=0,z=0, # branch position
               a=0,b=0,     # branch angle
               l=10,        # branch length
               lvl=0        # branch iteration
               ):
        if(lvl<self.it):
            x2 = x + l*cos(a)*cos(b)
            y2 = y + l*cos(a)*sin(b)
            z2 = z + l*sin(a)
            curve(p=[(x,y,z),(x2,y2,z2)],d=1)
            self.branch(x2,y2,z2,a+self.s,b,l*self.r,lvl+1)
            self.branch(x2,y2,z2,a-self.s,b,l*self.r,lvl+1)
            if(self.d3):
                self.branch(x2,y2,z2,a,b+self.s,l*self.r,lvl+1)
                self.branch(x2,y2,z2,a,b-self.s,l*self.r,lvl+1)
 
tree = FractalTree()

~~DISCUSSION~~