Workspace: Move engines to workspace and Properties Editor cleanup
[blender.git] / intern / cycles / app / io_export_cycles_xml.py
1 #
2 # Copyright 2011-2013 Blender Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16
17 # XML exporter for generating test files, not intended for end users
18
19 import os
20 import xml.etree.ElementTree as etree
21 import xml.dom.minidom as dom
22
23 import bpy
24 from bpy_extras.io_utils import ExportHelper
25 from bpy.props import PointerProperty, StringProperty
26
27 def strip(root):
28     root.text = None
29     root.tail = None
30
31     for elem in root:
32         strip(elem)
33
34 def write(node, fname):
35     strip(node)
36
37     s = etree.tostring(node)
38     s = dom.parseString(s).toprettyxml()
39
40     f = open(fname, "w")
41     f.write(s)
42     
43 class CyclesXMLSettings(bpy.types.PropertyGroup):
44     @classmethod
45     def register(cls):
46         bpy.types.Scene.cycles_xml = PointerProperty(
47                                         type=cls,
48                                         name="Cycles XML export Settings",
49                                         description="Cycles XML export settings")
50         cls.filepath = StringProperty(
51                         name='Filepath',
52                         description='Filepath for the .xml file',
53                         maxlen=256,
54                         default='',
55                         subtype='FILE_PATH')
56                         
57     @classmethod
58     def unregister(cls):
59         del bpy.types.Scene.cycles_xml
60         
61 # User Interface Drawing Code
62 class RenderButtonsPanel():
63     bl_space_type = 'PROPERTIES'
64     bl_region_type = 'WINDOW'
65     bl_context = "render"
66
67     @classmethod
68     def poll(self, context):
69         return context.engine == 'CYCLES'
70
71
72 class PHYSICS_PT_fluid_export(RenderButtonsPanel, bpy.types.Panel):
73     bl_label = "Cycles XML Exporter"
74
75     def draw(self, context):
76         layout = self.layout
77         
78         cycles = context.scene.cycles_xml
79         
80         #layout.prop(cycles, "filepath")
81         layout.operator("export_mesh.cycles_xml")
82
83         
84 # Export Operator
85 class ExportCyclesXML(bpy.types.Operator, ExportHelper):
86     bl_idname = "export_mesh.cycles_xml"
87     bl_label = "Export Cycles XML"
88
89     filename_ext = ".xml"
90
91     @classmethod
92     def poll(cls, context):
93         return (context.active_object is not None)
94
95     def execute(self, context):
96         filepath = bpy.path.ensure_ext(self.filepath, ".xml")
97
98         # get mesh
99         scene = context.scene
100         object = context.active_object
101
102         if not object:
103             raise Exception("No active object")
104
105         mesh = object.to_mesh(scene, True, 'PREVIEW')
106
107         if not mesh:
108             raise Exception("No mesh data in active object")
109
110         # generate mesh node
111         nverts = ""
112         verts = ""
113         uvs = ""
114         P = ""
115
116         for v in mesh.vertices:
117             P += "%f %f %f  " % (v.co[0], v.co[1], v.co[2])
118
119         verts_and_uvs = zip(mesh.tessfaces, mesh.tessface_uv_textures.active.data)
120         
121         for f, uvf in verts_and_uvs:
122             vcount = len(f.vertices)
123             nverts += str(vcount) + " "
124
125             for v in f.vertices:
126                 verts += str(v) + " "
127             
128             uvs += str(uvf.uv1[0]) + " " + str(uvf.uv1[1]) + " "
129             uvs += str(uvf.uv2[0]) + " " + str(uvf.uv2[1]) + " "
130             uvs += str(uvf.uv3[0]) + " " + str(uvf.uv3[1]) + " "
131             if vcount==4:
132                 uvs += " " + str(uvf.uv4[0]) + " " + str(uvf.uv4[1]) + " "
133             
134
135         node = etree.Element('mesh', attrib={'nverts': nverts.strip(), 'verts': verts.strip(), 'P': P, 'UV' : uvs.strip()})
136         
137         # write to file
138         write(node, filepath)
139
140         return {'FINISHED'}
141
142 def register():
143     bpy.utils.register_module(__name__)
144
145 def unregister():
146     bpy.utils.unregister_module(__name__)
147
148 if __name__ == "__main__":
149     register()
150
151