Basically, the vertexes will be the nodes and the edges will define valid paths. Start by making a path, you can make it any shape you want. Here is an example:
Split the view and open the text editor. Make a new file and name it pathfinder.py. I recomend turning on these options:
First we import Blender and declare a global variable nodelist and set it to None.
- Code: Select all
import Blender
nodelist = None
We will start be making a class called Node. This will hold all the important information about a given vertex.
- Code: Select all
class Node:
name = ''
co = [0,0,0]
g_score = 0
h_score = 0
f_score = 0
parent = None
children = []
The variable name is just a string we will use for printing out the nodes and co is the coordinates of the node. I will explain the others later. Now let's define a function called gen_nodelist that takes an arguemnt ob_name.
- Code: Select all
def gen_nodelist(ob_name):
What this function will be doing is getting an objects mesh, looping through it's vertexes appending Node instances to a list, then making connections between the Nodes using the mesh's edge data. Here is the code:
- Code: Select all
# local list that will be returned
nodelist = []
# used for naming
x = 1
# the objects mesh
mesh = Blender.Object.Get(ob_name).getData(mesh=1)
# add Nodes to the nodelist using the mesh's verticies
# nodes will be named NodeX
for vert in mesh.verts:
# make a new Node and initialize it's members
node = Node()
node.name = 'Node'+str(x)
node.co = vert.co
node.g_score = 0
node.h_score = 0
node.f_score = 0
node.parent = None
node.children = []
# add the node to the list and increment the counter
nodelist.append(node)
x += 1
# use edge data to create links between nodes (children)
for edge in mesh.edges:
for node in nodelist:
# if this node is the first vert on the edge
if node.co == edge.v1.co and edge.v2.co != node.co:
# add all Nodes that are connected to this vert to the child list
for n in nodelist:
if n.co == edge.v2.co:
node.children.append(n)
# if this node is the second vert on the edge
elif node.co == edge.v2.co and edge.v1.co != node.co:
# add all Nodes that are connected to this vert to the child list
for n in nodelist:
if n.co == edge.v1.co:
node.children.append(n)
# return the nodelist
return nodelist
Here we are using the children member of our Node class to hold all the Nodes that are connected to it. It is a good idea to print all this data out to verify that the code is working correctly. Add a new varibale called DEBUG to the function (make it 1) and right before the return statment add this code:
- Code: Select all
if DEBUG:
file = open('DEBUG.txt','w')
for node in nodelist:
nodes = []
for n in node.children:
nodes.append(n.name)
file.write(node.name+' at '+str(node.co)+ ' : '+str(nodes)+'\n')
This will make a file namde DEBUG.txt in the same directory as the .blend file. We still have a little bit more to add before we can run this. Make a new file namde core.py. This script will be our main script. Its contents are simple right now:
- Code: Select all
import pathfinder
def init():
pathfinder.gen_nodelist('path')
Replace 'path' with the name of your path object. Add an empty to the scene and set it up like so:
You can now press p to play the game! Nothing special will happen visably, but you should get a text file with all the path data. You should verifiy that it is correct before moving on. The next part will be creating our A* (a-star) algorithm.
Here is the .blend.




