I feel I should start by noting that I'm new to Sage itself, and I'm
no developer.

Anyway, one thing that I immediately looked for upon compiling Sage on
my system was a way to animate plots. For 2D plots, it was simple, of
course. But I was concerned when I saw that there was no animation
functionality for 3D plots.

Having a little bit of experience with coding in python, and using a
bit of simple initiative, I figured out that 3D animation using
Tachyon in Sage is actually quite trivial (at least in Notebook mode),
in that it follows the exact same code approach used for 2D animation
- indeed, no need to even specify Tachyon, as it seems to default to
that when using the png-generation function that is used by animate.

Anyway, I suppose I'm wondering why such a simple functionality has
not been added to Sage, yet?

Here's how it works:

def png3d(G, dir=None):
        d = sage.misc.misc.tmp_dir()
        for i, frame in enumerate(G):
            filename = '%s/%s'%(d,sage.misc.misc.pad_zeros(i,8))
            frame.save(filename + '.png')
        return d

def gif3d(animset, delay=20, savefile=None, iterations=0,
show_path=False):
        if not savefile:
            savefile = sage.misc.misc.graphics_filename(ext='gif')
        if not savefile.endswith('.gif'):
            savefile += '.gif'
        savefile = os.path.abspath(savefile)
        d = png3d(animset)
        cmd = 'cd "%s"; sage-native-execute convert -delay %s -loop %s
*.png "%s"'%(d, int(delay), int(iterations), savefile)
        from subprocess import check_call, CalledProcessError
        try:
            check_call(cmd, shell=True)
            if show_path:
                print "Animation saved to file %s." % savefile
        except (CalledProcessError, OSError):
            print ""
            print "Error: ImageMagick does not appear to be installed.
Saving an"
            print "animation to a GIF file or displaying an animation
requires"
            print "ImageMagick, so please install it and try again."
            print ""
            print "See www.imagemagick.org, for example."

def animate3dshow(animset, delay=20, iterations=0):
    gif3d(animset,delay = delay, iterations = iterations)

Most of this code is just directly from Sage's animate.py, with
appropriate changes. All the comments, obviously useless parts (in
context), etc, have been stripped away, so I could test it quickly. It
produces 3D animations using tachyon, and outputs them in the
notebook, just as desired.

-- 
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to