tree-gui.py

import Blender

from Blender import *

from math import *



##################################################################################################



def rawMesh():

	return NMesh.GetRaw()



def drawPoint(m,f,x,y,z):

	m.verts.append(NMesh.Vert(x,y,z))

	f.v.append(m.verts[-1])



def drawFace(m,x1,y1,z1,x2,y2,z2,x3,y3,z3):

	f=NMesh.Face()

	drawPoint(m,f,x1,y1,z1)

	drawPoint(m,f,x2,y2,z2)

	drawPoint(m,f,x3,y3,z3)

	m.faces.append(f)



def drawTube(m,r1,r2,h,n,x,y,z,hx,hy):

	for j in range(0, n):

		a1=(j-1)*(2*pi/n)

		a2=j*(2*pi/n)

		drawFace(m,x+r1*cos(a1),y+r1*sin(a1),z,x+r1*cos(a2),y+r1*sin(a2),z,hx+r2*cos(a2),hy+r2*sin(a2),z+h)

		drawFace(m,hx+r2*cos(a2),hy+r2*sin(a2),z+h,hx+r2*cos(a1),hy+r2*sin(a1),z+h,x+r1*cos(a1),y+r1*sin(a1),z)

	

def drawSphere(m,r,n1,n2):

	for j in range(0,n2):

		drawTube(m,r*sin((j*pi)/n2),r*sin(((j+1)*pi)/n2),r*cos((j*pi)/n2)-r*cos(((j+1)*pi)/n2),n1,0,0,-r*cos((j*pi)/n2),0,0)

		

def linkObject(obj,m):

	ob1=Object.New("Mesh",obj)

	ob1.link(m)

	Scene.GetCurrent().link(ob1)

	Redraw(1)



####################################################################################################



class Tree:

	m=rawMesh()

	arr=rawMesh()



	def drawLeaf(self,x,y,z,j,n,cx,cy,h,n2,p,w):

		a = -(j-1)*(2*pi/n)

		rin=w*5*sqrt((y-cy)*(y-cy)+(x-cx)*(x-cx))

		rout=w*10*sqrt((y-cy)*(y-cy)+(x-cx)*(x-cx))

		drawFace(self.arr,x,y,z,x+rin*cos(a),y+rin*sin(a),z,x+rout*cos(a),y+rout*sin(a),z+w)

		drawFace(self.arr,x,y,z,x-rin*cos(a),y-rin*sin(a),z,x+rout*cos(a),y+rout*sin(a),z+w)

		drawTube(self.arr,0.01,0.0,h/n2,2,cx,cy,z-h/n2,x,y)

	

	def arrayLeaves(self,r2,x2,y2,z,n,p,h,n2,x1,y1,amt,wid):

		for j in range(0,amt):

			self.drawLeaf(x2+r2*cos((1.0*n*j/amt-1)*(2*pi/n)),y2+r2*sin((1.0*n*j/amt-1)*(2*pi/n)),z,1.0*n*j/amt,n,x1,y1,h,n2,p,wid)



	def drawBranch(self,r1,r2,h,n,x,y,z,hx,hy,n2,p,amt,wid):

		for j in range(0,n2):

			x1=x+j*((hx-x)/n2)

			y1=y+j*((hy-y)/n2)

			x2=x+(j+1)*((hx-x)/n2)

			y2=y+(j+1)*((hy-y)/n2)

			drawTube(self.m,r1+(j)*((r2-r1)/n2),r1+(j+1)*((r2-r1)/n2),h/n2,n,x1,y1,z+j*(h/n2),x2,y2)

			if(p):self.arrayLeaves(r1+(j+1)*((r2-r1)/n2),x2,y2,z+j*(h/n2)+(h/n2),n,p,h,n2,x1,y1,amt,wid)



	def drawTree(self,d,d2,n,b,cx,cy,cz,p,amt,wid,s,k,end,end2,r):

		a=[0,0,0,0,0]

		p2=0.0

		for j in range(1,n+1):

			h=(cz[j]-cz[j-1])	

			i1=(1.0*n-j+1)/(1.0*n)

			i2=(1.0*n-j)/(1.0*n)

			l=[1,1,1,1,1]

			for i in range(0,5):

				if(i<j-end2):l[i]=s

			for a[0] in range(0,l[0]):

				for a[1] in range(0,l[1]):

					for a[2] in range(0,l[2]):

						for a[3] in range(0,l[3]):

							for a[4] in range(0,l[4]):

								k1=k2=k3=k4=0

								for i in range(0,j-end2-1):

									k1=k1+(i%2)*k*cos(a[i]*2*pi/s+(i+r))+((i+1)%2)*k*sin(a[i]*2*pi/s+(i+r))

									k2=k2+(i%2)*k*sin(a[i]*2*pi/s+(i+r))+((i+1)%2)*k*cos(a[i]*2*pi/s+(i+r))

								for i in range(0,j-end2):

									k3=k3+(i%2)*k*cos(a[i]*2*pi/s+(i+r))+((i+1)%2)*k*sin(a[i]*2*pi/s+(i+r))

									k4=k4+(i%2)*k*sin(a[i]*2*pi/s+(i+r))+((i+1)%2)*k*cos(a[i]*2*pi/s+(i+r))

								if(j==n):h=k*abs(cz[j]-cz[0])*(cz[j]-cz[j-1])/((k3*k3+k4*k4+abs(cz[j]-cz[0])))

								if(j==n-end+1):p2=p

								self.drawBranch(b*i1,b*i2,h,d2,cx[j-1]+k1,cy[j-1]+k2,cz[j-1],cx[j]+k3,cy[j]+k4,d,p2,amt,wid)

		

	def create(self,obj,d,d2,n,b,cx,cy,cz,p,amt,wid,s,k,end,end2,r):

		self.drawTree(d,d2,n,b,cx,cy,cz,p,amt,wid,s,k,end,end2,r)

		linkObject(obj,self.m)

		if(end):linkObject(obj+"_leaves",self.arr)



####################################################################################################



class Panel:

	name=Draw.Create("Tree")

	x=Draw.Create(0.0)

	y=Draw.Create(0.0)

	z=Draw.Create(-5.75)

	d=Draw.Create(4)

	c=Draw.Create(5)		

	symm=Draw.Create(4)

	b=Draw.Create(0.35)

	b_end=Draw.Create(2)	

	h=Draw.Create(10.5)

	spr=Draw.Create(1.5)

	lf_end=Draw.Create(2)		

	lf_size=Draw.Create(0.1)

	lf_amt=Draw.Create(7)

	lf_spr=Draw.Create(0.2)

	rfac=Draw.Create(0.5)

		

	def key(self,evt,val):

		if(evt==Draw.ESCKEY):Draw.Exit()



	def but(self,evt):

		if(evt==1):

			r=-self.rfac.val/2+self.rfac.val*(Blender.sys.time()%1)

			d2=self.d.val*2

			cx=[self.x.val,self.x.val+r,self.x.val+2.5*r,self.x.val+3*r,self.x.val+1.5*r,self.x.val+r]

			cy=[self.y.val,self.y.val+r,self.y.val+2.5*r,self.y.val+3*r,self.y.val+1.5*r,self.y.val+r]

			cz=[self.z.val,self.z.val+self.h.val/(self.c.val)+r,self.z.val+2*self.h.val/(self.c.val)+2*r,self.z.val+3*self.h.val/(self.c.val)+r,self.z.val+4*self.h.val/(self.c.val)+2*r,self.z.val+5*self.h.val/(self.c.val)]

			t=Tree()

			t.create(self.name.val,self.d.val,d2,self.c.val,self.b.val,cx,cy,cz,self.lf_size.val,self.lf_amt.val,self.lf_spr.val,self.symm.val,self.spr.val,self.lf_end.val,self.b_end.val,10*r)

			Draw.Exit()

		elif(evt==2):

			Draw.Exit()

		else:

			Draw.Redraw(1)



	def gui(self):

		self.name=Draw.String("Name: ",0,10,400,160,20,self.name.val,25,"Tree name")

		self.x=Draw.Number("locX: ",0,10,370,100,20,self.x.val,-20.0,20.0,"X-location")

		self.y=Draw.Number("locY: ",0,120,370,100,20,self.y.val,-20.0,20.0,"Y-location")

		self.z=Draw.Number("locZ: ",0,230,370,100,20,self.z.val,-20.0,20.0,"Z-location")

		self.d=Draw.Number("Resolution: ",0,10,340,160,20,self.d.val,2,20,"Number of divisions")

		self.c=Draw.Number("Sections: ",0,10,310,160,20,self.c.val,2,5,"Number of splits")

		self.symm=Draw.Number("Symmetry: ",0,10,280,160,20,self.symm.val,2,10,"Radial symmetry")

		self.b=Draw.Number("Base: ",0,10,250,160,20,self.b.val,0.0,10.0,"Width of trunk base")

		self.b_end=Draw.Number("Trunk Sections: ",0,10,220,160,20,self.b_end.val,0,5,"Length of trunk base")

		self.h=Draw.Number("Height: ",0,10,190,160,20,self.h.val,-20.0,20.0,"Tree height")

		self.spr=Draw.Number("Spread: ",0,10,160,160,20,self.spr.val,1.0,3.0,"Branch spread factor")

		self.lf_end=Draw.Number("Leaf Sections: ",0,10,130,160,20,self.lf_end.val,0,5,"Length of leaf branch")

		self.lf_size=Draw.Number("Leaf Size: ",0,10,100,160,20,self.lf_size.val,0.1,5.0,"Width of leaf")

		self.lf_amt=Draw.Number("Leaf Amount: ",0,10,70,160,20,self.lf_amt.val,1,20,"Radial number of leaves")

		self.lf_spr=Draw.Number("Leaf Spread: ",0,10,40,160,20,self.lf_spr.val,0.1,1.5,"Length of leaf")

		self.rfac=Draw.Number("Randomness: ",0,10,10,160,20,self.rfac.val,0.0,1.5,"Amount of uniqueness")

		Draw.PushButton("Create",1,230,40,100,20,"Add tree")

		Draw.PushButton("Cancel",2,230,10,100,20,"Close panel")



	def start(self):	

		Draw.Register(self.gui,self.key,self.but)



def main():

	p=Panel()

	p.start()



main()

Is this some sort of trick thread? What does the script do?

it looks like a tree generator…I did not look at all the math, but it basically loops through creating geometry and “leaves” and whatnot.

…Numby… please explain… :.(

Nice start, but the tree could use a bit more randomness.

The script is for 2.49. It creates a simple set of parameters that you can adjust and then execute to create a tree. I agree that the initial post is strangely devoid of any explanation.

thank-you so much for trying this. You’re right about the randomness oversampling…

What I want is to get as many suggestions from the community as possible to create a deluxe version for Blender users. Any comment on symmetry?

I want artists start using the Blender internals properly so I have posted an example of a simple python gui in class form (for noobs) if you want to help noobs, they can avoid problems in bad programming and still get really complex models. This tree is as basic as I could make it

I’m sorry I didn’t explain…not big on explaining…does 2.49 have something special? I haven’t tried 2.5 yet but if it’s a lot different I will have to see…
)

Does anyone still use a P4?