ordering a bin

This is not very appealing visually, and visuals mean nothing, but the code is well documented with each of the functions.

from pandac.PandaModules import *
import direct.directbase.DirectStart
from binOrder import binOrder
from random import uniform
#the visual really doesn't mean anything here...
def createSprite():
   vdata = GeomVertexData('', GeomVertexFormat.getV3t2(), Geom.UHStatic) 
   vertex = GeomVertexWriter(vdata, 'vertex') 
   uv = GeomVertexWriter(vdata, 'texcoord') 
   prim = GeomTriangles(Geom.UHStatic) 
    
   vertex.addData3f(0, 0, 1) 
   vertex.addData3f(0, 0, 0) 
   vertex.addData3f(1, 0, 0) 
   vertex.addData3f(1, 0, 1) 
   uv.addData2f(0,0) 
   uv.addData2f(0,1) 
   uv.addData2f(1,1) 
   uv.addData2f(1,0) 
    
   prim.addVertices(3,2,0) 
   prim.addVertices(1,0,2) 
   prim.closePrimitive() 
    
   geom = Geom(vdata) 
   geom.addPrimitive(prim) 
   nodepath = NodePath(GeomNode('gnode')) 
   nodepath.node().addGeom(geom) 
   nodepath.setTransparency(1) 

   return nodepath
  
nodes = []
for i in range(0,13):
	nodes.append(createSprite())
	nodes[-1].setPos(-1+i/7.0,0,0)
	nodes[-1].reparentTo(render2d)
	nodes[-1].setColorScale(uniform(0,1),uniform(0,1),uniform(0,1),1)
  
bo = binOrder()

#simple node operations
bo.insertInFront(None,nodes[0])	#nodes[0] is now in front of everything
bo.insertInBack(nodes[0],nodes[1])	#nodes[1] is now behind nodes[0]
bo.insertInFront(nodes[1],nodes[2])	#nodes[2] is now in between nodes[0] and nodes[1]
bo.moveInFront(nodes[0],nodes[2])	#now nodes[2] is in front of nodes[0] which is in front of nodes[1]
bo.moveInBack(nodes[1],nodes[0])	#now the order is nodes[2]->nodes[1]->nodes[0]
bo.insertInBack(nodes[1],nodes[3])	#order is 2>1>3>0
bo.bringToFront(nodes[0])			#0>2>1>3


#groups
group1 = bo.createGroup('group1name')	#creates a group called group1name...note that the variable group1 simply holds a string called group1name	
group2 = bo.createGroup('group2name',nodes[4:10])	#creates a group with nodes 4-10 inside with 10 rendered in front and 4 rendered in back

#groups are not in the list of render order yet, just created.
#we can add nodes to groups, and even groups to groups using the normal node commands:
bo.addToGroup(group1,nodes[11])	#group1 now contains 11
bo.insertInFront(nodes[11],nodes[12])	#group1 now contatins 12>11
bo.insertInBack(nodes[12],group2)	#group1 now contains 12>group2>11
										#also 12>10>9>8>7>6>5>4>11
										
#we can insert group1 into the actual list with the same commands:
bo.insertInFront(nodes[2],group1)	#total order is 0>group1>2>1>3
										#or 0>12>group2>11>2>1>3
										#or 0>12>10>9>8>7>6>5>4>2>1>3

										
#we can move things into/out of groups easy:
bo.moveInFront(nodes[4],nodes[0])	#now group2 is 10>9>8>7>6>5>0>4
									#the total is group1>2>1>3

#if you want to remove a node:
bo.remove(nodes[1])			#now the order is group1>2>3

#we can remove a group, but it keeps its data intact so we can reinsert it at a later time:
bo.remove(group2)			#now group1 is 12>11
#and insert again
bo.insertInBack(nodes[2],group2)	#now the total is group1>2>group2>3

#note that the two above commands are the same as
#bo.moveInBack(nodes[2],group2)

#to remove a group completely:
bo.removeGroup(group1)	#gone...Gone...GONE!

#bo.insertInback(nodes[3],group1)	#would error, group1 doesn't exist anymore.  anything inside of group1 was lost
 
 
run()