Skip to content

Automate

PythonMusic supports automation through the Automate.add() function.

Automate drives repeated calls (animating graphics, sweeping a volume or filter, stepping through data, and so on) from a single master timer, so every effect stays in sync. Automation starts as soon as the first function is loaded, and its rate is measured in frames per second.

For example:

randomlyMovingCircle.py
# Animate a circle by moving it randomly.

from gui import *
from random import *
from timer import *

d = Display("Automation Example", 600, 400)

# initialize circle coordinates
x = 300
y = 200

# create filled red circle
c = Circle(x, y, 25, Color.RED, True)
d.add(c)

def moveCircle():

   global x, y   # these will be updated

   # update global circle coordinates
   x = x + randint(-5, 5)
   y = y + randint(-5, 5)

   # now, move circle
   d.move(c, x, y)

# start animation
Automate.add(moveCircle)

For more complex automation, see the Timer class.

Creating an Automation

Automate is a static utility; you don't instantiate it like other objects. Call its methods on the class itself. For example,

Automate.add(moveCircle)

Functions

The following Automate functions are always available:

Function Description
Automate.add(action) Calls a function repeatedly based on automation rate (see setRate()).
Automate.remove(action) Stop calling a registered function.
Automate.resume() Resume automation after a pause.
Automate.pause() Pause automation.
Automate.getRate() Return how often automation runs. Default is 60 times per second.
Automate.setRate() Set how often automation runs.
Automate.addWithValues(action, values) Step a function through a list of values, evenly spaced over time.
Automate.addWithTimedValues(action, values, times) Step a function through a list of values, each delivered at its own time.