An Open Access Peon

07 January 2007

Changing the Starting Frame of a Fluid Animation in Blender

I've become somewhat enamoured with Blender recently (the open source 3D modeling and ray-tracing program). Once you've got a handle on the interface it's a really functional modeler, at least with my limited experience.

Anyway, a particularly 'cool' feature in Blender is support for fluid dynamics. This allows you to define some 'fluid' and fling it around inside a three dimensional space. Something I got stuck on however was how to get the fluid animation to start after the first frame. If you know beforehand what frame you want to start the animation at you can set the start-frame and away you go. If, like me, you just dived in there and didn't want to waste the fluid calculation then you need to get your hands a bit dirty.

The meshes generated from the fluid calculation are stored in a directory with file names like 'project_blah_XXXX.gz' where XXXX is the frame number. So if you want to shift the starting frame you need to rename all these files, changing the numbers to start at your needed frame. Here's a Perl script that'll do this for you (you ought to take a backup before doing this!):
#!/usr/bin/perl
# Copy this to the directory containing your fluid calculation
# files then do: perl rename.pl

die "Usage: $0 \n" unless @ARGV;
my $SFRAME = shift;

opendir(DIR,".");
my @files = sort { $b cmp $a } grep { $_ !~ /^\./ } readdir(DIR);
closedir(DIR);
foreach(@files)
{
next unless /_\d{4}\./;
my $new = $_;
$new =~ s/(\d+)/sprintf("%04d",$1+$SFRAME)/eg;
print "$_\t$new\n";
rename($_,$new);
}