home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / share / k3d / scripts / scripted_plugins / ogre_exporter.py < prev    next >
Encoding:
Python Source  |  2008-02-23  |  2.5 KB  |  78 lines

  1. #python
  2.  
  3. # k3d:plugin-class="document"
  4. # k3d:plugin-type="NullOutputScript"
  5. # k3d:plugin-name="OGREWriter"
  6. # k3d:plugin-description="Exports a mesh to OGRE XML format"
  7.  
  8. from xml.dom.minidom import getDOMImplementation
  9. import k3d
  10.  
  11. #Add the required user properties
  12. if not Node.has_property("input_mesh"):
  13.     Node.create_property("k3d::mesh*", "input_mesh", "Input Mesh", "The mesh that will have its points counted")
  14. if not Node.has_property("output_file"):
  15.     Node.create_property("k3d::filesystem::path", "output_file", "Output File", "The exportes OGRE mesh XML file")
  16.  
  17. # mesh components
  18. points =  Node.__getattr__("input_mesh").points()
  19. polyhedra = Node.__getattr__("input_mesh").polyhedra()
  20. edge_points = polyhedra.edge_points()
  21. clockwise_edges = polyhedra.clockwise_edges()
  22. face_first_loops = polyhedra.face_first_loops()
  23. loop_first_edges = polyhedra.loop_first_edges()
  24.  
  25. #init xml file
  26. impl = getDOMImplementation()
  27. doc = impl.createDocument(None, "mesh", None)
  28. top_element = doc.documentElement
  29. sharedgeometry = doc.createElement("sharedgeometry")
  30. sharedgeometry.setAttribute("vertexcount", str(len(points)))
  31. vertexbuffer = doc.createElement("vertexbuffer")
  32. vertexbuffer.setAttribute("positions", "true")
  33.  
  34. # append points
  35. for i in range(len(points)):
  36.     vertex = doc.createElement("vertex")
  37.     position = doc.createElement("position")
  38.     position.setAttribute("x", str(points[i][0]))
  39.     position.setAttribute("y", str(points[i][1]))
  40.     position.setAttribute("z", str(points[i][2]))
  41.     vertex.appendChild(position)
  42.     vertexbuffer.appendChild(vertex)
  43.  
  44. # finalize sharedgeometry element
  45. sharedgeometry.appendChild(vertexbuffer)
  46. top_element.appendChild(sharedgeometry)
  47.  
  48. #init submeshes
  49. submeshes = doc.createElement("submeshes")
  50. submesh = doc.createElement("submesh")
  51. faces = doc.createElement("faces")
  52. faces.setAttribute("count", str(len(face_first_loops)))
  53.  
  54. #Check if mesh is triangles-only
  55. if not k3d.is_triangles(Node.__getattr__("input_mesh")):
  56.     raise Exception, "Mesh has non-triangular faces"
  57.  
  58. #Loop over all faces, and add them as triangles
  59. for face in range(len(face_first_loops)):
  60.     first_edge = loop_first_edges[face_first_loops[face]]
  61.     edge = first_edge
  62.     face_element = doc.createElement("face")
  63.     for n in range(1, 4):
  64.         face_element.setAttribute("v" + str(n), str(edge_points[edge]))
  65.         edge = clockwise_edges[edge]
  66.     faces.appendChild(face_element)
  67.  
  68. #append the faces and submesh block to the main document
  69. submesh.appendChild(faces)
  70. submeshes.appendChild(submesh)
  71. top_element.appendChild(submeshes)
  72.  
  73. # write xml file
  74. f = open(str(Node.__getattr__("output_file")), 'w')
  75. doc.writexml(f)
  76. f.close()
  77.  
  78.