Alembic sequence export script

Hello! I need help exporting an alembic sequence for the reasons I’ll explain below.

I have run into the known issue of the Alembic exporter not correctly exporting vertex colors on simulations (where geometry data changes frame by frame). Only the first frame will be correct, so I did a test where I manually exported a few frames…1 frame per file…and imported that back in with “Is Sequence” checked in the import settings, and vert color works perfectly! The issue is exporting frames individually for sequences of hundreds of frames is not practical, which is where I need your help.

Do you know how I would write a script to do this? Ideally, it would work on my selection, and I’d set start and end frames, a folder location, and the name of the file would be formatted like this: “myfilename_00042.abc”

import bpy
scene = bpy.context.scene
for frame in range(scene.frame_start, scene.frame_end + 1):
    scene.frame_current = frame
    filename = "myfile_" + str(frame) + ".abc"
    bpy.ops.wm.alembic_export(filepath="/my/file/path/"+filename)

You’ll have to edit the file path strings and you’ll probably want to add more arguments for the the alembic exporter settings, but the above script should work.

Thanks for such a quick response :slight_smile:

This seems to get part of the way there. I’m not sure how to format the file path correctly, and haven’t been able to find what I need through googling it. But I replaced “/my/file/path/” with bpy.data.filepath and that got the script to run.

The main issue with this seems to be that it generates files that are all copies of each other, and all contain the entire sequence, instead of just being a single frame each. In other words, I’m after a file named “myfile_00001.abc” which is only frame one of the sequence, “…00002.abc” which is only frame 2, and so on.

I’ve also found that in order for alembic sequences to be read in correctly, they need to be 5-digit numbers, and I’m not sure what to look for to get the number to be formatted as such. Any thoughts about where to go from here?

You need to check the documentation for the alembic export parameters. It is exporting all the frames with the defaults.

So as far as I can tell, exporting all the frames is the only way the exporter was written to work. I don’t see any flags for exporting a sequence. I would love to read through documentation that tells me how to do this. Sorry, I’m new to scripting in Blender.

There is the start and the end frame settings in the exporter, basically those frames have to be pointing to the same frame number which should be the currently running frame.

Thanks. Yes, conceptually I follow and agree. I just don’t know how to code that.

A co-worker was able to lend me a hand this morning, and got something working. In case anyone else is interested, here’s the update:

import bpy
scene = bpy.context.scene
startFrame = scene.frame_start
endFrame = scene.frame_end
fileNameString = "myfile_0000"

for frame in range(startFrame, endFrame + 1):
    scene.frame_current = frame
    bpy.context.scene.frame_end = frame
    bpy.context.scene.frame_start = frame
    
    if frame >= 10:
        fileNameString = "myfile_000"
    elif frame >= 100:
        fileNameString = "myfile_00"
        
    filename = fileNameString + str(frame) + ".abc"
    bpy.ops.wm.alembic_export(filepath=bpy.data.filepath+filename, vcolors=True,)

bpy.context.scene.frame_end = endFrame
bpy.context.scene.frame_start = startFrame

Sorry to leave you hanging. That’s a much more robust script! There’s a couple things you can still do.

Basically, the most important line is the one that says:
bpy.ops.wm.alembic_export(...)
^ All the settings available to you when you run the exporter manually can also be put between those parenthesis. Those settings can be referred to here.

For instance, you mentioned wanting to export only the selected objects in your scene. This can be acheived by adding the “selected=True” argument to your function call, as in:

bpy.ops.wm.alembic_export(filepath=bpy.data.filepath+filename, vcolors=True, selected=True)

Also, your script appears to hack the exporter by making your global start and end frames the current frame for each exported file. This is not necessary; those are also parameters that you can set within the operator’s function call!

Here’s an update:

import bpy
scene = bpy.context.scene
fileNameString = "myfile_0000"

for frame in range(startFrame, endFrame + 1):
    scene.frame_current = frame

    if frame >= 10:
        fileNameString = "myfile_000"
    elif frame >= 100:
        fileNameString = "myfile_00"
        
    filename = fileNameString + str(frame) + ".abc"
    bpy.ops.wm.alembic_export(filepath=bpy.data.filepath+filename, vcolors=True, selected=True, start=frame, end=frame)

There’s also some more python magic you can do to replace the lines that pad out your filenames with zeroes but it’s not as important. Hope that helps!

Does anyone know how to adapt this script to Blender 2.79? Unfortunately, it exports only the first and last frames.

Hello!

Hoping to revive this topic as I have a very similar problem and your code has got me most of the way there - so thank you for sharing! (and hopefully you can give me some insight in how to get the rest of the way!!! :slight_smile: )

I’m trying to find a way to get a cache of a piece of animation that is currently on 1s so that it is on 2s.
Here is the code that I’ve written that works. It saves each frame as an alembic file and then duplicates that file with the numerical suffix +1.

import bpy
import shutil

scene = bpy.context.scene
startFrame = scene.frame_start
endFrame = scene.frame_end

path = 'path/to/files/'
folder = 'folder/'
fileNameString = 'ferd_pt1_'
step = 2


for frame in range(startFrame, endFrame, step):
    scene.frame_current = frame
    nextFrame = frame + 1
    
    filename = fileNameString + f'{frame:04}' + '.abc'
    bpy.ops.wm.alembic_export(filepath = path + folder + filename, vcolors = True, selected = True, start = frame, end = nextFrame)
    
    duplicateFilename = fileNameString + f'{nextFrame:04}' + '.abc'
    shutil.copy(path + folder + filename, path + folder + duplicateFilename)

The model is from Quill and so it doesnt have any UVs (and would be impossible to UV) so we’re using vertex colours. When I sequence the animation files I get the first frame with correct colours but the colours for the rest of the sequence are all messed up. When I import each frame on its own however the colours are correct. I figure it might have something to do with the fact that each file is using the colour attributes from frame 1?

Thanks in advance!

edit: clarity