redefinery

Motion Trackers

This section describes how to work with motion trackers and motion track points. The following topics are covered:


Checking if a layer can have motion trackers applied

Some types of layers, such as cameras and lights, do not use motion trackers. Before you apply a motion tracker to a layer, first check that the layer can accept them. Although you can check the layer’s type for supported layers, a more future-proof way is checking if the layer has a Motion Trackers property group (PropertyGroup object).

The following code shows how to check a layer for a Motion Trackers property group.

// given:
// layer = Layer object
//
var mtGroup = layer("Motion Trackers");          // Get PropertyGroup for the motion trackers
// This PropertyGroup can also be retrieved by using layer("ADBE MTrackers"),
// layer.property("Motion Trackers"), or layer.property("ADBE MTrackers")
if (mtGroup !== null)                            // Filter out invalid layers
{
    // Layer can have motion trackers
}

Retrieving all motion trackers on a layer

The motion trackers applied to a layer are the properties of the layer’s Motion Trackers PropertyGroup.

The following code shows how to iterate over the properties of a layer’s Motion Trackers PropertyGroup and store them in an array for retrieval or use elsewhere in your code.

// given:
// layer = Layer object
//
var mtOnLayer = new Array();                     // Store motion trackers in an array
var mtGroup = layer("Motion Trackers");
if (mtGroup !== null)
{
                                                 // Iterate through properties of mtGroup
                                                 // Append motion tracker to the array
    for (var i = 1; i <= mtGroup.numProperties; i++)
        mtOnLayer[mtOnLayer.length] = mtGroup.property(i);
}
// The mtOnLayer array now contains the list of motion trackers
// The number of motion trackers is mtOnLayer.length 

Retrieving the selected motion trackers on a layer

If your script needs to operate on the selected motion trackers on a layer, you need to check if the selected attribute for the motion tracker (Property) is true.

The following code shows how to iterate over the properties of a layer’s motion trackers, and store only the properties whose selected attribute is true in an array for retrieval or use elsewhere in your code. This code is very similar to the code for retrieving the motion trackers on a layer, with the extra check for the selected attribute value.

// given:
// layer = Layer object, and the layer can have motion trackers
//
var selectedMTs = new Array();                   // Store motion trackers in an array
var mtGroup = layer("Motion Trackers");
if (mtGroup !== null)
{
    var mtracker;
                                                 // Iterate over all motion trackers on layer
    for (var i = 1; i <= mtGroup.numProperties; i++)
    {
        mtracker = mtGroup.property(i);
        if (mtracker.selected)
            selectedMTs[selectedMTs.length] = mtracker;
    }
}
// The selectedMTs array now contains the list of selected motion trackers
// in top-to-bottom order

Retrieving all motion tracker track points on a layer

The track points of the motion trackers on a layer are the subproperties (properties of the properties) of the layer’s Motion Trackers PropertyGroup.

The following code shows how to iterate over the subproperties of a layer’s Motion Trackers PropertyGroup and store them in an array for retrieval or use elsewhere in your code.

// given:
// layer = Layer object
//
var mttpOnLayer = new Array();
var mtGroup = layer.property("Motion Trackers");
if (mtGroup !== null)
{
    var mtracker;
                                                 // Iterate over the motion trackers
    for (var i = 1; i <= mtGroup.numProperties; i++)
    {
        mtracker = mtGroup.property(i);
                                                 // Store track points in an array
        for (var j = 1; j <= mtracker.numProperties; j++)
            mttpOnLayer[mttpOnLayer.length] = mtracker.property(j);
    }
}
// The mttpOnLayer array now contains the list of motion tracker track points
// The number of motion tracker track points is mttpOnLayer.length 

Retrieving the selected motion tracker track points on a layer

If your script needs to operate on the selected motion tracker track points (subproperties of the motion tracker properties) on a layer, you need to check if the selected attribute for the track point (Property) is true.

The following code shows how to iterate over the subproperties of a layer’s motion trackers, and store only the subproperties whose selected attribute is true in an array for retrieval or use elsewhere in your code. This code is very similar to the code for retrieving the motion tracker track points on a layer, with the extra check for the selected attribute value.

// given:
// layer = Layer object
//
var selectedMTTPs = new Array();                 // Store track points in an array
var mtGroup = layer("Motion Trackers");
if (mtGroup !== null)
{
    var mtracker, mttrackpoint;
                                                 // Iterate over all motion trackers on layer
    for (var i = 1; i <= mtGroup.numProperties; i++)
    {
        mtracker = mtGroup.property(i);
                                                 // Store selected track points in an array
        for (var j = 1; j <= mtracker.numProperties; j++)
        {
            mttrackpoint = mtracker.property(j);
            if (mttrackpoint.selected)
                selectedMTTPs[selectedMTTPs.length] = mttrackpoint;
        }
    }
}
// The selectedMTTPs array now contains the list of selected motion tracker track points
// in top-to-bottom order