Set up a port’s data when the composition starts

Abstract

How to initialize input port values

When a composition starts running, its data-and-event input ports start out with some initial data — either the port’s constant value, if you’ve used the input editor to set one for the port, or the port’s default value. An input port with an incoming data-and-event cable stays at its default value until the first data-and-event comes in through the cable. Sometimes you may want to send certain data with that first event so that the port will start off with the right value.

A simple way to do that is with a Fire on Start node. In the Smooth with Spring example in the previous section, the Fire on Start node fires an event that sets up the data for two input ports. One is the Align Layer to Window node’s Layer input port, which gets the layer created by Make Text Layer. The other is the Smooth with Spring node’s Set Position input port, which gets initialized to (0,0). The Select Latest node helps out here by sending (0,0) to the Set Position port for the Fire on Start event and, after that, the current mouse position each time the Receive Mouse Drags on Layer fires an event.

Using Fire on Start to set up data is pretty simple, but it has one weakness: the Fire on Start node’s trigger isn’t coordinated with other triggers in the composition. If you’re trying to use Fire on Start together with the Refreshed at Time port of Fire on Display Refresh, you might see a momentary flicker or adjustment in graphics as the composition starts. That’s because the Fire on Start event and the first Refreshed at Time event are setting up different parts of the graphics at slightly different times. So how can you avoid the flicker? Instead of Fire on Start, which fires its own event, use Allow First Event, which can borrow the event fired from Refreshed at Time. Here’s an example.