# PyDia behavior tree json # Copyright (c) 2012, Haramanai # # Simple exporter of connected rectangles in json format # # Known Issues: # # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # Explenation #It 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 your json in the rectangle #I will present the rectangles like this # /--------/ # / / # /------/ #Write what you wand in the rectangle like this # /----------------------/ # / type: 'Wait', / # / duration: 10 / # /----------------------/ #Use type to identify the Task. But in general you can write anything #then pass all the properties that you 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. import sys, dia # sys.path.insert(0, 'd:/graph/dia/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 demand dia.register_export ("Behaviour Tree Json", "json", BTJSON())