I am not a programmer and I don't know what I am doing.

I want to export many shapekeys as OBJ files from an active object. Doing this manually would take an insane amount of time. Unfortunately, I haven't found an existing script that is doing this.

What I want to do: (Blender button: I dont know how to do that)

for each 'shapekey' in the 'acive object':
    if shapekey is 'hidden'
        1. unhide shapekey
        2. export 'active object' as "shapekey's name".obj
        3. hide shapekey

Here is what I managed to cobble together from code I found in stackoverflow and blender stack exchange so far:

import os   
import bpy

# Save the files where the .blend file is located
basedir = bpy.path.abspath('//')

ob = bpy.context.object.data.shape_keys.key_blocks

for i in ob:
    if i.mute == True:
        i.mute = False
            filepath=os.path.join(basedir, i.name + '.obj'),
        i.mute = True

This script will iterate over the active object's shape keys, set each in turn to 1.0 (while the others are set to 0.0) and save this as an OBJ file.

import bpy
from os.path import join

o = bpy.context.object # Reference the active object

# Change this to the actual folder you want to save your OBJ files in
exportPath = "C:/tmp/obj" 

# Reset all shape keys to 0 (skipping the Basis shape on index 0
for skblock in o.data.shape_keys.key_blocks[1:]:
    skblock.value = 0

# Iterate over shape key blocks and save each as an OBJ file
for skblock in o.data.shape_keys.key_blocks[1:]:
    skblock.value = 1.0  # Set shape key value to max

    # Set OBJ file path and Export OBJ
    objFileName = skblock.name + ".obj" # File name = shapekey name
    objPath = join( exportPath, objFileName )
    bpy.ops.export_scene.obj( filepath = objPath, use_selection = True )

    skblock.value = 0 # Reset shape key value to 0
  • $\begingroup$ Thanks for your answer. Setting the shapekey to 0 is not what I need because a lot of the shapekeys have values like 0.384 for example. Hide and Unhide works perfectly for me. I think my script crashes when I run it a second time because I need to put a sys.exit() at the end? $\endgroup$ – kopei Jul 28 '17 at 14:07
  • $\begingroup$ You don't need to put a sys.exit(), that will exit Blender after the script is finished. Sorry, I didn't notice you reporting the script crashes. Could it be a permissions issue? Where are you trying to save your OBJ files? Do they exist already and are being overwritten? (If so, maybe they're locked by some other program / process?) $\endgroup$ – TLousky Jul 28 '17 at 14:09
  • $\begingroup$ I found out why the script/blender "crashed". If you 'mark' text in the Blender 'System Console' the script will not run. (?) $\endgroup$ – kopei Jul 28 '17 at 15:56

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.