Merged changes in the trunk up to revision 42902.
[blender.git] / intern / cycles / app / io_export_cycles_xml.py
1 #
2 # Copyright 2011, Blender Foundation.
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software Foundation,
16 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 #
18
19 # XML exporter for generating test files, not intended for end users
20
21 import os
22 import xml.etree.ElementTree as etree
23 import xml.dom.minidom as dom
24
25 import bpy
26 from bpy_extras.io_utils import ExportHelper
27 from bpy.props import PointerProperty, StringProperty
28
29 def strip(root):
30     root.text = None
31     root.tail = None
32
33     for elem in root:
34         strip(elem)
35
36 def write(node, fname):
37     strip(node)
38
39     s = etree.tostring(node)
40     s = dom.parseString(s).toprettyxml()
41
42     f = open(fname, "w")
43     f.write(s)
44     
45 class CyclesXMLSettings(bpy.types.PropertyGroup):
46     @classmethod
47     def register(cls):
48         bpy.types.Scene.cycles_xml = PointerProperty(
49                                         type=cls,
50                                         name="Cycles XML export Settings",
51                                         description="Cycles XML export settings")
52         cls.filepath = StringProperty(
53                         name='Filepath',
54                         description='Filepath for the .xml file',
55                         maxlen=256,
56                         default='',
57                         subtype='FILE_PATH')
58                         
59     @classmethod
60     def unregister(cls):
61         del bpy.types.Scene.cycles_xml
62         
63 # User Interface Drawing Code
64 class RenderButtonsPanel():
65     bl_space_type = 'PROPERTIES'
66     bl_region_type = 'WINDOW'
67     bl_context = "render"
68
69     @classmethod
70     def poll(self, context):
71         rd = context.scene.render
72         return rd.engine == 'CYCLES'
73
74
75 class PHYSICS_PT_fluid_export(RenderButtonsPanel, bpy.types.Panel):
76     bl_label = "Cycles XML Exporter"
77
78     def draw(self, context):
79         layout = self.layout
80         
81         cycles = context.scene.cycles_xml
82         
83         #layout.prop(cycles, "filepath")
84         layout.operator("export_mesh.cycles_xml")
85
86         
87 # Export Operator
88 class ExportCyclesXML(bpy.types.Operator, ExportHelper):
89     bl_idname = "export_mesh.cycles_xml"
90     bl_label = "Export Cycles XML"
91
92     filename_ext = ".xml"
93
94     @classmethod
95     def poll(cls, context):
96         return context.active_object != None
97
98     def execute(self, context):
99         filepath = bpy.path.ensure_ext(self.filepath, ".xml")
100
101         # get mesh
102         scene = context.scene
103         object = context.active_object
104
105         if not object:
106             raise Exception("No active object")
107
108         mesh = object.to_mesh(scene, True, 'PREVIEW')
109
110         if not mesh:
111             raise Exception("No mesh data in active object")
112
113         # generate mesh node
114         nverts = ""
115         verts = ""
116         P = ""
117
118         for v in mesh.vertices:
119             P += "%f %f %f  " % (v.co[0], v.co[1], v.co[2])
120
121         for i, f in enumerate(mesh.faces):
122             nverts += str(len(f.vertices)) + " "
123
124             for v in f.vertices:
125                 verts += str(v) + " "
126             verts += " "
127
128         node = etree.Element('mesh', attrib={'nverts': nverts, 'verts': verts, 'P': P})
129         
130         # write to file
131         write(node, filepath)
132
133         return {'FINISHED'}
134
135 def register():
136     bpy.utils.register_module(__name__)
137
138 def unregister():
139     bpy.utils.unregister_module(__name__)
140
141 if __name__ == "__main__":
142     register()
143