Skip to main content
Inspiring
December 3, 2011
Question

How do i flip a layer in vertical direction through Coding?

  • December 3, 2011
  • 1 reply
  • 1029 views

Hi,

     I would like to make a mirror effect for the foreground. so i have to flip a layer in vertical direction, by move the anchor position to the bottom center.

Could you please help me on this?

thanks,

rajiv.s

This topic has been closed for replies.

1 reply

Paul Riggott
Inspiring
December 3, 2011

Here's one method...

flipLayer('v');
//flipLayer('h');

function flipLayer(direction) {
switch (direction.toLowerCase()){
    case 'h' : direction = 'Hrzn'; break;
    case 'v' : direction = 'Vrtc'; break;
    default : return;
    }
    var desc23 = new ActionDescriptor();
    desc23.putEnumerated( charIDToTypeID('Axis'), charIDToTypeID('Ornt'), charIDToTypeID(direction) );
try{
    executeAction( charIDToTypeID('Flip'), desc23, DialogModes.NO );
}catch(e){}
};

Inspiring
December 3, 2011

Hi Paul,

     It's working well. but I want to create a mirror effect for the foreground.

Could you please check the script one more time with any image?

Is there possible to move the center point into bottom center through coding?

thanks,

rajiv.s

Inspiring
December 3, 2011

If I recall correctly both me & mike had a play about with a similar request over at PS Scripts… There may be something in this that you can utilise… In my case it was just for pretty much square on product stots…

#target photoshop

app.bringToFront();

var userRulerUnits = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS;

var whiteRef = new SolidColor();

whiteRef.rgb.red = 255;

whiteRef.rgb.green = 255;

whiteRef.rgb.blue = 255;

var blackRef = new SolidColor();

blackRef.rgb.red = 0;

blackRef.rgb.green = 0;

blackRef.rgb.blue = 0;

app.backgroundColor = blackRef;

app.foregroundColor = whiteRef;

var docRef = app.activeDocument;

if (docRef.layers[0].isBackgroundLayer == false) {

                    if (docRef.layers[0].allLocked == true) docRef.layers[0].allLocked = false;

                    //if (docRef.layers[0].pixelsLocked == true) docRef.layers[0].pixelsLocked = false;

                    //if (docRef.layers[0].positionLocked == true) docRef.layers[0].positionLocked = false;

                    docRef.trim(TrimType.TRANSPARENT, true, true, true, true);

                    docWidth = docRef.width;

                    docHeight = docRef.height;

                    docRef.layers[0].duplicate();

                    docRef.resizeCanvas(docWidth, (docHeight * 2), AnchorPosition.TOPCENTER);

                    docRef.layers[1].translate(0, docHeight);

                    FlipLayer('Vrtc');

                    docRef.channels.add();

                    DrawGradient(docWidth*.5, docRef.height, docWidth*.5, docRef.height*.5, 50); //Adjust to tast

                    docRef.activeChannels = docRef.componentChannels;    

                    docRef.resizeCanvas(docWidth + 50, (docHeight * 2) + 50, AnchorPosition.MIDDLECENTER);

                    docRef.layers[1].opacity = 75;

                    docRef.layers[1].blendMode = BlendMode.SCREEN;

                    docRef.selection.load(docRef.channels.getByName('Alpha 1'));

                    MakeLayerMask('RvlS');

                    docRef.channels.getByName('Alpha 1').remove();

                    SolidFillLayer(0, 0, 0);

                    docRef.layers[1].move(docRef.layers[2], ElementPlacement.PLACEAFTER);

}

app.preferences.rulerUnits = userRulerUnits;

function FlipLayer(direction) {

          // direction = "Vrtc" or "Hrzn"

          function cTID(s) { return app.charIDToTypeID(s); };

          function sTID(s) { return app.stringIDToTypeID(s); };

                    var desc01 = new ActionDescriptor();

                    var ref01 = new ActionReference(); ref01.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt') );

                              desc01.putReference(cTID('null'), ref01);

                              desc01.putEnumerated(cTID('Axis'), cTID('Ornt'), cTID(direction));

                    executeAction(cTID('Flip'), desc01, DialogModes.NO);

}

function MakeLayerMask(maskRef) {

          function cTID(s) { return app.charIDToTypeID(s); };

          function sTID(s) { return app.stringIDToTypeID(s); };

          // maskRef = RvlA, HdAl, RvlS, HdSl

                    var desc01 = new ActionDescriptor();

                              desc01.putClass(cTID('Nw  '), cTID('Chnl'));

                    var ref01 = new ActionReference();

                              ref01.putEnumerated(cTID('Chnl'), cTID('Chnl'), cTID('Msk '));

                              desc01.putReference(cTID('At  '), ref01 );

                              desc01.putEnumerated(cTID('Usng'), cTID('UsrM'), cTID(maskRef));

                    executeAction(cTID('Mk  '), desc01, DialogModes.NO);

}

function DrawGradient(x1, y1, x2, y2, midPoint) {

          function cTID(s) { return app.charIDToTypeID(s); };

          function sTID(s) { return app.stringIDToTypeID(s); };

                    var desc01 = new ActionDescriptor();

                    var desc02 = new ActionDescriptor();

                              desc02.putUnitDouble( cTID('Hrzn'), cTID('#Pxl'), x1 );

                              desc02.putUnitDouble( cTID('Vrtc'), cTID('#Pxl'), y1 );

                              desc01.putObject( cTID('From'), cTID('Pnt '), desc02 );

                    var desc03 = new ActionDescriptor();

                              desc03.putUnitDouble( cTID('Hrzn'), cTID('#Pxl'), x2 );

                              desc03.putUnitDouble( cTID('Vrtc'), cTID('#Pxl'), y2 );

                              desc01.putObject( cTID('T   '), cTID('Pnt '), desc03 );

                              desc01.putEnumerated( cTID('Type'), cTID('GrdT'), cTID('Lnr ') );

                              desc01.putBoolean( cTID('Dthr'), true );

                              desc01.putBoolean( cTID('UsMs'), true );

                    var desc04 = new ActionDescriptor();

                              desc04.putString( cTID('Nm  '), "Foreground to Background" );

                              desc04.putEnumerated( cTID('GrdF'), cTID('GrdF'), cTID('CstS') );

                              desc04.putDouble( cTID('Intr'), 4096.000000 );

                    var list15 = new ActionList();

                    var desc05 = new ActionDescriptor();

                              desc05.putEnumerated( cTID('Type'), cTID('Clry'), cTID('FrgC') );

                              desc05.putInteger( cTID('Lctn'), 0 );

                              desc05.putInteger( cTID('Mdpn'), midPoint );

                              list15.putObject( cTID('Clrt'), desc05 );

                    var desc06 = new ActionDescriptor();

                              desc06.putEnumerated( cTID('Type'), cTID('Clry'), cTID('BckC') );

                              desc06.putInteger( cTID('Lctn'), 4096 );

                              desc06.putInteger( cTID('Mdpn'), midPoint );

                              list15.putObject( cTID('Clrt'), desc06 );

                              desc04.putList( cTID('Clrs'), list15 );

                    var list16 = new ActionList();

                    var desc07 = new ActionDescriptor();

                              desc07.putUnitDouble( cTID('Opct'), cTID('#Prc'), 100.000000 );

                              desc07.putInteger( cTID('Lctn'), 0 );

                              desc07.putInteger( cTID('Mdpn'), 50 );

                              list16.putObject( cTID('TrnS'), desc07 );

                    var desc08 = new ActionDescriptor();

                              desc08.putUnitDouble( cTID('Opct'), cTID('#Prc'), 100.000000 );

                              desc08.putInteger( cTID('Lctn'), 4096 );

                              desc08.putInteger( cTID('Mdpn'), midPoint );

                              list16.putObject( cTID('TrnS'), desc08 );

                              desc04.putList( cTID('Trns'), list16 );

                              desc01.putObject( cTID('Grad'), cTID('Grdn'), desc04 );

                    executeAction( cTID('Grdn'), desc01, DialogModes.NO );

};

function SolidFillLayer(r, g, b) {

          function cTID(s) { return app.charIDToTypeID(s); };

          function sTID(s) { return app.stringIDToTypeID(s); };

                    var desc616 = new ActionDescriptor();

                    var ref58 = new ActionReference();

                              ref58.putClass( sTID('contentLayer') );

                              desc616.putReference( cTID('null'), ref58 );

                    var desc617 = new ActionDescriptor();

                    var desc618 = new ActionDescriptor();

                    var desc619 = new ActionDescriptor();

                              desc619.putDouble( cTID('Rd  '), r );

                              desc619.putDouble( cTID('Grn '), g );

                              desc619.putDouble( cTID('Bl  '), b );

                              desc618.putObject( cTID('Clr '), cTID('RGBC'), desc619 );

                              desc617.putObject( cTID('Type'), sTID('solidColorLayer'), desc618 );

                              desc616.putObject( cTID('Usng'), sTID('contentLayer'), desc617 );

                    executeAction( cTID('Mk  '), desc616, DialogModes.NO );

};