Mobile app version of vmapp.org
Login or Join
Gretchen549

: How can I extract a layer's transformation matrix in Photoshop? I have a smart object in Photoshop CC which has been carefully distorted using Photoshop's 'Transform → Distort' function. Photoshop

@Gretchen549

Posted in: #AdobePhotoshop #Css #Transform

I have a smart object in Photoshop CC which has been carefully distorted using Photoshop's 'Transform → Distort' function. Photoshop presumably manages a transformation matrix for this layer, and I'm wanting to access the six(?) numeric values for it.

Some values (such as translation, scale and rotation) are given in the transform bar at the top of the screen when the layer is being transformed, but not enough to form a matrix, unfortunately. I've tried to access the matrix values through AppleScript, but Photoshop doesn't seem to provide the relevant information.

The reason I'm trying to do this is so that I can recreate the transformation using CSS's transform property with the matrix function. For that, I presumably need the matrix which Photoshop stores for the layer.

Edit 1: Alternatively, how can I get the precise coordinates of each corner of the smart object? I can generate the transformation matrix myself if I have those coordinates.

Edit 2: The types of transforms Photoshop allows for smart objects means that it needs to be represented by a 3D transform matrix, so if Photoshop does internally use a matrix, then it'll be a 4x4 one. I've been looking through the PSD spec to see if I can extract the values from it manually, or else find the coordinates of the corners, but I can't see where it's stored. I suspect my best bet is finding the coordinates of corners and generating the matrix myself; I've been eyeballing it and it's working relatively well, but it would be nice to have the precise coordinates.

Edit 3: Incase this helps anybody, I've just noticed that if the smart object is in transform mode (Edit → Free Transform), guides will snap to the corners if you drag them in from the ruler, showing their position. Unfortunately they only seem to snap to 0.1 pixel increments even though it looks like the corners can be more finely controlled, but at least it's faster (and somewhat more accurate) than just guessing.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Gretchen549

1 Comments

Sorted by latest first Latest Oldest Best

 

@Kimberly620

Normal layers Photoshop does not  manage transformation matrices. Once you commit the transformation to the layer gets rasterized and applied. Same seems to apply to shape layers. So there is simply nothing to recover in the common case.

Smart objects and text do have this info. This info is not easily available in scripting api. It is available in the SDK as a 3x3 matrix but that's pretty hardcore. Here is how you can get the upper two by two matrix. Used this stack overflow post by psycho brm post as guide. It only works for text layers at the moment (and layer needs to have rotation scale or skew).
#target photoshop
var doc = app.activeDocument;

alert(getTransformMatrix2by2())

function getTransformMatrix2by2()
{
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref).getObjectValue(stringIDToTypeID('textKey'));
if (desc.hasKey(stringIDToTypeID('transform')))
{
var matrix = [['xx','xy'],['yx','yy']];
desc = desc.getObjectValue(stringIDToTypeID('transform'));
for (var i=0;i < matrix.length;i++){
var row = matrix[i];
for (var j=0;j < row.length;j++){
matrix[i][j] = desc.getDouble(stringIDToTypeID(matrix[i][j]));
}
}
return matrix;
}
return undefined;
}


Now you should be able to decompose that.

Edit: Apparently the translations are ids 720-721 or names tx and ty but i keep getting 0 for them.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme