Skip to main content
Inspiring
November 8, 2015
Answered

toWorld in scripts

  • November 8, 2015
  • 1 reply
  • 1725 views

Where are toWorld, toComp, etc. expression functions in scripts?  These are fundamental to doing just about anything with a comp, so they must be in here somewhere, but I can't find them anywhere.  Maybe it's just undocumented--the latest doc I can find is from CS6, so the documentation doesn't seem very well maintained...

This topic has been closed for replies.
Correct answer Dan Ebberts

Unfortunately, those are expression-only. You can get the result by creating a temporary layer, adding the toWorld() or toComp() expression to some property, harvesting the result, and deleting the layer. Kind of a pain, but it works.

Dan

1 reply

Dan Ebberts
Community Expert
Dan EbbertsCommunity ExpertCorrect answer
Community Expert
November 8, 2015

Unfortunately, those are expression-only. You can get the result by creating a temporary layer, adding the toWorld() or toComp() expression to some property, harvesting the result, and deleting the layer. Kind of a pain, but it works.

Dan

zewtAuthor
Inspiring
November 8, 2015

That's unbelievably insane.

zewtAuthor
Inspiring
November 9, 2015

For the next person bit by this, here's a helper.  It's very slow--this makes my otherwise ~900ms script take over 10 seconds.

Now I get to figure out how to work around this "invalid index in indexed group" internal error when setting keys...

// layer is the layer to hold the conversion expressions.  targetLayer is the layer
// we're converting relative to.
//
// var conversion = new CoordinateSpaceConversion (layer, layer);
// conversion.toWorld([1,1], 10);
// conversion.cleanup();
CoordinateSpaceConversion = function(layer, targetLayer)
{
    this.layer = layer;
    this.expressionPointControlName = layer.Effects.addProperty("Point Control").name;
    this.toWorldName = layer.Effects.addProperty("Point Control").name;
    this.fromWorldName = layer.Effects.addProperty("Point Control").name;

    var worldToObj = layer.Effects.property(this.fromWorldName).property("Point");
    var e = 'layer = thisComp.layer("' + targetLayer.name + '");\n';
    e +=    'e = effect(\"' + this.expressionPointControlName + '")("Point");\n';
    e +=    'layer.fromWorld(e, time);';
    worldToObj.expression = e;

    var objToWorld = layer.Effects.property(this.toWorldName).property("Point");
    var e = 'layer = thisComp.layer("' + targetLayer.name + '");\n';
    e +=    'e = effect(\"' + this.expressionPointControlName + '")("Point");\n';
    e +=    'layer.toWorld(e, time);\n';
    objToWorld.expression = e;

    // AFX sometimes invalidates property objects when new ones are added.  updateCache can be
    // called if this happens.
    this.updateCache();
}

CoordinateSpaceConversion.prototype.updateCache = function()
{
    this.expressionPointControl = findProperty(this.layer, "Effects", this.expressionPointControlName, "Point");
    this.toWorldPoint = findProperty(this.layer, "Effects", this.toWorldName, "Point");
    this.fromWorldPoint = findProperty(this.layer, "Effects", this.fromWorldName, "Point");
}

CoordinateSpaceConversion.prototype.toWorld = function(point, time)
{
    this.expressionPointControl.setValue([point[0], point[1]]);
    return this.toWorldPoint.valueAtTime(time, false);
}

CoordinateSpaceConversion.prototype.fromWorld = function(point, time)
{
    this.expressionPointControl.setValue([point[0], point[1]]);
    return this.fromWorldPoint.valueAtTime(time, false);
}

CoordinateSpaceConversion.prototype.cleanup = function()
{
    findProperty(this.layer, "Effects", this.expressionPointControlName).remove();
    findProperty(this.layer, "Effects", this.toWorldName).remove();
    findProperty(this.layer, "Effects", this.fromWorldName).remove();
}