[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [812] branches/ivygen/ivy_test.py: - generation of branches works now

Florian Meyer florianfelix at web.de
Tue Jul 20 01:25:54 CEST 2010


Revision: 812
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-extensions&revision=812
Author:   testscreenings
Date:     2010-07-20 01:25:53 +0200 (Tue, 20 Jul 2010)

Log Message:
-----------
- generation of branches works now
- ominous bug is squashed
--> probably learned something about class instancing
- very slow with many iterations, mainly due to many prints
- no more empties --> curves instead, looks nicer

Modified Paths:
--------------
    branches/ivygen/ivy_test.py

Modified: branches/ivygen/ivy_test.py
===================================================================
--- branches/ivygen/ivy_test.py	2010-07-19 17:53:53 UTC (rev 811)
+++ branches/ivygen/ivy_test.py	2010-07-19 23:25:53 UTC (rev 812)
@@ -3,6 +3,7 @@
 import mathutils
 import random
 from ivy_functions import *
+import math
 
 
 def vertsToArray(root):
@@ -14,11 +15,11 @@
 def createIvyCurves(context, IVY):
     bpy.ops.object.select_all(action='DESELECT')
     scene = context.scene
-    splines = []
     curve = bpy.data.curves.new("IVY", type = 'CURVE')
 
     for root in IVY.ivyRoots:
-        newSpline = curve.splines.new(type = 'NURBS')
+        print('generate Root with', len(root.ivyNodes), 'verts')
+        newSpline = curve.splines.new(type = 'POLY')
         splineVerts = vertsToArray(root)
         #print('splineVerts', splineVerts, len(splineVerts))
         newSpline.points.add(int(len(splineVerts)*0.25 - 1))
@@ -66,8 +67,8 @@
     adhesionVector = (0,0,0)
     
     # a smoothed adhesion vector computed and used during the birth phase,
-	#  since the ivy leaves are align by the adhesion vector, this smoothed vector
-	#  allows for smooth transitions of leaf alignment
+    #  since the ivy leaves are align by the adhesion vector, this smoothed vector
+    #  allows for smooth transitions of leaf alignment
     smootAdhesionVector = (0,0,0)
     
     # length of the associated ivy branch at this node
@@ -80,8 +81,11 @@
     climbing = False
 
 
-class IvyRoot(IvyNode): #not sure about this. why subclassing. can put everything into IvyNode
+class IvyRoot(IvyNode):
     # List with containing IvyNodes
+    def __init__(self):
+        self.ivyNodes = []
+
     ivyNodes = []
     
     # living state of this Root
@@ -93,6 +97,9 @@
 
 class Ivy():
     # list of containing Roots
+    def __init__(self):
+        self.ivyRoots = []
+
     ivyRoots = []
     
     # the ivy size factor, influences the grow behaviour [0..0,1]
@@ -130,13 +137,14 @@
 
 
     ######### SEEDING ROOTS ###########
-    def seed(self, startLocation, parentOrder=0):
+    def seed(self, startLocation, parentOrder=0, primaryVector=mathutils.Vector((0,0,1))):
         tmpRoot = IvyRoot()             # create a new IvyRoot
         tmpRoot.parents = parentOrder   # set childstatus
 
         tmpIvy = IvyNode()              # new IvyNode
         tmpIvy.position = startLocation # position of IvyNode
-        tmpIvy.primaryVector = mathutils.Vector((0,0,1))
+        tmpIvy.primaryVector = primaryVector
+        tmpIvy.length = 0.0001          # avoid division by zero
 
         tmpRoot.ivyNodes.append(tmpIvy) # put IvyNode int RootNode
         self.ivyRoots.append(tmpRoot)   # put RootNode into Ivy
@@ -147,11 +155,15 @@
     def grow(self, props):
         
         # newRoots = []
-
+        rootNr = 0
         for root in self.ivyRoots:
+            print('\nrootNr', rootNr, 'parents', root.parents)
+            rootNr += 1
+            if root.alive == False:
+                continue
 
             prevIvy = root.ivyNodes[-1]
-
+            print('rootstart', root.ivyNodes[0].position)
             # RANDOM VECTOR
             randomVector = (mathutils.Vector((random.random()-0.5,
                                               random.random()-0.5,
@@ -183,16 +195,39 @@
             tmpIvy.primaryVector = ((prevIvy.primaryVector +
                                    (tmpIvy.position - prevIvy.position)).normalize())
 
+            # length to this IvyNode from start of root
+            tmpIvy.length = prevIvy.length + mathutils.Vector((tmpIvy.position - prevIvy.position)).length
+            
             # put IvyNode into ROOT
             root.ivyNodes.append(tmpIvy)
 
 
             # test for branching
             # if branching: self.seed(startLocation = tmpIvy.position, parentOrder = root.parentOrder + 1)
-            print(len(root.ivyNodes))
+            #print(len(root.ivyNodes))
+            
+            # test for maximum rootdepth and death
+            if root.parents > 3 or root.alive == False:
+                continue
+                
+            # weight depending on ratio of node length to total length
+            weight = 1.0 - (math.cos(tmpIvy.length / prevIvy.length * 2.0 * 0.5 + 0.5))
+            weight = 1.1
+            probability = random.random()
+            print('probability', probability * weight)
+            if (probability * weight > self.branchingProbability):
+                print('seeding at', tmpIvy.position)
+                
+                self.seed(startLocation = tmpIvy.position,
+                            parentOrder = root.parents +1,
+                            primaryVector = tmpIvy.primaryVector)
+                self.ivyRoots[-1].ivyNodes[0].position = tmpIvy.position
+                            
+                print('seeded at:', self.ivyRoots[-1].ivyNodes[0].position)
+                print('newRootsLength', len(self.ivyRoots[-1].ivyNodes))
+                pass # seed new root
 
 
-
 def main(context, props, data):
     seedPoint = context.scene.cursor_location  #start the Ivy at the cursor
 
@@ -200,17 +235,18 @@
 
     scene = context.scene
     obj = context.active_object
-    IVY = Ivy() #create main IvyObject stores all the IvyNodes
+    IVY = Ivy() #create main IvyObject stores all the IvyRoots.IvyNodes
 
     # PROBLEM: SOMEHOW DOESN'T WORK
     IVY.ivyRoots = [] #empty the roots list
 
-    IVY.seed(seedPoint, 0) #set inital IvyRoot
+    IVY.seed(seedPoint, parentOrder=0, primaryVector=mathutils.Vector((0,0,1))) #set inital IvyRoot
 
     for i in range(props.ivyIter): #for now usercontrollable growiterations
         IVY.grow(props)
 
     print('IvyNodes:', len(IVY.ivyRoots[0].ivyNodes))
+    print('IvyRoots:', len(IVY.ivyRoots))
     #createIvy(context, IVY) # create emptis at IvyRoots
     createIvyCurves(context, IVY) #create Curverepresentation of the Ivy
 
@@ -228,7 +264,7 @@
                             default=0)
     ivyIter = IntProperty(name='Iterations',
                             min=0, soft_min=0,
-                            default=25)
+                            default=9)
     ivyGravityWeigth = FloatProperty(name='Gravity Weight',
                             min=0,soft_min=0,
                             max=1,soft_max=1,




More information about the Bf-extensions-cvs mailing list