[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [811] branches/ivygen/ivy_test.py: -commit this anyway

Florian Meyer florianfelix at web.de
Mon Jul 19 19:53:53 CEST 2010


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

Log Message:
-----------
-commit this anyway
-rewritten most of it
-is now in line with the outline in the wiki
-has still one bug i can't seem to squash
-but branching is now possible
-needs only implementation of the branching process
-much cleaner and more readable

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

Modified: branches/ivygen/ivy_test.py
===================================================================
--- branches/ivygen/ivy_test.py	2010-07-19 16:53:51 UTC (rev 810)
+++ branches/ivygen/ivy_test.py	2010-07-19 17:53:53 UTC (rev 811)
@@ -4,21 +4,32 @@
 import random
 from ivy_functions import *
 
-def createIvy(context, IVY):
-    last = None
-    for root in IVY.ivy_Roots:
-        bpy.ops.object.add(type='EMPTY',
-                            location=root.position)
-        context.active_object.empty_draw_type = 'PLAIN_AXES'
-        context.active_object.name ='RootNode'+str(root.chainNumber)
-        ''' WTF why isn't this working
-        if last:
-            context.active_object.parent = last
-            last = context.active_object
-        else:
-            last = context.active_object        
-        '''
-        #print(root.position)
+
+def vertsToArray(root):
+    splineVerts = []
+    for ivyNode in root.ivyNodes:
+        splineVerts += ivyNode.position.copy().resize4D().to_tuple()
+    return splineVerts
+
+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')
+        splineVerts = vertsToArray(root)
+        #print('splineVerts', splineVerts, len(splineVerts))
+        newSpline.points.add(int(len(splineVerts)*0.25 - 1))
+        newSpline.points.foreach_set('co', splineVerts)
+        newSpline.order_u = 2
+        newSpline.endpoint_u = True
+
+    newCurve = bpy.data.objects.new("IVY_Curve", curve)
+    scene.objects.link(newCurve)
+    newCurve.select = True
+    scene.objects.active = newCurve
         
 def computeBoundingSphere(objs):
     #needs improvement not really a bounding sphere because of use of
@@ -38,114 +49,177 @@
     return [center, radius]
 
 class IvyNode(): #IvyNode Baseclass
-    position = mathutils.Vector()
-    primaryVector = mathutils.Vector((0,0,0.1))
-    adhesionVector = mathutils.Vector()
-    smoothAdhesionVector = mathutils.Vector()
+    '''
+    def __init__(self, climbing, length, floatingLength):
+        self.climbing = False
+        self.length = 0.0
+        self.floatingLength = 0.0
+    '''
+    
+    # node position
+    position = mathutils.Vector((0,0,0))
+    
+    # primary grow direction, a weighted sum of the previous directions
+    primaryVector = (0,0,1)
+    
+    #adhesion vector as a result from other scene objects
+    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
+    smootAdhesionVector = (0,0,0)
+    
+    # length of the associated ivy branch at this node
     length = 0.0
+    
+    # length at the last node that was climbing
     floatingLength = 0.0
-    climbing = True
+    
+    # climbing state
+    climbing = False
 
 
 class IvyRoot(IvyNode): #not sure about this. why subclassing. can put everything into IvyNode
-    nodes = []
+    # List with containing IvyNodes
+    ivyNodes = []
+    
+    # living state of this Root
     alive = True
+    
+    # number of parents, represents the level in the root hierarchy
     parents = 0
-    chainNumber = 0
 
 
 class Ivy():
+    # list of containing Roots
+    ivyRoots = []
+    
+    # the ivy size factor, influences the grow behaviour [0..0,1]
+    ivySize = 1.0
+    
+    # leaf size factor [0..0,1]
+    leafSize = 1.0
+    
+    # branch size factor [0..0,1]
+    branchSize = 1.0
+    
+    # maximum length of an ivy branch segment that is freely floating [0..1]
+    maxFloatingLength = 0.1
+    
+    # maximum distance for adhesion of scene object [0..1]
+    maxAdhesionDistance = 0.1
+    
+    # weight for the primary grow vector [0..1]
+    primaryWeight = 0.5
+    
+    # weight for the random influence vector [0..1]
+    randomWeight = 0.5
+    
+    # weight for the gravity vector [0..1]
+    gravityWeight = 0.5
+    
+    # weight for the adhesion vector [0..1]
+    adhesionWeight = 0.5
+    
+    # the probability of producing a new ivy root per iteration [0..1]
+    branchingProbability = 0.95
+    
+    # the probability of creating a new ivy leaf [0..1]
+    leafingProbability = 0.7
 
-    # obj = None
 
-    def __init__(self, obj):
-        self.obj = obj
+    ######### SEEDING ROOTS ###########
+    def seed(self, startLocation, parentOrder=0):
+        tmpRoot = IvyRoot()             # create a new IvyRoot
+        tmpRoot.parents = parentOrder   # set childstatus
 
-    #the ivyRoots
-    ivy_Roots = []
+        tmpIvy = IvyNode()              # new IvyNode
+        tmpIvy.position = startLocation # position of IvyNode
+        tmpIvy.primaryVector = mathutils.Vector((0,0,1))
 
-    def resetSettings(self):
-        #probably resets the settings :)
-        pass
+        tmpRoot.ivyNodes.append(tmpIvy) # put IvyNode int RootNode
+        self.ivyRoots.append(tmpRoot)   # put RootNode into Ivy
 
-    #initialize a new ivy root
-    def seed(self):
-        tmpSeed = IvyRoot()
-        tmpSeed.position = bpy.context.scene.cursor_location
-        self.ivy_Roots.append(tmpSeed)
-        
-    #one single grow iteration
-    def grow(self, props):
-        tmpRoot = IvyRoot() # create new RootNode
 
-        #generate random Vector with slight upvector
-        randomVector = (mathutils.Vector((random.random()-0.5, random.random()-0.5, random.random()-0.5)) +
-                     mathutils.Vector((0,0,0.1)))
 
-        #primaryVector
-        primaryVector = self.ivy_Roots[-1].primaryVector
+    ######### one GROW iteration ######
+    def grow(self, props):
         
-        #AdhesionVector
-        adhesionVector = computeAdhesion(self.obj, self.ivy_Roots[-1].position)
+        # newRoots = []
 
-        #compute the growVector
-        '''growVec = localIvySize * (primaryVector * primaryWeight *
-                                    randomVector * randomWeight *
-                                    adhesionVector * adhesionWeight)'''
+        for root in self.ivyRoots:
 
-        #set position of new RootNode --> replace with growVector
-        tmpRoot.position = (self.ivy_Roots[-1].position +           #just using the randomVector for now
-                            randomVector * props.ivyRandomWeight +
-                            primaryVector * props.ivyPrimaryWeight)   #
+            prevIvy = root.ivyNodes[-1]
 
-        #do the collisiondetection
-        #computeCollision(obj=self.obj, old_pos=self.ivy_Roots[-1].position, new_pos=tmpRoot.position, tmpRoot=tmpRoot)
+            # RANDOM VECTOR
+            randomVector = (mathutils.Vector((random.random()-0.5,
+                                              random.random()-0.5,
+                                              random.random()-0.5)) +
+                            mathutils.Vector((0,0,0.1)))
+            
+            # PRIMARY VECTOR
+            primaryVector = prevIvy.primaryVector
 
-        # add to the chainNumber
-        if tmpRoot.chainNumber == 0 or tmpRoot.chainNumber == self.ivy_Roots[-1].chainNumber:
-            tmpRoot.chainNumber = self.ivy_Roots[-1].chainNumber + 1
-        #print(self.ivy_Roots[-1].chainNumber, self.ivy_Roots[-1].position)
+            # ADHESION VECTOR
 
-        
-        #compute this Roots primaryVector
-        tmpRoot.primaryVector = ((self.ivy_Roots[-1].primaryVector +
-                                (tmpRoot.position - self.ivy_Roots[-1].position)).normalize())
-        
-        self.ivy_Roots.append(tmpRoot) #put into list of IvyNodes
 
+            # GROW VECTOR
+            growVector = (randomVector * props.ivyRandomWeight +
+                          primaryVector * props.ivyPrimaryWeight)
 
-    #creates the ivy triangle mesh
-    def birth():
-        pass
-    #the ivy roots
-    #again, what is this?:  std::vector<IvyRoot> roots
+            # modify with GRAVITY VECTOR
 
+            # compute COLLISION
 
+            # create new IvyNode
+            tmpIvy = IvyNode()
 
+            # set position
+            tmpIvy.position = prevIvy.position + growVector
+            #print('tmpIvy.position', tmpIvy.position)
+
+            # set new PRIMARY VECTOR
+            tmpIvy.primaryVector = ((prevIvy.primaryVector +
+                                   (tmpIvy.position - prevIvy.position)).normalize())
+
+            # 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))
+
+
+
 def main(context, props, data):
     seedPoint = context.scene.cursor_location  #start the Ivy at the cursor
-    gravity = mathutils.Vector((0,0,-1))  #not used yet
-    upVector = mathutils.Vector((0,0,1))  #not used yet
+
     random.seed(props.ivySeed)  #controlling the random seed
 
+    scene = context.scene
     obj = context.active_object
-    IVY = Ivy(obj) #create main IvyObject stores all the IvyNodes
-    IVY.ivy_Roots = [] #empty the roots list
+    IVY = Ivy() #create main IvyObject stores all the IvyNodes
 
-    IVY.seed() #set inital IvyNode
+    # PROBLEM: SOMEHOW DOESN'T WORK
+    IVY.ivyRoots = [] #empty the roots list
 
+    IVY.seed(seedPoint, 0) #set inital IvyRoot
+
     for i in range(props.ivyIter): #for now usercontrollable growiterations
         IVY.grow(props)
 
+    print('IvyNodes:', len(IVY.ivyRoots[0].ivyNodes))
+    #createIvy(context, IVY) # create emptis at IvyRoots
+    createIvyCurves(context, IVY) #create Curverepresentation of the Ivy
 
-    createIvy(context, IVY) # create emptis at IvyRoots
 
-
     return
 
-class ivy_test(bpy.types.Operator):
+class ivy_test2(bpy.types.Operator):
     ''''''
-    bl_idname = "ivy_test"
+    bl_idname = "ivy_test2"
     bl_label = "IVY_test"
     bl_options = {'REGISTER', 'UNDO'}
 
@@ -154,7 +228,7 @@
                             default=0)
     ivyIter = IntProperty(name='Iterations',
                             min=0, soft_min=0,
-                            default=5)
+                            default=25)
     ivyGravityWeigth = FloatProperty(name='Gravity Weight',
                             min=0,soft_min=0,
                             max=1,soft_max=1,
@@ -192,18 +266,18 @@
 
 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list