import math import random def setRandomValue(min,max): return (random.random()*(max - min))+min def scaleFactor(id, numOfLevel, parse): return (math.sin(id/(numOfLevel/2.7) + parse/5.0))+1 def makeLevel(bodyString, attrs, levelId): n = 0 while(n < attrs['numOfBranch']): #position yTheta = ((math.pi*2.0)/(attrs['numOfLevel']*1.0)) * levelId distance = attrs['stemBallDistance'] * math.cos(yTheta) theta = ((math.pi*2.0)/attrs['numOfBranch']) * n zTheta = ((math.pi*2.0)/(attrs['numOfLevel']*1.0)) * levelId ty = math.sin(zTheta)*attrs['stemBallDistance'] + attrs['ty'] tx = math.sin(theta) * distance + attrs['tx'] tz = math.cos(theta) * distance + attrs['tz'] #traslation noise tx *= setRandomValue(1,1.2) ty *= setRandomValue(1,1.2) tz *= setRandomValue(1,1.2) #rotation ry = (360.0/attrs['numOfBranch'])* (n*1.0) rx = (360.0/attrs['numOfLevel'])* (levelId*1.0) #rotation noise ry *= setRandomValue(.8,1.2) rx *= setRandomValue(.8,1.2) #size scale = attrs['plantSize'] #stem ball bodyString += '\t'+'TransformBegin\n' bodyString += '\t\t'+'Scale %g %g %g\n' % (scale,scale,scale) bodyString += '\t\t'+'Surface "plastic"\n' bodyString += '\t\t'+'Translate %g %g %g\n' % (tx,ty,tz) bodyString += '\t\t'+'Rotate %g 0 1 0\n' % ry bodyString += '\t\t'+'Rotate %g 1 0 0\n' % -rx #color red = 5 r= 1;g=1;b=1 if((math.floor(random.random() *100) % red) == 0): r = 1 g = 0 b = 0 bodyString += '\tAttributeBegin\n \t\t\tColor %g %g %g\n' % (r,g,b) #stem ball size noise stemBallSize = attrs['stemBallSize'] * setRandomValue(.8,1.1) bodyString += '\t\t'+'Sphere %g %g %g %g\n' % (stemBallSize, -stemBallSize, stemBallSize, 360) #branch ball bodyString += '\t'+'TransformBegin\n' bodyString += '\t\t'+'Translate 0 0 %g\n' % stemBallSize branchBallSize = (stemBallSize / 10.0) *attrs['branchBallSize'] #pin #pin lenght noise pinLength = attrs['pinLength'] * setRandomValue(.2,3) if((math.ceil(random.random()*100))%9 == 0.0): pinLength = attrs['pinLength'] * (setRandomValue(.2,10) * attrs['randomAmount']) bodyString += '\t\t'+'TransformBegin\n' bodyString += '\t\t'+'Cone %g %g 360\n' % (pinLength , branchBallSize) if((math.ceil(random.random()*100))%5 == 0.0): bodyString += '\t\t'+'Translate 0 0 %g\n' % pinLength bodyString += '\t\t'+'Sphere %g %g %g %g\n' % (branchBallSize, -branchBallSize, branchBallSize, 360) bodyString += '\t\t'+'TransformEnd\n' bodyString += '\t'+'TransformEnd\n' bodyString += '\tAttributeEnd\n' bodyString += 'TransformEnd\n' n += 1 return bodyString def makePlant(bodyString, id, seed,randomAmount): random.seed(seed+id) numOfArms = math.ceil(setRandomValue(3,12)) if(numOfArms %2 == 0): numOfArms += 1 areaWidth = 0 attrs = {'id' : id, 'tx' : setRandomValue(-areaWidth,areaWidth), 'ty' : setRandomValue(-areaWidth,areaWidth), 'tz' : setRandomValue(-areaWidth,areaWidth), 'numOfLevel' : numOfArms, 'levelHeight' : setRandomValue(1.0,1.0), 'plantSize' : setRandomValue(.5,.7), 'numOfBranch' : numOfArms, 'stemBallSize' : setRandomValue(.1,10), 'stemBallDistance' : setRandomValue(1,9), 'branchBallDistance' : setRandomValue(0,0), 'branchBallSize' : setRandomValue(.2,2), 'pinLength' : setRandomValue(1,3), 'randomAmount' : randomAmount } j = 0 while(j < attrs['numOfLevel']): bodyString = makeLevel(bodyString,attrs, j) j += 1 return bodyString def makeObjects(): #Make objects numOfPlants = 200 i = 0 while(i < numOfPlants): bodyString = '' seed = random.random() randomAmount = (1.0/numOfPlants) * i bodyString = makePlant(bodyString, i,seed,randomAmount) fileName = 'objects/objectProgress_step8.%04d.rib' % i file = open(fileName, 'w') file.write(bodyString) file.close() i += 1 makeObjects()