Blender V2.61 - r43446

io_export_cycles_xml.py

Go to the documentation of this file.
00001 #
00002 # Copyright 2011, Blender Foundation.
00003 #
00004 # This program is free software; you can redistribute it and/or
00005 # modify it under the terms of the GNU General Public License
00006 # as published by the Free Software Foundation; either version 2
00007 # of the License, or (at your option) any later version.
00008 #
00009 # This program is distributed in the hope that it will be useful,
00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 # GNU General Public License for more details.
00013 #
00014 # You should have received a copy of the GNU General Public License
00015 # along with this program; if not, write to the Free Software Foundation,
00016 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017 #
00018 
00019 # XML exporter for generating test files, not intended for end users
00020 
00021 import os
00022 import xml.etree.ElementTree as etree
00023 import xml.dom.minidom as dom
00024 
00025 import bpy
00026 from bpy_extras.io_utils import ExportHelper
00027 from bpy.props import PointerProperty, StringProperty
00028 
00029 def strip(root):
00030     root.text = None
00031     root.tail = None
00032 
00033     for elem in root:
00034         strip(elem)
00035 
00036 def write(node, fname):
00037     strip(node)
00038 
00039     s = etree.tostring(node)
00040     s = dom.parseString(s).toprettyxml()
00041 
00042     f = open(fname, "w")
00043     f.write(s)
00044     
00045 class CyclesXMLSettings(bpy.types.PropertyGroup):
00046     @classmethod
00047     def register(cls):
00048         bpy.types.Scene.cycles_xml = PointerProperty(
00049                                         type=cls,
00050                                         name="Cycles XML export Settings",
00051                                         description="Cycles XML export settings")
00052         cls.filepath = StringProperty(
00053                         name='Filepath',
00054                         description='Filepath for the .xml file',
00055                         maxlen=256,
00056                         default='',
00057                         subtype='FILE_PATH')
00058                         
00059     @classmethod
00060     def unregister(cls):
00061         del bpy.types.Scene.cycles_xml
00062         
00063 # User Interface Drawing Code
00064 class RenderButtonsPanel():
00065     bl_space_type = 'PROPERTIES'
00066     bl_region_type = 'WINDOW'
00067     bl_context = "render"
00068 
00069     @classmethod
00070     def poll(self, context):
00071         rd = context.scene.render
00072         return rd.engine == 'CYCLES'
00073 
00074 
00075 class PHYSICS_PT_fluid_export(RenderButtonsPanel, bpy.types.Panel):
00076     bl_label = "Cycles XML Exporter"
00077 
00078     def draw(self, context):
00079         layout = self.layout
00080         
00081         cycles = context.scene.cycles_xml
00082         
00083         #layout.prop(cycles, "filepath")
00084         layout.operator("export_mesh.cycles_xml")
00085 
00086         
00087 # Export Operator
00088 class ExportCyclesXML(bpy.types.Operator, ExportHelper):
00089     bl_idname = "export_mesh.cycles_xml"
00090     bl_label = "Export Cycles XML"
00091 
00092     filename_ext = ".xml"
00093 
00094     @classmethod
00095     def poll(cls, context):
00096         return context.active_object != None
00097 
00098     def execute(self, context):
00099         filepath = bpy.path.ensure_ext(self.filepath, ".xml")
00100 
00101         # get mesh
00102         scene = context.scene
00103         object = context.active_object
00104 
00105         if not object:
00106             raise Exception("No active object")
00107 
00108         mesh = object.to_mesh(scene, True, 'PREVIEW')
00109 
00110         if not mesh:
00111             raise Exception("No mesh data in active object")
00112 
00113         # generate mesh node
00114         nverts = ""
00115         verts = ""
00116         P = ""
00117 
00118         for v in mesh.vertices:
00119             P += "%f %f %f  " % (v.co[0], v.co[1], v.co[2])
00120 
00121         for i, f in enumerate(mesh.faces):
00122             nverts += str(len(f.vertices)) + " "
00123 
00124             for v in f.vertices:
00125                 verts += str(v) + " "
00126             verts += " "
00127 
00128         node = etree.Element('mesh', attrib={'nverts': nverts, 'verts': verts, 'P': P})
00129         
00130         # write to file
00131         write(node, filepath)
00132 
00133         return {'FINISHED'}
00134 
00135 def register():
00136     bpy.utils.register_module(__name__)
00137 
00138 def unregister():
00139     bpy.utils.unregister_module(__name__)
00140 
00141 if __name__ == "__main__":
00142     register()
00143