4

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.

  • did you try recording all the Photoshop transformations into a sequence of actions? and extract the transformation matrix from it ? – hsawires Dec 24 '15 at 18:30
  • @hsawires Unfortunately I'm working with an existing PSD which I didn't create. – Robert Dec 24 '15 at 18:50
1

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.

  • I suspect that Photoshop probably stores the text layer transform differently to the smart object transform, because PS restricts the types of transforms you can make to text (no distort or perspective) and the PSD specification seems to reference 2D matrices for text layers. Smart objects can be transformed in other ways and they require 3D matrices. I don't know how PS internally stores transformed smart objects, but I think my best bet is probably finding the coordinates of each of the four corners instead and generating the matrix myself. Unfortunately I can't find how to do this either! – Robert Dec 24 '15 at 20:22
  • @Robert agreed but i didnt have much time on this. 3 by 3 should be sufficient for a plane – joojaa Dec 24 '15 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.