Skip to main content
markk9596665
Participating Frequently
May 20, 2015
Answered

How can I retrieve the transform information on a layer

  • May 20, 2015
  • 6 replies
  • 9644 views

I am trying to retrieve the transformation information on a layer through script (for a smart object if it matters).

 

I've been able to do it if there is a text layer attached, but I need to be able to do it with just the smart object.

 

This is what I have so far:

var idnull = charIDToTypeID("null");
var layerReference = new ActionReference();
layerReference.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var layerDescriptor = new ActionDescriptor();
layerDescriptor.putReference(idnull, layerReference);
layerDescriptor.putEnumerated(charIDToTypeID("FTcs"), charIDToTypeID("QCSt"), charIDToTypeID("Qcsa"));
var transformDescriptor = app.executeAction(charIDToTypeID("Trnf"), layerDescriptor, DialogModes.NO);
var offset= transformDescriptor.getObjectValue(charIDToTypeID("Ofst"));
var horizontalOffset = offset.getUnitDoubleValue(charIDToTypeID("Hrzn"));
var verticalOffset = offset.getUnitDoubleValue(charIDToTypeID("Vrtc"));

 

However, horizontalOffset and verticalOffset are both returned as zero.

 

Thanks in advance, Mark.

This topic has been closed for replies.
Correct answer c.pfaffenbichler

c.pfaffenbichler wrote:

I'd really just like to pull out the existing transformations applied to the layer.

I’m confident that information is not stored with the Layer, not even with a Smart Object, because the Layer’s position at some previous time is generally irrelevant.

Rotation, Scale, Skew, … are different for a Smart Object naturally – unfortunately they can not be directly retrieved as fas as I know.

And Type Layers are special cases anyhow.

However, if I open the free transform tool on the smart object, the current transformations (e.g., x, y, rotational angle, scale, etc) are all available, so they must be stored somewhere associated with that smart object.

Basically I need to replace the smart object with the layers in the smart object and transform them in such a way that they match the transformations applied to the smart object.  I want to actually extract the layers, and not just edit the linked SO.


if I open the free transform tool on the smart object, the current transformations (e.g., x, y, rotational angle, scale, etc)

But x and y are not related to the offset of a previous transformation, but the current position.

And as I already mentioned this information is not Script-accessible to the best of my knowledge (and others have tried, too).

Basically I need to replace the smart object with the layers in the smart object and transform them in such a way that they match the transformations applied to the smart object.  I want to actually extract the layers, and not just edit the linked SO.

I think you are going about this less than ideally, especially as Warps, perspectival transformations etc. might be difficult to evaluate.

One can use "New Smart Object via Copy" instead, open the SO and hide all but one Layer, save, rasterize, repeat for the next Layer etc.

I even attempted a Script on that basis once, but Groups, Layer Masks on Groups and Adjustment Layers, Clipping Masks etc. proved a bit difficult to handle …

6 replies

NB, colourmanagement
Community Expert
Community Expert
February 1, 2020

Hi

 

in any case, it seems its worth pointing out that once scaled, bitmap pixels are gone - so rescaling back to the original size is not loss-less

 

thanks

neil barstow, colourmanagement.net

[please do not use the reply button on a message in the thread, only use the one at the top of the page, to maintain chronological order]

schroef
Inspiring
February 6, 2020

Well examples show using a smartObject, not even sure what you mean by "Bitmap pixels are gone"???

Participating Frequently
April 20, 2018

from cc 2015, we can get 4 points coordinates of smartobject layer

1. then we can calculate the angle

2. we can calculate the size of current smartobject layer,  we can also get the original size of smartobject by action manager, so "current size/original size" is the scale

ExtendScript-Helper/smartobject.jsx at master · songgeb/ExtendScript-Helper · GitHub

JJMack
Community Expert
Community Expert
April 20, 2018

There are problem with that script's function    getSmartObjectScale  getgetSmartObjectScaleSmartObjectScale getSmartObjectScale  getSmartObjectScale.

function getSmartObjectScale (layer) {

    if (layer.kind != LayerKind.SMARTOBJECT) {alert("layer is not smart object!"); return null;}

     var originalActiveLayer = app.activeDocument.activeLayer;

     app.activeDocument.activeLayer = layer;

     //获取智能对象当前尺寸

     var originalSize = getSmartObjectSize (layer);

    //进入智能对象编辑页面,获取智能对象原尺寸

    var idplacedLayerEditContents = stringIDToTypeID( "placedLayerEditContents" );

    var desc1900 = new ActionDescriptor();

    executeAction( idplacedLayerEditContents, desc1900, DialogModes.NO );//进入对象编辑页面

    //获取文档大小

    var width = app.activeDocument.width.value;

    var height = app.activeDocument.height.value;

    app.activeDocument.close (SaveOptions.DONOTSAVECHANGES);

    app.activeDocument.activeLayer = originalActiveLayer;

   

    return {"width": originalSize.width/width, "height": originalSize.height/height};

}

All smart object layers are not the same. There are different type of objects.  That script function opens the active layer object "placedLayerEditContents" that is a problem.  All object will not be opened by Photoshop.  A placed image file may not open in Photoshop.  My test document had a Placed RAW file when the script function open the object  I was then in ACR when I click OK the function closed the current document in Photoshop and there was no open Document in Photoshop.  Likewise with a vector smart object layer a placed .svg file the SVG was opend in Internet Eplorer on my machine and when I closed IE the function close the current document and nothing was open in Photoshop.

When my smart object layer was a placed Jpeg file the function  getSmartObjectScale   would only get the scale correct when the Documents DPI and the Jpeg same the same DPI.

I added thie alert to your script so you can see the scale isn not correct.

alert("scale " + scale.width + " " + scale.height + "\n"

     + "size " + size.width + " " + size.height + "\n"

     + "corners " + corners + "\n"

     + "bounds " + bounds + "\n"

     + "angle " + angle);

Here is a screen capture shows the Placed jpeg place transform in the tool option bar then I ran your script and then an other script like your that rounds values gets the scaling right and it works with all object types for it does not open the objects. Vector Smart Opbects have not size.  Devision by 0 is infinity so theat script shows infinity as yje scale value not what the Place transfom shows.  Photoshop must set some size for Places .svg and .ai files.  I was unable to figure out how Photoshop handled placed vector files like .svg and .ai files.

A Placed SVG file Follows

JJMack
Participating Frequently
April 23, 2018

Thanks for your reply!

PS can not open svg smart object layer. But we can export the original svg file by action manager. Then open it again by ps. So we can get its size.

var idplacedLayerExportContents = stringIDToTypeID( "placedLayerExportContents" );

var desc3027 = new ActionDescriptor();

var idnull = charIDToTypeID( "null" );

desc3027.putPath( idnull, new File( "/Users/admin/xxx.svg" ) );

executeAction( idplacedLayerExportContents, desc3027, DialogModes.NO );

JJMack
Community Expert
Community Expert
April 18, 2018

Have you been able to do it?  Using what r-bin posted for a smart object layer if it is rectangular you should be able to get the object's width and height scaling and angle of rotation, If it been distorted with some perspective or warp I think it very hard or impossible to deal with.  Even it is rectangular I do not know if it would be easy to determine if it has been flipped vertically or horizontally.

Photoshop also does some scaling when the object resolution is not the same as the document resolution. I have been able to figured  exactly what Photoshop does whet there is a resolution mismatch.   After I place in image in a script I always  the smart object layers associated transform to 100% width and height. so I know the object size using layer bounds. and know it the object is it full size and work from there. I know I can resized it the way I need to then.

By look at the four corners x y in order you can tell if the layer has been flipped. However the layer can be flipped horizontally or vertically also be rotated and the object and documents can have different resolutions.  Telling what is what  is complex.

JJMack
JJMack
Community Expert
Community Expert
April 19, 2018

Here an Smart Object image layer with no rotation straight up portrait and not flipped,   then flipped horizontally, then flipped vertically, and then flipped  horizontally and  vertically.  Screen Capture shows the Smart object layers Transform setting in the tool option bar.  Also screen captures that show Information a script can retrieved using the action manager code r-bin posted for retrieving the objects dour corners and some of the additional smart object information available. By comparing the x,y corner points positions relative to each other in order one would be able to figure out if any flipping was done. However as shown in my previous append throwing in rotation may make it much harder rotation 90, 180, 270 may look like flipping or 180 may look lo 0 and 270 look like 90.

JJMack
Participant
April 18, 2018

Hi. Have read many threads about this. But haven't find any solution. So do you have any luck with your issue?

I have a smartObject which is flipped horizontally. And I need get a current width value. If it have "-" smartObject is flipped.

Can anyone help me with this?

Thanks in advance.

Participant
June 8, 2015

Oh man, I know your frustration.
I'm having the similar woes. However, I'm not looking to apply the information to another layer, but just read the data, for use in other ways.
I also, didnt expect to run into this problem, as when CTRL+T is used on a smart object, the relative scale and angle are visible in the "INFO" window. I figured it was an easy win to get the transforms. I was wrong.
I find myself lost in TypeID's and unclear class structures for smartObjects.  I just havent found any good reference for "getting" the property value, only ways of setting  it, sadly.

If you or anyone else are successful, I'd be very interested in a  solution also, likewise, if I stumble into it, I'll post it.

Inspiring
June 25, 2015

I've made a script that somehow took the informations from the Smart Layer's transforms,

you can check this post: Need help cleaning up a script and adding loop, there is a reply there explaining what the script is doing, till now is the only way I found to get these values without reading the binary of the psd.

Participant
June 25, 2015

thanx it worked for me!!

c.pfaffenbichler
Community Expert
Community Expert
May 20, 2015

Under which circumstances exactly does this provide the result you intend (what does "attached text layer" mean)? Could you post a screenshot with the Layers Panel visible?

Once a Layer has been moved that information may be in the History but as far as I can tell no space is wasted on storing that information with the Layer itself.

markk9596665
Participating Frequently
May 20, 2015

Sorry, here's some clarification.  If I use code found here How to extract layer rotation (transform) in jsx photoshop script? - Stack Overflow for retrieving the transform of the text layer "PixelSquid Diving Helmet" it will work.  What I'm trying to do is get the transform that's applied to the layer DivingHelmet.H03, basically the data that is available:

I'd like the information on the existing transformations applied to the object, not trying to apply a new transformation to it.

Thanks again,

Mark

c.pfaffenbichler
Community Expert
Community Expert
May 20, 2015

Maybe you could elaborate on the process and why that information is important?


If you change the line

var transformDescriptor = app.executeAction(charIDToTypeID("Trnf"), layerDescriptor, DialogModes.NO);

to

var transformDescriptor = app.executeAction(charIDToTypeID("Trnf"), layerDescriptor, DialogModes.ALL);

and perform a transformation you get the values.