Auto-export to OBJ format on save I have the following workflow in Blender:


Edit object in .blend format
Save .blend
Set axis options in OBJ exporter
Enable normals export in OBJ exporter
Enable "triangulate" in OBJ exporter
Export .obj (and .mtl) using OBJ exporter
Copy .obj from wherever it got saved to $someDir/mesh/
Copy .mtl from whetever it got saved to $someDir/material/


This is getting pretty tedious, so I'd like to write a Blender script so that when I press save, all of the above steps happen automatically. I couldn't figure out how to attach a script action to the "save" button. Is this possible? 

(Note: I want to keep the native .blend file too)
Here's my answer with most of the script credit going to zeffii from this page. I just added the Save Blend file line at the top.

You can set the output path parent folder with the folder variable. The script creates the child mesh and material folders. The MTL files are moved to the material folder at the end of each OBJ loop.

import bpy
import os
import shutil

bpy.ops.wm.save_mainfile() 


folder = '/path/to/export/folder/'
meshFolder = folder + 'mesh/'
matFolder = folder + 'material/'

if not os.path.exists(folder):
        os.makedirs(folder)
if not os.path.exists(meshFolder):
        os.makedirs(meshFolder)
if not os.path.exists(matFolder):
        os.makedirs(matFolder)        

selected = bpy.context.selected_objects.copy()
bpy.ops.object.select_all(action='DESELECT')

for obj in selected:
    name = obj.name.replace('.', '_')
    obj.select = True
    fullpath = os.path.join(meshFolder, name + '.obj')
    bpy.ops.export_scene.obj(
        filepath=fullpath, 
        check_existing=True, 
        axis_forward='-Z', 
        axis_up='Y', 
        use_selection=True, 
        use_animation=False, 
        use_mesh_modifiers=True, 
        use_edges=True, 
        use_smooth_groups=False, 
        use_smooth_groups_bitflags=False, 
        use_normals=True, 
        use_uvs=True, 
        use_materials=True, 
        use_triangles=False, 
        use_nurbs=False, 
        use_vertex_groups=False, 
        use_blen_objects=True, 
        group_by_object=False, 
        group_by_material=False, 
        keep_vertex_order=False, 
        global_scale=1, 
        path_mode='AUTO')
    obj.select = False
    shutil.move(meshFolder + name + '.mtl', matFolder + name + '.mtl')