Hi.I created a little python plug in for dia that will export the drawn 
behavior tree in a json format...Note that this is created for game ai. But in 
the end you get a powerful json file. So you may wand to use it for some thing 
else
here it is
#START
import sys, dia
class BTJSON :  def __init__ (self) :           pass    def begin_render (self, 
data, filename) :               self.f = open(filename, "w")            
self.f.write ('{')              o = data.layers[0].objects[0]           
self.f.write(o.properties['text'].value.text.replace("\n", " "))                
self.write_children(o)          self.f.write ('}')      def end_render (self) : 
        self.f.close()          def write_children (self, o) :          
children = []           left = []               have_children = 0               
got_children = 0                children_count = 0              
connections_count = 0           for c in o.connections :                        
if (len(c.connected) < 1) :                             continue                
        for co in c.connected :                         if 
(co.handles[0].connected_to.object == o) :                                   
children_count += 1                                     left.append 
(co.handles[1].connected_to.object.bounding_box.left)                           
            
children.append(co.handles[1].connected_to.object.bounding_box.left)            
                left.sort()             for c in o.connections :                
        if (len (c.connected) < 1) :                            continue        
                for co in c.connected :                         if 
(co.handles[0].connected_to.object == o) :                                   
for i in range(len(left)) :                                             if 
(left[i] == co.handles[1].connected_to.object.bounding_box.left) :              
                                     children[i] = 
co.handles[1].connected_to.object                                               
                                  if (len (children) > 0) :                     
  self.f.write (',children: [')                                                 
  for i in range (len(children)) :                        self.f.write ('{')    
                  
self.f.write(children[i].properties['text'].value.text.replace("\n", " "))      
                self.write_children (children[i])                       if (i < 
len(children)-1) :                              self.f.write ('},')             
        else :                          self.f.write ('}]')                     
                                                                                
return children# dia-python keeps a reference to the renderer class and uses it 
on demanddia.register_export ("Behaviour Tree Json", "json", BTJSON())
#END
I hope that spaces will remain in the email format....AnywayIt export some 
thing like this :
{type: 'Selector',children: [{type: 'Limit',duration: 10,children: [{type: 
'MoveTo',position: [0.0,0.0]}]},{type: 'Parallel',children: [{type: 
'Wait',duration: 15,children: [{type: 'Fatality',gore: true}]},{type: 
'Action1'},{type: 'action2'}]},{type: 'Action',duration: 5}]}
This was exported from a simple diagram that looks like a behavior tree. As you 
can see from the source the script starts from the first object of the first 
layer and solves in a way the tree.It writes the text of the rectangle and 
checks for children and creates an array with the childrens.childrens are other 
connected rectangles with text. Then childrens will write their text and will 
check for childrens and ...
So you write the text-code that you want in you json in the rectangle I will 
present the rectangles like this /-------//        //------/                    
                                                        
I write what I wand in the rectangle like this          
/----------------------/         /     type: 'Wait',     /        /    
duration: 10      /       /----------------------/
I use type to identify the Task. But in general you can write anythingthen I 
pass all the properties I wand in a json style.So a Behavior tree will look 
like this.                          /--------------------/                      
    / type: 'Selector', /                          /-------------------/        
                   |                |              /---------------/         
/-----------------/              / type: 'Limit'  /         /  type: 'Reload' / 
            / duration: 5  /           /----------------/             
/---------------/                            |           
/---------------------/            / type: 'Shoot',     /            
/-------------------/
And you will get this :{type: 'Selector',children: [{type: 'Limit',duration: 
5,children: [{type: 'Shoot'}]},{type: 'Reload'}]}
Limitations: I haven't tested very much but the limitations are.1. It will 
start from the first object of the first layer. So Don't ever delete the first 
object just change the text to suits your needs.2. You can only export vertical 
trees. Like the example. Because the script checks the left bounding box edge 
of the rectangles to set the children order.
That's all. It is just a script that I created to use, and I don't mind to 
share.                                         
_______________________________________________
dia-list mailing list
dia-list@gnome.org
http://mail.gnome.org/mailman/listinfo/dia-list
FAQ at http://live.gnome.org/Dia/Faq
Main page at http://live.gnome.org/Dia

Reply via email to