6 Replies Latest reply on Nov 4, 2015 11:04 AM by JJMack

    How to get the dimensions of the content of a smart object?

    cwodwrth Level 1

      I have a scaled smart object and I'm trying to make a calculation in my script using the bounds of the smart layer vs the bounds of the content inside the smart layer, so I can add text proportionally outside of the smart layer and have everything line up properly. Is there a good method for determining the dimensions of a smart layer's content?


      destX = myLayer.bounds[0] + origX * ((myLayer.bounds[2]-myLayer.bounds[0])/8793);

      destY = myLayer.bounds[1] + origY * ((myLayer.bounds[3]-myLayer.bounds[1])/4990);


      This is the calculation I'm using where I know the content inside the smart layer is 8793x4990, but what's the best approach to not hard code this?

        • 1. Re: How to get the dimensions of the content of a smart object?
          cbuliarca Level 3

          hi, cwodwrth


          take a look at this thread:

          Need help cleaning up a script and adding loop


          I've done a script in there that I think will do what you need.

          • 2. Re: How to get the dimensions of the content of a smart object?
            JJMack Most Valuable Participant

            Every smart object layer has a layer transform associated with it.  This is the layers current bounds.  The transform can be scaling the actual object size up or down in size.  It Also positions the layer location relative to the Canvas.  In Fact  the layer bounds can even be partly or completely off canvas.


            I do not think you need to do any thing with regard to the smart object actual size.  You just  need to deal with the layer's transform size and it location.  Adding text outside the smart object layer may be adding text outside the document canvas size.   If you add canvas to house the text you may also be revealing pixels that were clipped off by the prior canvas size.  Layers can be larger than canvas size and extent outsize of the canvas size.


            Text Size seem to be a strange beast to me.  The only way I have ever been able  figure out a text size to use is the know how many letters must fit within  some number of pixels wide.  I then save the document current resolution and change the document resolution to 72 DPI and use a fontsize  calculated using

            (Width Pixels) / (Number of Letters) = Fontsize  Add the text layer then change the document resolution back to its original resolution.  It seems that Points and Pica are 72 px units. The Text layer can be aligned to the smart object layer by loading the smart object layer bounds as a selection and aligning the text layer to the selection You have nine area in the selection can align to. You would align to the top or bottom of the layers bound then mover the layer  up or down the fontsize  to  position the  text layer outside the smart objects layer mounds.  That could move the text off canvas.

            • 3. Re: How to get the dimensions of the content of a smart object?
              cwodwrth Level 1

              I'm still struggling with this issue in another application. I really need a way to determine the original size the content held in a smart object. I'm assuming you can get this by looking at the Smart Object's transform data, but I'm not quite sure how to do this. Anyone out there have experience with this?

              • 4. Re: How to get the dimensions of the content of a smart object?
                JJMack Most Valuable Participant

                Resize the smart object layer to 100% size and then get the layers bounds. 


                Over the release pf Photoshop there has been errors in Photoshop scripting.  Place would fail in a script if the current Layer item being targeted was the layers layer mask in some versions of Photoshop like Cs2 and CS4.  It would not fail in CS3 or CS5. It should not matter which layer item was targeted. For new layer are added above the currently targeted layer and the new layer becomes the current Photoshop target. Anyway I added code to target the RGB channel in my scripts to avoid Adobe bug before doing a Place. And after doing a place I would resize the new current layer to 100% size in case Photoshop place operation scaled the placed smart object layer. The code is Action manager code so not very readable.


                function placeImage(file) {

                  // =======avoid bug in cs2 and  CS4 ========Targtet RGB======================

                  var idslct = charIDToTypeID( "slct" );

                     var desc5 = new ActionDescriptor();

                     var idnull = charIDToTypeID( "null" );

                         var ref3 = new ActionReference();

                         var idChnl = charIDToTypeID( "Chnl" );

                         var idChnl = charIDToTypeID( "Chnl" );

                         var idRGB = charIDToTypeID( "RGB " );

                         ref3.putEnumerated( idChnl, idChnl, idRGB );

                     desc5.putReference( idnull, ref3 );

                     var idMkVs = charIDToTypeID( "MkVs" );

                     desc5.putBoolean( idMkVs, false );

                  executeAction( idslct, desc5, DialogModes.NO );

                  // Place in the file

                  var idPlc = charIDToTypeID( "Plc " );

                     var desc5 = new ActionDescriptor();

                     var idnull = charIDToTypeID( "null" );

                     desc5.putPath( idnull, new File( file ) );

                     var idFTcs = charIDToTypeID( "FTcs" );

                     var idQCSt = charIDToTypeID( "QCSt" );

                     var idQcsa = charIDToTypeID( "Qcsa" );

                     desc5.putEnumerated( idFTcs, idQCSt, idQcsa );

                     var idOfst = charIDToTypeID( "Ofst" );

                         var desc6 = new ActionDescriptor();

                         var idHrzn = charIDToTypeID( "Hrzn" );

                         var idPxl = charIDToTypeID( "#Pxl" );

                         desc6.putUnitDouble( idHrzn, idPxl, 0.000000 );

                         var idVrtc = charIDToTypeID( "Vrtc" );

                         var idPxl = charIDToTypeID( "#Pxl" );

                         desc6.putUnitDouble( idVrtc, idPxl, 0.000000 );

                     var idOfst = charIDToTypeID( "Ofst" );

                     desc5.putObject( idOfst, idOfst, desc6 );

                  executeAction( idPlc, desc5, DialogModes.NO );

                  // because can't get the scale of a smart object, reset to 100%

                  activeDocument.activeLayer.resize(100 ,100,AnchorPosition.MIDDLECENTER);

                  return app.activeDocument.activeLayer;


                • 5. Re: How to get the dimensions of the content of a smart object?
                  cwodwrth Level 1

                  Hmm, is there not a way to determine the current scale % width/height of a smart object without actually resizing it? With the image sizes I'm using scaling it to 100% then back down to original % make runtimes significantly longer.

                  • 6. Re: How to get the dimensions of the content of a smart object?
                    JJMack Most Valuable Participant

                    I was not able to find a way.  But I' do not know all that is possible with Action Manager Code.


                    The current scale is easy to see in Photoshop UI after  you do a place. The scale percent is shown in the option bar that is showing the transform associated the smart object layer created during Place Image.  I do not know how to or even if it is possible to retrieve that scale value via scripting.


                    Photoshop does display the scale in its UI. If scaling was done during place the width and height scale will be the same and less than 100%.   If Scaling was not done both values will be 100%.


                    Photoshop only scale placed layers that do not fit within the current document canvas size and the users Photoshop preference is set to scale placed objects during place is set.   My code insures that the smart object layer will be the layers actual 100% scale size when my script uses place no matter how the user preferences is set.


                    When you scale a smart object layer you need to scale it relative to the object's size to scale the object to the size you want.  You will also run into scaling problems when object contain borders that are transparent.   I normally address that by eliminating that condition in my saved image file the have transparency. I will add a pixel to the top left canvas bound and the bottom right canvas bound and set their opacity to 1%. OR I will trim the image transparency before saving the image file.  A Transparent border can cause alignment and sizing complications.