Sunday, April 23, 2017

Tweened vector animations with python

What?

In this blog post I'll show how to create tweened vector animations with python. People who arrive here probably ought to look at Zulko's excellent blog post first to find the excellent gizeh library, written on top of cairo, that allows for generating vector graphics animations in python. And then there's also Zulko's wonderful moviepy library that allows saving these animations to animated .gif or one of the many video formats supported by ffmpeg.

While experimenting with Zulko's libraries, I was struck by what seemed a missing feature: a convenient way to introduce "easing" in animations. I subsequently found the pytweening library which implements the formulas required to perform easing and created my own vectortween library to wrap pytweening and make it usable in combination with Zulko's libraries (as well as other libraries).

How?

Here's some sample code using plain gizeh. It creates a yellow circle moving from the top left to the middle of the drawing:

if __name__ == "__main__":
    import gizeh
    import moviepy.editor as mpy

    W, H = 250, 250  # width, height, in pixels
    duration = 5 # duration of the clip, in seconds
    fps = 25

    def make_frame(t):
       # prepare a drawing surface
       surface = gizeh.Surface(W, H)

       # as time t evolves from 0 to 5 seconds, calculate new position of
       # the yellow circle
       gizeh.circle(30, xy=((t*22),(t*22)), fill=(1,1,0)).draw(surface)
       return surface.get_npimage()

    clip = mpy.VideoClip(make_frame, duration=duration)
    clip.write_gif("example0-plain.gif", fps=fps, fuzz=10)

And what does it look like?

Boring, right?

Now how about you wanted add some excitement by letting the yellow circle bounce a bit upon arrival in the middle of the picture? Are you ready to write out the math formulas to do so? If yes, by all means go ahead! If no, you could try the vectortweening library to write this:

if __name__ == "__main__":
    import gizeh
    import moviepy.editor as mpy

    from vectortween.PointAnimation import PointAnimation

    W, H = 250, 250  # width, height, in pixels
    duration = 5 # duration of the clip, in seconds
    fps = 25

    def make_frame(t):
        # prepare a drawing surface
        surface = gizeh.Surface(W, H)

        # p animates from position (0,0) to position (110,110) with an
        # easeOutElastic tweening method
        p = PointAnimation((0, 0), (110, 110), tween=['easeOutElastic', 1, 0.2])

        # circle to appear at second 0.2, animate from second 1 to 4,
        # then stay visible until disappearance in second 5
        xy = p.make_frame(t, 0.2, 1, 4, 5)

        # because graphics can disappear, we must check for None
        if None not in xy:
            gizeh.circle(30, xy=xy, fill=(1,1,0)).draw(surface)
            return surface.get_npimage()

    clip = mpy.VideoClip(make_frame, duration=duration)
    clip.write_gif("example0-tween.gif", fps=fps, fuzz=10)

And what does it look like?


We want more, we want more...

Ok, ok... I hear you. Here's another one:


Ooooh I need this now!

Well of course you do... Here's all you need!

Saturday, March 12, 2016

Virtualbox on arch linux kernel 4.4.5


Since my last upgrade to latest packages of arch linux including kernel 4.4.5 I had trouble starting virtualbox.

[user@xxxxx ~]$ sudo /sbin/rcvboxdrv setup           Unloading modules:  Loading modules: modprobe: ERROR: could not insert 'vboxnetadp': Exec format error
modprobe: ERROR: could not insert 'vboxnetflt': Exec format error
modprobe: ERROR: could not insert 'vboxpci': Exec format error
modprobe: ERROR: could not insert 'vboxdrv': Exec format error
Here are the steps I took to make the trouble go away:
  • reboot to make the new kernel 4.4.5 active
    • sudo shutdown -r now
  • reinstall linux-headers (use the correct version for your kernel!): this triggered a recompile of the virtualbox kernel modules by dkms
    • sudo pacman -S linux-headers
  • remove package virtualbox-host-modules: this removed old pre-compiled vbox kernel modules. In retrospect, it was probably this last step that actually solved the problem.
    • sudo pacman -R virtualbox-host-modules
For what it's worth :)

Friday, August 21, 2015

Microsoft Random Limitation Rant

Welcome in 2015, the era of Multi-GHz Clocks, Multi-GB RAM, PetaByte storage and... 260 character path names.

Once again I was bitten by yet a random path length limitation on a win 7 64-bit platform with visual studio 2013. Of course I am aware that longer path lengths are supported on current windows platforms, provided you do some "\\?\" hocus pocus but Microsoft basically admits it's too much magic for them, given that they won't fix the random limitations embedded in their own build tools and just prefer to let hundreds (probably thousands) of developers bump into them.

Don't believe me? Read the giant's own website for more details and weep :)  https://connect.microsoft.com/VisualStudio/Feedback/Details/932051

More info about using long path names:



Monday, August 11, 2014

Why did flash player stop working in chromium on Debian?

Adobe flash is evil right? Yet many sites insist on using it (who knows for what evil purpose?!) and it's annoying the heck out of me if they keep throwing warnings about a missing flash player. 

Well, here's a clue: somehow I missed the news that chrome/chromium now uses a google maintained version of flash player which you can install using 
apt-get install pepperflashplugin-nonfree
Nothing to see here really. Carry on :)


Sunday, June 8, 2014

problems encountered while switching to systemd

Computer stopped booting

After the latest dist-upgrade in debian SID my system refused to boot. After seemingly doing nothing for a while my computer would just drop into an emergency root console. In the mean time everything is up and running again, and here's what I had to do to make it boot again:

  • After reading many bug reports and forum entries about NFS mounts in fstab causing a system not to boot, I decided to take a look at my /etc/fstab. In /etc/fstab I had specified (amongst other things) mount points for cdrom, dvd, and up to two usb sticks:
/dev/cdrom /media/cdrom0 udf,iso9660 user,noauto 0 0
/dev/dvdrw1 /media/dvd auto noauto,rw,user,exec 0 0
/dev/sdb1 /media/usbstick auto users,rw
/dev/sdc1 /media/usbstick2 auto users,rw
After commenting out all those lines, the system suddenly booted again. Despite the "missing" entries, KDE still manages to recognize USB sticks and while it might be unrelated, I noticed that now suddenly KDE is able to correctly play audio CDs -- something I had never managed before. The player would start but I never got any sound from it.


CUPS refused to start

Once the system booted again I hit the next problem: I couldn't print anymore since CUPS refused to come up. After searching the net, I saw more people having the same problem. In a lucky guess I looked in /etc/sysctl.conf and saw a line:

net.ipv6.conf.all.disable_ipv6 = 1

I disabled that line and cups worked again! I then added that information to a bug record related to bringing up CUPS on systemd https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=742668#76 . In the mean time other people have encountered the same issue:
https://lists.debian.org/debian-user/2014/06/msg00355.html


No visual feedback during boot

This was the least of my problems initially, but after a while I missed the coloured OK's during startup.
Turns out /etc/default/grub contained a line

GRUB_CMDLINE_LINUX_DEFAULT="quiet"

after changing that to

GRUB_CMDLINE_LINUX_DEFAULT=""

and running update-grub, my beloved colored OK's are back :)


Conclusion

All systemd versus SysVinit wars aside, I haven't seen other issues on my system so far. Keeping fingers crossed :)

Sunday, March 9, 2014

Getting rid of "What's hot and recommended" in your google plus stream

1. go tohttps://plus.google.com/[google.com]
2. click the explore link on the top of the page
3. click the "what's hot" link in the "Explore google+" blue thingy that appears
4. click the cogwheel in the red "What's hot and recommended" thingy that appears
5. uncheck "show posts in home stream"