Clutter: animations

Simple implicit animations

It is very easy to get something animated. For an actor we might use something like:

_actor_anim = _actor.animatev(
                 Clutter.AnimationMode.EASE_OUT_BOUNCE,
                 1500,
                 ["x"],
                 [20] )

where:

  • EASE_OUT_BOUNCE: this is the effect of the animation
  • 1500 represents how long will be animated in milliseconds.
  • [“x”]: this is a list with the elements that will be animated.
  • [20]: means that at the end of the animation, “x” will be equal to 20.

Full example

Using this base we could write:

#!/usr/bin/env python
#! -*- coding: utf-8 -*-

from gi.repository import Clutter
import sys

if __name__ == '__main__':
    Clutter.init( sys.argv )

    # Create Stage
    _stage = Clutter.Stage()
    _stage.set_title( "Basic Usage" )
    _stage.set_size( 400, 200 )

    # Create Actor
    _red = Clutter.Color().new(255, 0, 0, 255) # R,G,B,alpha
    _actor = Clutter.Text().new_full(
                  "Mono 10",
                  "Hello World!",
                  _red )

    _actor.set_position( 100,100 )
    _actor_anim = _actor.animatev(
                     Clutter.AnimationMode.EASE_OUT_BOUNCE,
                     1500,
                     ["x"],
                     [20] )

    # Add Actor to the Stage
    _stage.add_actor( _actor )
    _stage.connect("destroy", lambda w: Clutter.main_quit() )
    _stage.show_all()

    Clutter.main()

more information about Implicit animations.

Animation based on state

The following code is failing for some reason (any help is welcomed):

#!/usr/bin/env python
#! -*- coding: utf-8 -*-

from gi.repository import Clutter
import sys

def keyPress(self, event, _transitions):
    Clutter.State.set_state(_transitions, "move-down")

if __name__ == '__main__':
    Clutter.init( sys.argv )

    # Create Stage
    _stage = Clutter.Stage()
    _stage.set_title( "Animation using states" )
    _stage.set_size( 400, 400 )

    # Create Actor
    _red = Clutter.Color().new(255, 0, 0, 255) # R,G,B,alpha
    _actor = Clutter.Text().new_full(
                  "Mono 10",
                  "Press any key...",
                  _red )

    _actor.set_position( 100.0,100.0 )

    # Transition
    # - State creation
    _transitions = Clutter.State()



    # - Defines de behaviour of a number of actors
    _transitions.set_key( None, "move-down",  # source_state, target_state
          _actor, "x", Clutter.AnimationMode.EASE_OUT_CUBIC, 300,
          pre_delay=0.0, post_delay=0.0)

    # - All state transitions take 250ms
    _transitions.set_duration(None,None,3000)

    # Add Actor to the Stage
    _stage.add_actor( _actor )
    _stage.connect("destroy", lambda w: Clutter.main_quit() )
    _stage.connect('key-press-event', keyPress, _transitions)

    _stage.show_all()

    # Create animation


    Clutter.main()

Currently this code fails complaining of:

(anim_states.py:12376): Clutter-CRITICAL **: clutter_interval_set_final_value: assertion `G_VALUE_TYPE (value) == priv->value_type' failed
/usr/lib/python3.2/site-packages/gi/types.py:43: Warning: g_value_set_float: assertion `G_VALUE_HOLDS_FLOAT (value)' failed
  return info.invoke(*args, **kwargs)
/usr/lib/python3.2/site-packages/gi/types.py:43: Warning: g_value_get_float: assertion `G_VALUE_HOLDS_FLOAT (value)' failed
  return info.invoke(*args, **kwargs)

Animator

The reference documentation for the animator.

Not working for me yet:

#!/usr/bin/env python
#! -*- coding: utf-8 -*-

from gi.repository import Clutter
import sys

def keyPress(self, event, _anim):
    _anim.start()

if __name__ == '__main__':
    Clutter.init( sys.argv )

    # Create Stage
    _stage = Clutter.Stage()
    _stage.set_title( "Animation using states" )
    _stage.set_size( 400, 400 )
    _stage.set_reactive( True )

    # Create Actor
    _red = Clutter.Color().new(255, 0, 0, 255) # R,G,B,alpha
    _actor = Clutter.Text().new_full(
                  "Mono 10",
                  "Press any key...",
                  _red )

    _actor.set_position( 100.0,100.0 )

    # Transition
    # - State creation
    _anim = Clutter.Animator()
    _anim.set_duration( 4000 )
    #_anim.set_properties(
       # start of the animation
    #   _actor, "x", Clutter.AnimationMode.LINEAR, 0.0, 0.0,

       # half-way
    #   _actor, "x", Clutter.Animation.LINEAR, 0.5, 100.0,

       # end of animation
    #   _actor, "x", Clutter.Animation.LINEAR, 0.5, 100.0,
    #   )

    _anim.set_key( _actor, "x", Clutter.AnimationMode.LINEAR, 0.0, 50.0)

       # half-way
       #     _actor, "x", Clutter.Animation.LINEAR, 0.5, 100.0,

       # end of animation
       #    _actor, "x", Clutter.Animation.LINEAR, 0.5, 100.0,
       #    )
    # - Defines de behaviour of a number of actors

       #          pre_delay=0.0, post_delay=0.0)

    # - All state transitions take 250ms
    #    _transitions.set_duration(None,None,3000)

    # Add Actor to the Stage
    _stage.add_actor( _actor )
    _stage.connect("destroy", lambda w: Clutter.main_quit() )
    _stage.connect('key-press-event', keyPress, _anim)

    _stage.show_all()

    # Create animation


    Clutter.main()