Extending layer functions

Hello.

I have a question about extending the layer functionality via new functions. Let’s I want to set the layer opacity and blending modes and set them via the Blending Modes dialog. If I use this method below, it will always apply it to the active layer and not to the layer that I use the new function on.

So for example the new function is called like that … layer1.setLayerOptions(a, b). But when I’m on layer2 and call this function, the function is applied to layer2.

Maybe somebody can nudge me in the right direction, how this extending method could work.

Here’s the function that I created …

// LAYER - SET LAYER OPTIONS
app.Layer.prototype.setLayerOptions = async function (opacity, blendmode) {
const result = await batchPlay(
[
{
“_obj”: “set”,
“_target”: [
{
“_ref”: “layer”,
“_enum”: “ordinal”,
“_value”: “targetEnum”
}
],
“to”: {
“_obj”: “layer”,
“opacity”: {
“_unit”: “percentUnit”,
“_value”: opacity
},
“mode”: {
“_enum”: “blendMode”,
“_value”: blendmode
}
},
“_isCommand”: false,
“_options”: {
“dialogOptions”: “dontDisplay”
}
}
],{
“synchronousExecution”: false,
“modalBehavior”: “fail”
});
}

you can choose the layer id

async function blendIf(idLayer,opacity,blend){

  const batchPlay = require('photoshop').action.batchPlay 

  const result = await batchPlay( 

  [ 

  {

   "_obj": "set",

   "_target": [

    {

     "_ref": "layer",

  "_id":idLayer

    }

   ],

   "to": {

    "_obj": "layer",

    "opacity": {

     "_unit": "percentUnit",

     "_value": opacity

    },

    "mode": {

     "_enum": "blendMode",

     "_value": blend

    }

   },

   "_isCommand": true,

   "_options": {

    "dialogOptions": "display"

   }

  }

  ],{ 

  "synchronousExecution": false, 

  "modalBehavior": "fail" 

  });}
1 Like

Yeah. I know. But that isn’t an extension of the Layer object. It’s about how to extend an object properly while referring back to itself.

maybe I didn’t understand,
I thought that this could be correct

app.Layer.prototype.blendIf=async function (idLayer,opacity,blend){
const batchPlay = require(‘photoshop’).action.batchPlay
const result = await batchPlay(
[
{
“_obj”: “set”,
“_target”: [
{
“_ref”: “layer”,
“_id”:idLayer
}
],
“to”: {
“_obj”: “layer”,
“opacity”: {
“_unit”: “percentUnit”,
“_value”: opacity
},
“mode”: {
“_enum”: “blendMode”,
“_value”: blend
}
},
“_isCommand”: true,
“_options”: {
“dialogOptions”: “display”
}
}
],{
“synchronousExecution”: false,
“modalBehavior”: “fail”
});}
ex.
app.activeDocument.layers[0].blendIf(6,100,“darken”)

1 Like

Hey.

Cheers for your suggestions, but there’s a misunderstanding indeed. The thing is, that the extension of the Layer object should not need and “id” parameter as the object calls the function itself.

Take a look at this funktion >>> const layer1_copy = await layer1.duplicate()

This will duplicate “layer1” even if you are on some other layer. No need for an “id” here. I hope this makes things a bit clearer.

I found this solution. The question is … is this the most elegant/logical solution?

// LAYER - SET LAYER OPTIONS
app.Layer.prototype.setLayerOptions = async function (opacity, blendmode) {
const result = await batchPlay(
[
{
“_obj”: “set”,
“_target”: [
{
“_ref”: “layer”,
“_id”: this._id
}
],
“to”: {
“_obj”: “layer”,
“opacity”: {
“_unit”: “percentUnit”,
“_value”: opacity
},
“mode”: {
“_enum”: “blendMode”,
“_value”: blendmode
}
},
“_isCommand”: false,
“_options”: {
“dialogOptions”: “dontDisplay”
}
}
],{
“synchronousExecution”: false,
“modalBehavior”: “fail”
});
}

1 Like

You can actually get rid of const result = , since this isn’t a “getter” but a “setter”.
And a few other lines too…

// LAYER - SET LAYER OPTIONS
app.Layer.prototype.setLayerOptions = async function (opacity, blendMode) {
  await batchPlay(
    [{
      "_obj": "set",
      "_target": [{
        "_ref": "layer",
        "_id": this._id
      }],
      "to": {
        "_obj": "layer",
        "opacity": {
          "_unit": "percentUnit",
          "_value": opacity
        },
        "mode": {
          "_enum": "blendMode",
          "_value": blendMode
        }
      }
    }], {});
}

Call with
app.activeDocument.layers[0].setLayerOptions(69, "hardLight") or whatever layers[n] you want.

2 Likes