How to set corners (free transform) to smart layer by script?
Copy link to clipboard
Copied
Hi,
Below is the code that is getting smart object corners coordinates (from free transform). How I can take these corners and apply to another smart object? Can anyone help with the SET function?
You can simply run this code on your side to get complete understanding of what I mean. It's 4 relative coordinates of four corners when you move each corner independently in Free Transform. (Hold Option/Control + drag the corner point).
Example :
function get_smart_object_corners(layer) {
try
{
if(layer.kind == LayerKind.SMARTOBJECT)
{
var layer0 = activeDocument.activeLayer;
activeDocument.activeLayer = layer;
var r = new ActionReference();
r.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
var d;
try { d = executeActionGet(r); } catch (e) { alert(e); return; }
try { d = d.getObjectValue(stringIDToTypeID("smartObjectMore"));} catch (e) { alert(e); return; }
try { d = d.getList(stringIDToTypeID("nonAffineTransform"));} catch (e) { alert(e); return; }
var ret = [ [UnitValue(d.getDouble(0), "px"), UnitValue(d.getDouble(1), "px")],
[UnitValue(d.getDouble(2), "px"), UnitValue(d.getDouble(3), "px")],
[UnitValue(d.getDouble(4), "px"), UnitValue(d.getDouble(5), "px")],
[UnitValue(d.getDouble(6), "px"), UnitValue(d.getDouble(7), "px")] ];
activeDocument.activeLayer = layer0;
return ret;
}
else return layer.boundsNoEffects;
}
catch (e) { alert(e); }
}
Explore related tutorials & articles
Copy link to clipboard
Copied
I do not exactly understand what you are trying to do. Every Smart Object layer has an associated layer transform that may scales, rotate and distort the layer object. There are more things involved then the two smart object layers current corners locations. Are the object being rotated, scaled, distorted by their current associated layer transforms. What are you trying to do?
If search worked here I would suggest searching on r-bin post on smart object layer and the information you can retrieve about them. The old forums search was so much better than the changed site search.
For example here you see a rectangle smart object layer that its associated layer transform has rotated the rectangle some and scaled it down in size some and has not distorted the rectangle for it look like a rotated rectangle. The first script got the object four corners calculated the slopes of the rectangle path sides and their length and the total length of the sides. From the slope angle you can see the object has 90 degree corners and the opposite side are the same length. It still a rectangle. The smart object layer bounds is quit different. From the associated layer transform you can see the object has been scaled down in size to 67.51% width and height a constrained resizing. However, the rectangle was rotated -19.90 degrees by the transform but the associated transform warp controls show no wrapping was applied.
This site seems to resize posted screen captures and image you may not be able the read the resized images this site creates it post. I also tried toe attach the Image I posted below perhaps you will fine one readable.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
What does one smart object layer corners have to do with an other smart object layer corners. Do the two object have anything in common? Size, Aspect ratio, rotation, Scaling, shape? What are you trying to do. Are you trying to distort one object to the other object location and size? Do either of the object have transparent areas?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
I see what may or may not be a smart object layer in Photoshop no Photoshop palettes are shown like the layer palette and the transform option bar is also not shown. If it is a smart object layer. The smart object may have been a rectangular area filled with a checkerboard fill pattern. The smart object layers associated transform being show on the image seems to be distorting the rectangle object to none rectangular four sided shape. Using r-bin script code you easily get the four corners locations. I do not believe you can get the transform width and height scaling percentages though. How does this smart object layer's associated transform four corners locations relate to the other smart object layer current associated layer.s transform? What are you trying to accomplish?
Copy link to clipboard
Copied
Transform are not set like selections and paths with point x,y locations. Adobe also did not include a DOM transform method. To set a transform. You need to create a Function that uses Action Manger code. transform is quite complex and may have many parameters.
Action manager code to set that last transform that distorts the oval smart object layer
/ =======================================================
var idTrnf = charIDToTypeID( "Trnf" );
var desc749 = new ActionDescriptor();
var idFTcs = charIDToTypeID( "FTcs" );
var idQCSt = charIDToTypeID( "QCSt" );
var idQcsa = charIDToTypeID( "Qcsa" );
desc749.putEnumerated( idFTcs, idQCSt, idQcsa );
var idOfst = charIDToTypeID( "Ofst" );
var desc750 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idPxl = charIDToTypeID( "#Pxl" );
desc750.putUnitDouble( idHrzn, idPxl, 3.890141 );
var idVrtc = charIDToTypeID( "Vrtc" );
var idPxl = charIDToTypeID( "#Pxl" );
desc750.putUnitDouble( idVrtc, idPxl, 22.164683 );
var idOfst = charIDToTypeID( "Ofst" );
desc749.putObject( idOfst, idOfst, desc750 );
var idWdth = charIDToTypeID( "Wdth" );
var idPrc = charIDToTypeID( "#Prc" );
desc749.putUnitDouble( idWdth, idPrc, 87.976128 );
var idHght = charIDToTypeID( "Hght" );
var idPrc = charIDToTypeID( "#Prc" );
desc749.putUnitDouble( idHght, idPrc, 102.616586 );
var idSkew = charIDToTypeID( "Skew" );
var desc751 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idAng = charIDToTypeID( "#Ang" );
desc751.putUnitDouble( idHrzn, idAng, -1.465292 );
var idVrtc = charIDToTypeID( "Vrtc" );
var idAng = charIDToTypeID( "#Ang" );
desc751.putUnitDouble( idVrtc, idAng, 0.000000 );
var idPnt = charIDToTypeID( "Pnt " );
desc749.putObject( idSkew, idPnt, desc751 );
var idAngl = charIDToTypeID( "Angl" );
var idAng = charIDToTypeID( "#Ang" );
desc749.putUnitDouble( idAngl, idAng, -0.639322 );
var idUsng = charIDToTypeID( "Usng" );
var desc752 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idPrc = charIDToTypeID( "#Prc" );
desc752.putUnitDouble( idHrzn, idPrc, 0.002021 );
var idVrtc = charIDToTypeID( "Vrtc" );
var idPrc = charIDToTypeID( "#Prc" );
desc752.putUnitDouble( idVrtc, idPrc, 0.006530 );
var idPnt = charIDToTypeID( "Pnt " );
desc749.putObject( idUsng, idPnt, desc752 );
var idTwst = charIDToTypeID( "Twst" );
var idPxl = charIDToTypeID( "#Pxl" );
desc749.putUnitDouble( idTwst, idPxl, 150.000000 );
var idPrsp = charIDToTypeID( "Prsp" );
desc749.putInteger( idPrsp, 1 );
var idCrss = charIDToTypeID( "Crss" );
var desc753 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idPxl = charIDToTypeID( "#Pxl" );
desc753.putUnitDouble( idHrzn, idPxl, 352.890141 );
var idVrtc = charIDToTypeID( "Vrtc" );
var idPxl = charIDToTypeID( "#Pxl" );
desc753.putUnitDouble( idVrtc, idPxl, 271.164683 );
var idOfst = charIDToTypeID( "Ofst" );
desc749.putObject( idCrss, idOfst, desc753 );
executeAction( idTrnf, desc749, DialogModes.NO );
That code was recorded by Adobe Scriptlistener Plugin. Its not very readable. Here is that code cleaned up using the Clean SL script to make it more readable.
/ =======================================================
transform(3.890141, 22.164683, 87.976128, 102.616586, -1.465292, 0, -0.639322, 0.002021, 0.00653, 150, 1, 352.890141, 271.164683);
function transform(horizontal, vertical, width, height, horizontal2, vertical2, angle, horizontal3, vertical3, twist, Prsp, horizontal4, vertical4) {
var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
var descriptor3 = new ActionDescriptor();
var descriptor4 = new ActionDescriptor();
var descriptor5 = new ActionDescriptor();
descriptor.putEnumerated( stringIDToTypeID( "freeTransformCenterState" ), stringIDToTypeID( "quadCenterState" ), stringIDToTypeID( "QCSAverage" ));
descriptor2.putUnitDouble( stringIDToTypeID( "horizontal" ), stringIDToTypeID( "pixelsUnit" ), horizontal );
descriptor2.putUnitDouble( stringIDToTypeID( "vertical" ), stringIDToTypeID( "pixelsUnit" ), vertical );
descriptor.putObject( stringIDToTypeID( "offset" ), stringIDToTypeID( "offset" ), descriptor2 );
descriptor.putUnitDouble( stringIDToTypeID( "width" ), stringIDToTypeID( "percentUnit" ), width );
descriptor.putUnitDouble( stringIDToTypeID( "height" ), stringIDToTypeID( "percentUnit" ), height );
descriptor3.putUnitDouble( stringIDToTypeID( "horizontal" ), stringIDToTypeID( "angleUnit" ), horizontal2 );
descriptor3.putUnitDouble( stringIDToTypeID( "vertical" ), stringIDToTypeID( "angleUnit" ), vertical2 );
descriptor.putObject( stringIDToTypeID( "skew" ), charIDToTypeID( "Pnt " ), descriptor3 );
descriptor.putUnitDouble( stringIDToTypeID( "angle" ), stringIDToTypeID( "angleUnit" ), angle );
descriptor4.putUnitDouble( stringIDToTypeID( "horizontal" ), stringIDToTypeID( "percentUnit" ), horizontal3 );
descriptor4.putUnitDouble( stringIDToTypeID( "vertical" ), stringIDToTypeID( "percentUnit" ), vertical3 );
descriptor.putObject( stringIDToTypeID( "using" ), charIDToTypeID( "Pnt " ), descriptor4 );
descriptor.putUnitDouble( stringIDToTypeID( "twist" ), stringIDToTypeID( "pixelsUnit" ), twist );
descriptor.putInteger( charIDToTypeID( "Prsp" ), Prsp );
descriptor5.putUnitDouble( stringIDToTypeID( "horizontal" ), stringIDToTypeID( "pixelsUnit" ), horizontal4 );
descriptor5.putUnitDouble( stringIDToTypeID( "vertical" ), stringIDToTypeID( "pixelsUnit" ), vertical4 );
descriptor.putObject( stringIDToTypeID( "crossover" ), stringIDToTypeID( "offset" ), descriptor5 );
executeAction( stringIDToTypeID( "transform" ), descriptor, DialogModes.NO );
}
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
var idTrnf = charIDToTypeID( "Trnf" );
var desc71064 = new ActionDescriptor();
var idFTcs = charIDToTypeID( "FTcs" );
var idQCSt = charIDToTypeID( "QCSt" );
var idQcsa = charIDToTypeID( "Qcsa" );
desc71064.putEnumerated( idFTcs, idQCSt, idQcsa );
var idOfst = charIDToTypeID( "Ofst" );
var desc71065 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idRlt = charIDToTypeID( "#Rlt" );
desc71065.putUnitDouble( idHrzn, idRlt, -123.948781 );
var idVrtc = charIDToTypeID( "Vrtc" );
var idRlt = charIDToTypeID( "#Rlt" );
desc71065.putUnitDouble( idVrtc, idRlt, -241.095108 );
var idOfst = charIDToTypeID( "Ofst" );
desc71064.putObject( idOfst, idOfst, desc71065 );
var idWdth = charIDToTypeID( "Wdth" );
var idPrc = charIDToTypeID( "#Prc" );
desc71064.putUnitDouble( idWdth, idPrc, 97.912414 );
var idHght = charIDToTypeID( "Hght" );
var idPrc = charIDToTypeID( "#Prc" );
desc71064.putUnitDouble( idHght, idPrc, 89.449139 );
var idSkew = charIDToTypeID( "Skew" );
var desc71066 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idAng = charIDToTypeID( "#Ang" );
desc71066.putUnitDouble( idHrzn, idAng, -12.818416 );
var idVrtc = charIDToTypeID( "Vrtc" );
var idAng = charIDToTypeID( "#Ang" );
desc71066.putUnitDouble( idVrtc, idAng, 0.000000 );
var idPnt = charIDToTypeID( "Pnt " );
desc71064.putObject( idSkew, idPnt, desc71066 );
var idAngl = charIDToTypeID( "Angl" );
var idAng = charIDToTypeID( "#Ang" );
desc71064.putUnitDouble( idAngl, idAng, 7.025203 );
var idUsng = charIDToTypeID( "Usng" );
var desc71067 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idPrc = charIDToTypeID( "#Prc" );
desc71067.putUnitDouble( idHrzn, idPrc, -0.009036 );
var idVrtc = charIDToTypeID( "Vrtc" );
var idPrc = charIDToTypeID( "#Prc" );
desc71067.putUnitDouble( idVrtc, idPrc, -0.064095 );
var idPnt = charIDToTypeID( "Pnt " );
desc71064.putObject( idUsng, idPnt, desc71067 );
executeAction( idTrnf, desc71064, DialogModes.NO );
Copy link to clipboard
Copied
Unfortunately, as JJ mentioned, transform is not so simple via AM code. scriptListener doesn't use the vertices to transform. It uses width, height, screw, rotation, etc, even when you try and record a simple distortion. There might be some way to reverse engineer all of this, but I don't know.

