r/Advanced_3DPrinting • u/Dirk_39 • 5d ago
Randomly shaped trees with OpenSCAD
Fabulous! In just a few lines, OpenSCAD makes it possible to construct a tree whose appearance changes with every execution, thanks to the "rands" instruction, which generates random values. A variable "h"—which is recalculated at each level— translate([0,0,h]) —determines the starting point for the branches at each level. Meanwhile, the variable "b"—also updated at each level within the "for" loop— b=[0:120:240]) —spreads the branches out across the X and Y axes of the tree. Below are the few lines of code used to create these magnificent trees.
fn = 12;
module FEUILLE() {color("lightgreen")scale([1,0.5,0.15])sphere(r=6,$fn=16);}
module ARBRE(n,h,r) {if(n>0) {color("#d80")cylinder(h=h,r1=r,r2=r*0.7);translate([0,0,h])rotate([0,0,rands(0,360,1)[0]])for(b=[0:120:240])rotate([0,0,b])rotate([0,30,0])ARBRE(n-1,h*0.7,r*0.7);} else {FEUILLE();}}
ARBRE(5,50,2);
La commande ARBRE(n,h,r) se définit ainsi :
n : nombre di niveaux des ramages
h : point de départ des faisceaux de ramage
r : épaisseur des branches
Pour plus d'info voir 🔥 https://robotix.ah-oui.org/site/main.php...