views:

70

answers:

1

hello, i have a problem with breadth first algorithm, my script generates curves in maya, position them, rotate and scale them so they give me the tree shape, i have these variables

cs=current State,

p=parent,

nodes=non visited node List

lvl=current depth

maxlvl= max depth

the problem is that i cant determine current depth, and terminate the tree there, it exists without all nodes being visited

here is my script

import random
import math
import maya.cmds as mc
#----define set-----
#--- curve, #children,angle,l--------
#----define rule----
#    l=80%   ch<=3   angle<=45
l=0.8
ch=6
ang=50
#---list to track----
nodes=[]
#--- begin-----
#---start curve-----
nodes.append(mc.curve(n="a",d=3,p=((0,0,0),(0,1,0),(0,3,0),(0,5,0),(0,6,0)),k=(0,0,0,1,2,2,2)))
cs="" #---current state----
p="a" #---paretn of the current state----
maxlvl=6 #--max depth for the tree
lvl=1 #-- cuurent level----
while (len(nodes)!=0 and lvl<5):
 #---generate children number----
 chN=random.randint(0,ch)
 for j in range(chN):
  #----generate node----
  mc.select(p,r=1)
  cs=mc.duplicate(rr=1)[0]
  #---append to node lst----
  nodes.append(cs)
  #---adjust pos---
  pos=mc.pointPosition(p+".cv[4]")
  mc.xform(cs,t=(pos[0],pos[1],pos[2]),ws=1)
  #---adjust rotation---
  mc.rotate(0,0,(random.random()*45+5)*math.pow(-1,j%2),cs,r=1)
  #---adjust scale---
  mc.scale(0,math.pow(0.8,lvl),0,cs)
  j+=1
 #--- go to next parent---
 nodes.pop(0)
 if nodes:
  p=nodes[0]
 lvl+=1

thank you in advance

+3  A: 

You need to associate the depth with each node. Either put it as a member in the node class, or have your queue entries store both depth and node like this:

nodes.append((1, firstNode))
while nodes and lvl<5:
    lvl, p = nodes.pop(0)

    For each child:
        #...create child
        nodes.append((lvl+1, cs))

Some unrelated notes:

  • Use collections.deque instead of list: It's faster for FIFO queues.

  • The j+=1 statement is unnecessary in the for loop.

interjay
thank you.. that was helpful :D +1
Ahmad Dwaik