L-System Script

Share your scripts with other members
User avatar
Ron
Posts: 43
Joined: Wed May 28, 2008 1:57 pm

L-System Script

Postby Ron » Fri Oct 15, 2010 1:39 am

I have not been able to work on my L-System Script for a while, because I no longer work for a company that has a license and I am not going to pay $5k for it my self. Especially since I can get Houdini apprentice for free. So I thought I would post my last RealFlow script for everyone. It still only works in 2D but maybe someone else can improve it. I was also hoping to give it a nice GUI too but I didn't get to it so the code might be a little messy. But it should be readable.

My axis setup is zxy. If you have a different setup then change the folllowing:
initialDirection

The only variables you should change are:
numberOfGenerations
initialLength
numberOfPoints
delta
premise
rules


I hope you like it.
Ron

Code: Select all

import math

#--------------------------------------------------
# Function: onSimulationBegin
#--------------------------------------------------

def onSimulationBegin():
   ruleCount = 0
   generationCount = 0
   numberOfGenerations = 4
   initialLength = 0.5
   numberOfPoints = 3
   delta = 22.5
   alpha = 90

   ruleVariable = []
   rule = []

   temp1 = ''
   symbol = '+-^&/?|$[]{}()G.~!`%'

   premise = 'F'   
   #premise = 'F+F+F+F'
   lSystem = premise

   #rules = ['F=FF-[-F+F+F]+[+F-F-F]']
   rules = ['F=FfF', 'f=fff']
   #rules = ['F=F-F+F+FF-F-F+F']
   #rules = ['F=FF-[-F+F+F]+[+F-F-F]']
   #rules = ['F=FfF', 'f=fff']
   #rules = ['F=F+f-FF+F+FF+Ff+FF-f+FF-F-FF-Ff-FFF', 'f=ffffff']
   numberOfRules = len(rules)

   while (ruleCount < numberOfRules):
      rules[ruleCount] = rules[ruleCount].replace(' ', '')
      temp = rules[ruleCount].split('=')
      ruleVariable.append(temp[0])
      rule.append(temp[1])
      ruleCount += 1

   while (generationCount < numberOfGenerations):
      posCount = 0
      lSystemLength = len(lSystem)

      while (posCount < lSystemLength):
         ruleCount = 0
         while (ruleCount < numberOfRules):
            if (lSystem[posCount] == ruleVariable[ruleCount]):
               temp1 += rule[ruleCount]
            ruleCount += 1
         if (lSystem[posCount] in symbol):
            temp1 += lSystem[posCount]
         posCount += 1
      lSystem = temp1
      temp1 = ''
      generationCount += 1

   scene.message(str(len(lSystem)))

   velocity = Vector.new(0,0,0)
   initialDirection = Vector.new(0,1,0)
   initialPosition = Vector.new(0,0,0)
   lSystemSource = scene.getEmitter("Circle01")
   lSystemSource.setParameter("Type", "Elastics")
   lSystemSource.setParameter("Speed", 0.0)

   def degToRad(angle):
      return (angle*3.141593)/180

   lSystemLength = len(lSystem)

   posCount = 0
   posXold = 0
   posYold = 0
   pointSpacing = initialLength/numberOfPoints
   dataXpos = []
   dataYpos = []
   dataAngle = []
   n = 0

   while (posCount < lSystemLength):
      if (lSystem[posCount] == 'F'):
         pointCount = 0
         while (pointCount < numberOfPoints):
            posXnew = posXold + (pointSpacing)*math.cos(degToRad(alpha))
            posYnew = posYold + (pointSpacing)*math.sin(degToRad(alpha))
            position = Vector.new(posXnew, posYnew, 0)
            lSystemSource.addParticle(position, velocity)
            posXold = posXnew
            posYold = posYnew
            pointCount += 1

      if (lSystem[posCount] == 'f'):
            posXnew = posXold + (initialLength)*math.cos(degToRad(alpha))
            posYnew = posYold + (initialLength)*math.sin(degToRad(alpha))
            posXold = posXnew
            posYold = posYnew

      if (lSystem[posCount] == '['):
         dataXpos.append(posXold)
         dataYpos.append(posYold)
         dataAngle.append(alpha)

      if (lSystem[posCount] == ']'):
         posLen = len(dataXpos)
         angleLen = len(dataAngle)
         posXold = dataXpos[posLen-1]
         posYold = dataYpos[posLen-1]
         alpha = dataAngle[angleLen-1]
         if (posLen != 0):
            dataXpos.pop()
            dataYpos.pop()
            dataAngle.pop()

      if (lSystem[posCount] == '+'):
         alpha += delta

      if (lSystem[posCount] == '-'):
         alpha -= delta

      posCount +=1


Grakus
Posts: 7
Joined: Fri Sep 04, 2009 4:32 pm

L-System Script

Postby Grakus » Fri Oct 15, 2010 8:37 am

Ron: Thank you :)

User avatar
D&Esign
Posts: 112
Joined: Thu Oct 15, 2009 7:44 am
Contact:

L-System Script

Postby D&Esign » Fri Oct 15, 2010 9:55 am

Shame U couldn't carry on Your marvelous work. in terms of scripting it surely is one of a kind. Shell be pleased to see Your progressions in Houdini.

Congratulations:
Diego

Ps: Thought You maight like to see something cool that concerns similar algorithms as Yours by this very talented Artist: Click me

User avatar
Ron
Posts: 43
Joined: Wed May 28, 2008 1:57 pm

L-System Script

Postby Ron » Fri Oct 15, 2010 4:47 pm

Thanks for the kind words guys I'm glad you like it.

D&Esign:
Thanks for the link I really like his videos. The ones that jitter as they morph into new shapes are great.

Ron

User avatar
Ron
Posts: 43
Joined: Wed May 28, 2008 1:57 pm

L-System Script

Postby Ron » Thu Oct 21, 2010 2:53 pm

If anybody cares here is my original WIP thread with examples of what the script can do.

Original WIP Thread

Later
Ron


Return to “User Scripts”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest