Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import xml.etree.ElementTree as tree
  2.  
  3. tree=tree.parse("depth.svg")#basic level modeling done in inkscape.
  4. pygameTemplate="template.py"#basic pygame file that will be edited by this script.
  5. savefile="test.py"#final pygame playable file
  6. root=tree.getroot()#required for xml parsing
  7.  
  8. roomheight=640 #int(root.attrib["height"])
  9. roomwidht=480 #int(root.attrib["width"])
  10.  
  11. #makes the bottom most object be a refernce point for where true zero is, becaus Inkscape likes to shift where 0,0 is.
  12. y1=round(float(root[3][0].attrib["y"]),6)
  13. offby=y1-roomheight
  14.  
  15. #convert colors from svg to pygame
  16. def HTMLColorToRGB(colorstring):
  17.     """ convert #RRGGBB to an (R, G, B) tuple """
  18.     colorstring = colorstring.strip()
  19.     if colorstring[0] == '#': colorstring = colorstring[1:]
  20.     if len(colorstring) != 6:
  21.         raise ValueError, "input #%s is not in #RRGGBB format" % colorstring
  22.     r, g, b = colorstring[:2], colorstring[2:4], colorstring[4:]
  23.     r, g, b = [int(n, 16) for n in (r, g, b)]
  24.     return (r, g, b)
  25.  
  26. # make a bunch of lines that can appear in the terminal when converting.
  27. line="-"*30+"\n"
  28.  
  29. print line
  30. print str("COUNT OBJECTS IN FILE:"+str(len(root[3]))).center(30)
  31. print line
  32. print str("COUNT OBJECTS IN FILE:"+str(len(root[3]))).center(30)
  33.  
  34. #Open a basic pygame file to edit.
  35. p=open(pygameTemplate)
  36. template=p.read()
  37. p.close()
  38.  
  39. p=open("test.py","w")
  40.  
  41. boxes=str()
  42. images=str()
  43.  
  44. job=range(len(root[3]))#make a list ranging from zero to the number of objects
  45.  
  46. for i in job:
  47.  
  48.     x=round(float(root[3][i].attrib["x"]),6)
  49.     y=round(float(root[3][i].attrib["y"])-offby-roomheight,6)
  50.     w=round(float(root[3][i].attrib["width"]),6)
  51.     h=round(float(root[3][i].attrib["height"]),6)
  52.     objID=str(root[3][i].attrib["id"])
  53.  
  54.     if "style" in root[3][i].attrib:
  55.         color=HTMLColorToRGB(root[3][i].attrib["style"].split(":")[1].split(";")[0])
  56.     if "image" in str(root[3][i]):
  57.         href=root[3][i].attrib["{http://www.w3.org/1999/xlink}href"][7:]
  58.         newimage='{name}=pygame.image.load("{location}")\n'
  59.         images+=newimage.format(name=objID,location=href)
  60.  
  61.     if "image" in str(root[3][i]):
  62.         newbox="screen.blit({name},{position})\n\t"
  63.         boxes+=newbox.format( name=objID, position=str([x,y]) )
  64.  
  65.     if "rect" in str(root[3][i]):  
  66.         newbox="pygame.draw.rect(screen, {color},{boxinfo})\n\t"
  67.         boxes+=newbox.format( color=color, boxinfo=str([x,y,w,h]))
  68.  
  69.     i+=1
  70.  
  71. final=template.format( loadboxes=boxes, loadimages=images )
  72.  
  73. p.write(final)
  74. #print final