0
$\begingroup$

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)

$\endgroup$
  • $\begingroup$ You're better off running a script that saves the blend from inside the script. That would be easier than attaching a script to the save button. $\endgroup$ – Todd McIntosh Jul 18 '15 at 18:23
  • $\begingroup$ You can find an OBJ export script on this SE answer: blender.stackexchange.com/questions/31528/… $\endgroup$ – Todd McIntosh Jul 18 '15 at 18:23
2
$\begingroup$

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')
$\endgroup$
  • $\begingroup$ you can add the copy command at the end to separate the mat and mesh, see this stackoverflow.com/a/123212/2987234 $\endgroup$ – Chebhou Jul 18 '15 at 18:59
  • $\begingroup$ Yes, it depends how you want the OBJ and MTL files organized following export. $\endgroup$ – Todd McIntosh Jul 18 '15 at 19:03
  • $\begingroup$ he has said that they should be separated $\endgroup$ – Chebhou Jul 18 '15 at 19:10
  • $\begingroup$ Thanks your right. Script has been updated to separate files and move the MTL files at the end of each loop increment. $\endgroup$ – Todd McIntosh Jul 18 '15 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.