Skip to content

Instantly share code, notes, and snippets.

@1f0
Created April 15, 2018 14:44
Show Gist options
  • Save 1f0/9413044072e51439907ed57728c7bb97 to your computer and use it in GitHub Desktop.
Save 1f0/9413044072e51439907ed57728c7bb97 to your computer and use it in GitHub Desktop.
Blender addons for exporting material
'''
# blender-mtl-exporter
This addons can export all custom properties of all materials in the *.blend file
# Usage
Just open the "File > Export > Material (.mtl)"
'''
import bpy
from bpy_extras.io_utils import ExportHelper
bl_info = {
"name": "My Material Exporter",
"category": "Import-Export",
"location": "File > Export",
"author": "Ghorte",
"description": "Export all materials in this .blend file."
}
class ExportMtl(bpy.types.Operator, ExportHelper):
bl_idname = "export.mtl"
bl_label = "Export Material"
filename_ext = ".mtl"
def invoke(self, context, event):
return ExportHelper.invoke(self, context, event)
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
if not self.properties.filepath:
raise Exception("filename not set")
write_out = \
"""\
# Blender Material Custom Properties '%s'
# Material Count: %d
""" % (bpy.data.filepath, len(bpy.data.materials))
for mat in bpy.data.materials:
write_out = write_out + "newmtl %s\n" % mat.name
for k in mat.keys():
if (k.startswith("_")):
continue
write_out = write_out + "%s %s\n" % (k, str(mat.get(k)))
write_out = write_out + "\n"
with open(self.filepath, mode="w") as stream:
stream.write(write_out)
return {"FINISHED"}
def menu_func_export(self, context):
default_path = bpy.data.filepath.replace(".blend", ".mtl")
text = "Material (.mtl)"
operator = self.layout.operator(ExportMtl.bl_idname, text=text)
operator.filepath = default_path
def register():
bpy.utils.register_module(__name__)
bpy.types.INFO_MT_file_export.append(menu_func_export)
def unregister():
bpy.utils.unregister_module(__name__)
bpy.types.INFO_MT_file_export.remove(menu_func_export)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment