diff options
author | Chris Foster <cdbfoster@gmail.com> | 2013-12-12 00:54:59 +0400 |
---|---|---|
committer | Chris Foster <cdbfoster@gmail.com> | 2013-12-12 00:54:59 +0400 |
commit | 4b9e07014b9734df30f90301d58cad5e4cfc6c70 (patch) | |
tree | 471ffe51a0a0894261179320303044ade788b273 /io_scene_x | |
parent | c33c2c0b7a28787123f4b9e7354549a18e8b88b4 (diff) |
Add option to export texture references to the active images of each face. This is useful to some users.
Diffstat (limited to 'io_scene_x')
-rw-r--r-- | io_scene_x/__init__.py | 9 | ||||
-rw-r--r-- | io_scene_x/export_x.py | 87 |
2 files changed, 93 insertions, 3 deletions
diff --git a/io_scene_x/__init__.py b/io_scene_x/__init__.py index 394954e6..91c9ac36 100644 --- a/io_scene_x/__init__.py +++ b/io_scene_x/__init__.py @@ -91,7 +91,13 @@ class ExportDirectX(bpy.types.Operator): name=" Export Materials", description="Export material properties and reference image textures", default=True) - + + ExportActiveImageMaterials = BoolProperty( + name=" Reference Active Images as Textures", + description="Reference the active image of each face as a texture, "\ + "as opposed to the image assigned to the material", + default=False) + ExportVertexColors = BoolProperty( name=" Export Vertex Colors", description="Export mesh vertex colors, if any", @@ -182,4 +188,3 @@ def unregister(): if __name__ == "__main__": register() - diff --git a/io_scene_x/export_x.py b/io_scene_x/export_x.py index dcaf2206..8c595fe1 100644 --- a/io_scene_x/export_x.py +++ b/io_scene_x/export_x.py @@ -506,7 +506,12 @@ class MeshExportObject(ExportObject): if self.Config.ExportMaterials: self.Exporter.Log("Writing mesh materials...") - self.__WriteMeshMaterials(Mesh) + if self.Config.ExportActiveImageMaterials: + self.Exporter.Log("Referencing active images instead of "\ + "material image textures.") + self.__WriteMeshActiveImageMaterials(Mesh) + else: + self.__WriteMeshMaterials(Mesh) self.Exporter.Log("Done") if self.Config.ExportVertexColors: @@ -701,6 +706,86 @@ class MeshExportObject(ExportObject): self.Exporter.File.Write("}} // End of {} material list\n".format( self.SafeName)) + def __WriteMeshActiveImageMaterials(self, Mesh): + def WriteMaterial(Exporter, MaterialKey): + #Unpack key + Material, Image = MaterialKey + + Exporter.File.Write("Material {} {{\n".format( + Util.SafeName(Material.name))) + Exporter.File.Indent() + + Diffuse = list(Vector(Material.diffuse_color) * + Material.diffuse_intensity) + Diffuse.append(Material.alpha) + # Map Blender's range of 1 - 511 to 0 - 1000 + Specularity = 1000 * (Material.specular_hardness - 1.0) / 510.0 + Specular = list(Vector(Material.specular_color) * + Material.specular_intensity) + + Exporter.File.Write("{:9f};{:9f};{:9f};{:9f};;\n".format(Diffuse[0], + Diffuse[1], Diffuse[2], Diffuse[3])) + Exporter.File.Write(" {:9f};\n".format(Specularity)) + Exporter.File.Write("{:9f};{:9f};{:9f};;\n".format(Specular[0], + Specular[1], Specular[2])) + Exporter.File.Write(" 0.000000; 0.000000; 0.000000;;\n") + + if Image is not None: + Exporter.File.Write("TextureFilename {{\"{}\";}}\n".format( + bpy.path.basename(Image.filepath))) + + self.Exporter.File.Unindent() + self.Exporter.File.Write("}\n") + + def GetMaterialKey(Material, UVTexture, Index): + Image = None + if UVTexture != None and UVTexture.data[Index].image != None: + Image = UVTexture.data[Index].image if \ + UVTexture.data[Index].image.source == 'FILE' else None + + return (Material, Image) + + Materials = Mesh.materials + # Do not write materials if there are none + if not Materials.keys(): + return + + self.Exporter.File.Write("MeshMaterialList {{ // {} material list\n". + format(self.SafeName)) + self.Exporter.File.Indent() + + from array import array + MaterialIndexes = array("I", [0]) * len(Mesh.polygons) # Fast allocate + MaterialIndexMap = {} + + for Index, Polygon in enumerate(Mesh.polygons): + MaterialKey = GetMaterialKey(Materials[Polygon.material_index], + Mesh.uv_textures.active, Index) + + if MaterialKey in MaterialIndexMap: + MaterialIndexes[Index] = MaterialIndexMap[MaterialKey] + else: + MaterialIndex = len(MaterialIndexMap) + MaterialIndexMap[MaterialKey] = MaterialIndex + MaterialIndexes[Index] = MaterialIndex + + self.Exporter.File.Write("{};\n".format(len(MaterialIndexMap))) + self.Exporter.File.Write("{};\n".format(len(Mesh.polygons))) + + for Index in range(len(Mesh.polygons)): + self.Exporter.File.Write("{}".format(MaterialIndexes[Index])) + if Index == len(Mesh.polygons) - 1: + self.Exporter.File.Write(";;\n", Indent=False) + else: + self.Exporter.File.Write(",\n", Indent=False) + + for Material in MaterialIndexMap.keys(): + WriteMaterial(self.Exporter, Material) + + self.Exporter.File.Unindent() + self.Exporter.File.Write("}} // End of {} material list\n".format( + self.SafeName)) + def __WriteMeshVertexColors(self, Mesh, MeshEnumerator=None): # If there are no vertex colors, don't write anything if len(Mesh.vertex_colors) == 0: |