How can I capture a smart object's original dimension via scripts?

The Setting

I have a document with a canvas size of 1920x1080 and a large number of layers. Each layer is a smart object which I've placed by dragging and dropping the contents of a folder onto the canvas. No two layers have the same dimensions. Some of the smart objects are larger than the canvas and photoshop is automatically shrinking them down to fit.

The Goal

I want all my smart objects to be their original dimensions.

I want to avoid going to each object, transforming, clicking the "link dimensions" button, manually typing "100%", and placing the transform.

The Problem

First, I tried creating an action to select the layer > transform width and height to 100% > select next layer. Next, I tried creating a script to go through each layer and resize() to 100%. Both attempts were foiled by the same problem, the resize function is relative to the object's current dimensions.

for example

When creating an action, I recorded my actions and by manually typing in "100%" for the smart object transform. The smart object resized to its original dimensions. Perfect, this is what I want. However, when I drilled into the action it had actually resized 405%. I typed in "100%" but it recorded "405%" making this action unusable unless the layers were all the same exact size.

My Questions

How can I capture a smart object's original dimensions?

Is there a better way to go about this?

---------

Cheers!

---------

My Current Script

var doc = app.activeDocument;

var allLayers = [];

var allLayers = collectAllLayers(doc, allLayers);

function collectAllLayers (doc, allLayers){

    for (var m = 0; m < doc.layers.length; m++){

        var theLayer = doc.layers;

        if (theLayer.typename === "ArtLayer"){

            allLayers.push(theLayer);

        }else{

            collectAllLayers(theLayer, allLayers);

        }

    }

    return allLayers;

}

for(i = (allLayers.length-1); i >= 0; i--){

//resize smart object to original dimension

};

View all replies

Views

164

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn More

How can I capture a smart object's original dimension via scripts?

The Setting

I have a document with a canvas size of 1920x1080 and a large number of layers. Each layer is a smart object which I've placed by dragging and dropping the contents of a folder onto the canvas. No two layers have the same dimensions. Some of the smart objects are larger than the canvas and photoshop is automatically shrinking them down to fit.

The Goal

I want all my smart objects to be their original dimensions.

I want to avoid going to each object, transforming, clicking the "link dimensions" button, manually typing "100%", and placing the transform.

The Problem

First, I tried creating an action to select the layer > transform width and height to 100% > select next layer. Next, I tried creating a script to go through each layer and resize() to 100%. Both attempts were foiled by the same problem, the resize function is relative to the object's current dimensions.

for example

When creating an action, I recorded my actions and by manually typing in "100%" for the smart object transform. The smart object resized to its original dimensions. Perfect, this is what I want. However, when I drilled into the action it had actually resized 405%. I typed in "100%" but it recorded "405%" making this action unusable unless the layers were all the same exact size.

My Questions

How can I capture a smart object's original dimensions?

Is there a better way to go about this?

---------

Cheers!

---------

My Current Script

var doc = app.activeDocument;

var allLayers = [];

var allLayers = collectAllLayers(doc, allLayers);

function collectAllLayers (doc, allLayers){

    for (var m = 0; m < doc.layers.length; m++){

        var theLayer = doc.layers;

        if (theLayer.typename === "ArtLayer"){

            allLayers.push(theLayer);

        }else{

            collectAllLayers(theLayer, allLayers);

        }

    }

    return allLayers;

}

for(i = (allLayers.length-1); i >= 0; i--){

//resize smart object to original dimension

};

View all replies

Views

165

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn More

Thanks JJMack for pointing me in the right direction. I now realize this should be in the Photoshop Scripts forum. If anyone knows how to move it over let me know.

-------

I figured it out. This will resize all smart object to their original dimensions within a document, including smart objects nested in groups. I'm not much of a developer so my code is neither beautiful nor efficient. For anyone curious:

var doc = app.activeDocument;

function resizeSmartObject() {

//target current active layer

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );   

//check the type of layer

//1 = ArtLayer

//2 = Adjustment

//3 = Text

//4 = Shape

//5 = Smart Object

// ...

//9 = Gradient Fil

//10 = Pattern Fill

//11 = Solid Fill

var layerType = executeActionGet(ref).getInteger(stringIDToTypeID("layerKind"));

//if layer is a smart object, get diminensions of source

if (layerType == "5") {

    var obj = executeActionGet(ref).getObjectValue(stringIDToTypeID("smartObjectMore"));   

    with (obj) { 

        var _tmp = getObjectValue(stringIDToTypeID("size")); 

        var size = new Object({ 

            width:  _tmp.getDouble(stringIDToTypeID("width")), 

            height: _tmp.getDouble(stringIDToTypeID("height")), 

        }); 

    }

    //divide the original/current

    var bounds = app.activeDocument.activeLayer.bounds; 

    var w_new = (size.width/(bounds[2].value - bounds[0].value))*100;

    var h_new = (size.height/(bounds[3].value - bounds[1].value))*100;

    app.activeDocument.activeLayer.resize(w_new, h_new);

    return;

} else { return; }

}

function cycleLayers (set){

     for (var i = 0; i < set.layers.length; i++){

        doc.activeLayer = set.layers;

        if(doc.activeLayer.typename == 'LayerSet'){

            if(doc.activeLayer.layers.length > 0) {

                cycleLayers(doc.activeLayer);

            }

        } else {

            resizeSmartObject();

        }

     }

}

cycleLayers(doc);

2 Replies

Re: How can I capture a smart object's original dimension via scripts?

Most Valuable Participant, Mar 02, 2018

GOTO the photoshop scripting forum r-bin pos%ed some script code that works in cc 2018 that can be used to find that information

JJMack
View all replies

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn More
Reply
Loading...

Re: How can I capture a smart object's original dimension via scripts?

Thanks JJMack for pointing me in the right direction. I now realize this should be in the Photoshop Scripts forum. If anyone knows how to move it over let me know.

-------

I figured it out. This will resize all smart object to their original dimensions within a document, including smart objects nested in groups. I'm not much of a developer so my code is neither beautiful nor efficient. For anyone curious:

var doc = app.activeDocument;

function resizeSmartObject() {

//target current active layer

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );   

//check the type of layer

//1 = ArtLayer

//2 = Adjustment

//3 = Text

//4 = Shape

//5 = Smart Object

// ...

//9 = Gradient Fil

//10 = Pattern Fill

//11 = Solid Fill

var layerType = executeActionGet(ref).getInteger(stringIDToTypeID("layerKind"));

//if layer is a smart object, get diminensions of source

if (layerType == "5") {

    var obj = executeActionGet(ref).getObjectValue(stringIDToTypeID("smartObjectMore"));   

    with (obj) { 

        var _tmp = getObjectValue(stringIDToTypeID("size")); 

        var size = new Object({ 

            width:  _tmp.getDouble(stringIDToTypeID("width")), 

            height: _tmp.getDouble(stringIDToTypeID("height")), 

        }); 

    }

    //divide the original/current

    var bounds = app.activeDocument.activeLayer.bounds; 

    var w_new = (size.width/(bounds[2].value - bounds[0].value))*100;

    var h_new = (size.height/(bounds[3].value - bounds[1].value))*100;

    app.activeDocument.activeLayer.resize(w_new, h_new);

    return;

} else { return; }

}

function cycleLayers (set){

     for (var i = 0; i < set.layers.length; i++){

        doc.activeLayer = set.layers;

        if(doc.activeLayer.typename == 'LayerSet'){

            if(doc.activeLayer.layers.length > 0) {

                cycleLayers(doc.activeLayer);

            }

        } else {

            resizeSmartObject();

        }

     }

}

cycleLayers(doc);

View all replies

Likes

1 Like

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn More
Reply
Loading...