io_export_cycles_xml.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. # XML exporter for generating test files, not intended for end users
  17. import xml.etree.ElementTree as etree
  18. import xml.dom.minidom as dom
  19. import bpy
  20. from bpy_extras.io_utils import ExportHelper
  21. from bpy.props import PointerProperty, StringProperty
  22. def strip(root):
  23. root.text = None
  24. root.tail = None
  25. for elem in root:
  26. strip(elem)
  27. def write(node, fname):
  28. strip(node)
  29. s = etree.tostring(node)
  30. s = dom.parseString(s).toprettyxml()
  31. f = open(fname, "w")
  32. f.write(s)
  33. class CyclesXMLSettings(bpy.types.PropertyGroup):
  34. @classmethod
  35. def register(cls):
  36. bpy.types.Scene.cycles_xml = PointerProperty(
  37. type=cls,
  38. name="Cycles XML export Settings",
  39. description="Cycles XML export settings")
  40. cls.filepath = StringProperty(
  41. name='Filepath',
  42. description='Filepath for the .xml file',
  43. maxlen=256,
  44. default='',
  45. subtype='FILE_PATH')
  46. @classmethod
  47. def unregister(cls):
  48. del bpy.types.Scene.cycles_xml
  49. # User Interface Drawing Code
  50. class RenderButtonsPanel():
  51. bl_space_type = 'PROPERTIES'
  52. bl_region_type = 'WINDOW'
  53. bl_context = "render"
  54. @classmethod
  55. def poll(cls, context):
  56. return context.engine == 'CYCLES'
  57. class PHYSICS_PT_fluid_export(RenderButtonsPanel, bpy.types.Panel):
  58. bl_label = "Cycles XML Exporter"
  59. def draw(self, context):
  60. layout = self.layout
  61. cycles = context.scene.cycles_xml
  62. #layout.prop(cycles, "filepath")
  63. layout.operator("export_mesh.cycles_xml")
  64. # Export Operator
  65. class ExportCyclesXML(bpy.types.Operator, ExportHelper):
  66. bl_idname = "export_mesh.cycles_xml"
  67. bl_label = "Export Cycles XML"
  68. filename_ext = ".xml"
  69. @classmethod
  70. def poll(cls, context):
  71. return (context.active_object is not None)
  72. def execute(self, context):
  73. filepath = bpy.path.ensure_ext(self.filepath, ".xml")
  74. # get mesh
  75. scene = context.scene
  76. object = context.active_object
  77. if not object:
  78. raise Exception("No active object")
  79. mesh = object.to_mesh(scene, True, 'PREVIEW')
  80. if not mesh:
  81. raise Exception("No mesh data in active object")
  82. # generate mesh node
  83. nverts = ""
  84. verts = ""
  85. uvs = ""
  86. P = ""
  87. for v in mesh.vertices:
  88. P += "%f %f %f " % (v.co[0], v.co[1], v.co[2])
  89. verts_and_uvs = zip(mesh.tessfaces, mesh.tessface_uv_textures.active.data)
  90. for f, uvf in verts_and_uvs:
  91. vcount = len(f.vertices)
  92. nverts += str(vcount) + " "
  93. for v in f.vertices:
  94. verts += str(v) + " "
  95. uvs += str(uvf.uv1[0]) + " " + str(uvf.uv1[1]) + " "
  96. uvs += str(uvf.uv2[0]) + " " + str(uvf.uv2[1]) + " "
  97. uvs += str(uvf.uv3[0]) + " " + str(uvf.uv3[1]) + " "
  98. if vcount==4:
  99. uvs += " " + str(uvf.uv4[0]) + " " + str(uvf.uv4[1]) + " "
  100. node = etree.Element('mesh', attrib={'nverts': nverts.strip(), 'verts': verts.strip(), 'P': P, 'UV' : uvs.strip()})
  101. # write to file
  102. write(node, filepath)
  103. return {'FINISHED'}
  104. def register():
  105. bpy.utils.register_module(__name__)
  106. def unregister():
  107. bpy.utils.unregister_module(__name__)
  108. if __name__ == "__main__":
  109. register()