Skip to main content
Participant
June 10, 2024
Question

distort a layer / smartlayer by four coordinates

  • June 10, 2024
  • 2 replies
  • 690 views

Hi all,

I want to distort (free transform) the four corners of a layer (rectangle shape) to 4 specific coordinates.
For example: [[971,776],[1518,688],[1519,974],[968,1312]]

Scriptlistener showed this, but I dont see any coordinates:

// =======================================================
var idTrnf = charIDToTypeID( "Trnf" );
    var desc252 = new ActionDescriptor();
    var idFTcs = charIDToTypeID( "FTcs" );
    var idQCSt = charIDToTypeID( "QCSt" );
    var idQcsa = charIDToTypeID( "Qcsa" );
    desc252.putEnumerated( idFTcs, idQCSt, idQcsa );
    var idOfst = charIDToTypeID( "Ofst" );
        var desc253 = new ActionDescriptor();
        var idHrzn = charIDToTypeID( "Hrzn" );
        var idPxl = charIDToTypeID( "#Pxl" );
        desc253.putUnitDouble( idHrzn, idPxl, 700.958190 );
        var idVrtc = charIDToTypeID( "Vrtc" );
        var idPxl = charIDToTypeID( "#Pxl" );
        desc253.putUnitDouble( idVrtc, idPxl, 367.508658 );
    var idOfst = charIDToTypeID( "Ofst" );
    desc252.putObject( idOfst, idOfst, desc253 );
    var idWdth = charIDToTypeID( "Wdth" );
    var idPrc = charIDToTypeID( "#Prc" );
    desc252.putUnitDouble( idWdth, idPrc, 148.730064 );
    var idHght = charIDToTypeID( "Hght" );
    var idPrc = charIDToTypeID( "#Prc" );
    desc252.putUnitDouble( idHght, idPrc, 92.668201 );
    var idSkew = charIDToTypeID( "Skew" );
        var desc254 = new ActionDescriptor();
        var idHrzn = charIDToTypeID( "Hrzn" );
        var idAng = charIDToTypeID( "#Ang" );
        desc254.putUnitDouble( idHrzn, idAng, -3.365472 );
        var idVrtc = charIDToTypeID( "Vrtc" );
        var idAng = charIDToTypeID( "#Ang" );
        desc254.putUnitDouble( idVrtc, idAng, 0.000000 );
    var idPnt = charIDToTypeID( "Pnt " );
    desc252.putObject( idSkew, idPnt, desc254 );
    var idAngl = charIDToTypeID( "Angl" );
    var idAng = charIDToTypeID( "#Ang" );
    desc252.putUnitDouble( idAngl, idAng, 0.490732 );
    var idUsng = charIDToTypeID( "Usng" );
        var desc255 = new ActionDescriptor();
        var idHrzn = charIDToTypeID( "Hrzn" );
        var idPrc = charIDToTypeID( "#Prc" );
        desc255.putUnitDouble( idHrzn, idPrc, 0.086137 );
        var idVrtc = charIDToTypeID( "Vrtc" );
        var idPrc = charIDToTypeID( "#Prc" );
        desc255.putUnitDouble( idVrtc, idPrc, -0.010603 );
    var idPnt = charIDToTypeID( "Pnt " );
    desc252.putObject( idUsng, idPnt, desc255 );
    var idreplaceLayer = stringIDToTypeID( "replaceLayer" );
        var desc256 = new ActionDescriptor();
        var idFrom = charIDToTypeID( "From" );
            var ref6 = new ActionReference();
            var idLyr = charIDToTypeID( "Lyr " );
            ref6.putIdentifier( idLyr, 26 );
        desc256.putReference( idFrom, ref6 );
        var idT = charIDToTypeID( "T   " );
            var ref7 = new ActionReference();
            var idLyr = charIDToTypeID( "Lyr " );
            ref7.putIdentifier( idLyr, 26 );
        desc256.putReference( idT, ref7 );
    var idTrnf = charIDToTypeID( "Trnf" );
    desc252.putObject( idreplaceLayer, idTrnf, desc256 );
executeAction( idTrnf, desc252, DialogModes.NO );

 

This topic has been closed for replies.

2 replies

c.pfaffenbichler
Community Expert
Community Expert
June 12, 2024

@r-bin posted a function a while ago; p1 is an array of the current bounds, p2 an darray of the four target-points. 

////// free transform by r-bin //////
function non_affine_transform(p0, p1) 
{  
try {
var d = new ActionDescriptor();
var r = new ActionReference();
r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
d.putReference(stringIDToTypeID("null"), r);

var l = new ActionList();
l.putUnitDouble(stringIDToTypeID("pixelsUnit"), p0[0]);
l.putUnitDouble(stringIDToTypeID("pixelsUnit"), p0[1]);
l.putUnitDouble(stringIDToTypeID("pixelsUnit"), p0[2]);
l.putUnitDouble(stringIDToTypeID("pixelsUnit"), p0[3]);
d.putList(stringIDToTypeID("rectangle"), l );

var l = new ActionList();
l.putUnitDouble(stringIDToTypeID("pixelsUnit"), p1[0][0]);
l.putUnitDouble(stringIDToTypeID("pixelsUnit"), p1[0][1]);
l.putUnitDouble(stringIDToTypeID("pixelsUnit"), p1[1][0]);
l.putUnitDouble(stringIDToTypeID("pixelsUnit"), p1[1][1]);
l.putUnitDouble(stringIDToTypeID("pixelsUnit"), p1[2][0]);
l.putUnitDouble(stringIDToTypeID("pixelsUnit"), p1[2][1]);
l.putUnitDouble(stringIDToTypeID("pixelsUnit"), p1[3][0]);
l.putUnitDouble(stringIDToTypeID("pixelsUnit"), p1[3][1]);
d.putList(stringIDToTypeID("quadrilateral"), l);

d.putEnumerated(stringIDToTypeID("interpolation"), stringIDToTypeID("interpolationType"), stringIDToTypeID("bicubic"));

executeAction(stringIDToTypeID("transform"), d, DialogModes.NO); 

return true;
}
catch (e) { alert(e); return false; }        
};

 

Inspiring
July 22, 2024

Hi, thanks for the solution.
Is it possible to get Distort points?
I mean transformation cordinats

c.pfaffenbichler
Community Expert
Community Expert
July 22, 2024

Please elaborate. 

Stephen Marsh
Community Expert
Community Expert
June 10, 2024

Looking at the recorded code, more is going on under the hood than one appreciates when simply moving the four points via Transform > Distort in the GUI.

 

Here I have converted your raw recording via the Clean SL script, which makes things just a little more legible:

 

var c2t = function (s) {
	return app.charIDToTypeID(s);
};

var s2t = function (s) {
	return app.stringIDToTypeID(s);
};

var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
var descriptor3 = new ActionDescriptor();
var descriptor4 = new ActionDescriptor();
var descriptor5 = new ActionDescriptor();
var reference = new ActionReference();
var reference2 = new ActionReference();

descriptor.putEnumerated( s2t( "freeTransformCenterState" ), s2t( "quadCenterState" ), s2t( "QCSAverage" ));
descriptor2.putUnitDouble( s2t( "horizontal" ), s2t( "pixelsUnit" ), 700.958190 );
descriptor2.putUnitDouble( s2t( "vertical" ), s2t( "pixelsUnit" ), 367.508658 );
descriptor.putObject( s2t( "offset" ), s2t( "offset" ), descriptor2 );
descriptor.putUnitDouble( s2t( "width" ), s2t( "percentUnit" ), 148.730064 );
descriptor.putUnitDouble( s2t( "height" ), s2t( "percentUnit" ), 92.668201 );
descriptor3.putUnitDouble( s2t( "horizontal" ), s2t( "angleUnit" ), -3.365472 );
descriptor3.putUnitDouble( s2t( "vertical" ), s2t( "angleUnit" ), 0.000000 );
descriptor.putObject( s2t( "skew" ), c2t( "Pnt " ), descriptor3 );
descriptor.putUnitDouble( s2t( "angle" ), s2t( "angleUnit" ), 0.490732 );
descriptor4.putUnitDouble( s2t( "horizontal" ), s2t( "percentUnit" ), 0.086137 );
descriptor4.putUnitDouble( s2t( "vertical" ), s2t( "percentUnit" ), -0.010603 );
descriptor.putObject( s2t( "using" ), c2t( "Pnt " ), descriptor4 );
reference.putIdentifier( s2t( "layer" ), 26 );
descriptor5.putReference( s2t( "from" ), reference );
reference2.putIdentifier( s2t( "layer" ), 26 );
descriptor5.putReference( s2t( "to" ), reference2 );
descriptor.putObject( s2t( "replaceLayer" ), s2t( "transform" ), descriptor5 );
executeAction( s2t( "transform" ), descriptor, DialogModes.NO );