Setting minimum brush opacity

At least that’s what I did in my plugin following Simons advice in the other topic :slight_smile:

@Karmalakas is right :slight_smile: The code I post isn’t always for direct copy/paste. I try to show the relevant parts to understand the concept. So sometimes it might include some getters or helpers, which aren’t directly part of the solution or can be easily replicated. As @Karmalakas pointed out, you already had a function to get the app property, so I just left that in there. Just in case, I’ll post it here, too:

function getAppProperty<T extends keyof ApplicationDescriptor>(_property: T): ApplicationDescriptor[T] {
  return photoshop.action.batchPlay([
    {
      _obj: 'get',
      _target: [{ _property}, { _ref: 'application', _enum: 'ordinal', _value: 'targetEnum' }],
    },
  ], { synchronousExecution: true })[0][_property]
}

(the types can be removed again, I’m just too lazy when pasting it here :sweat_smile:)

PaintBrushToolOptionsDescriptor is just the name I gave to that type, but you could also just leave it out and write
const toolOpt = getAppProperty('currentToolOptions'). It’s just helping me to not make any mistakes while navigating through those nested objects. For example if you’d forget that minimum is not a number, but a percent value, you’d get the following error:
image

1 Like

Cheers. I’ll give it a try.

And I also didn’t have any place to look that up. As @Karmalakas said, batchPlay is a guessing game with a lot of trial and error.

The syntax for setting properties is always quite similar. Something like

{
    _obj: 'set',
    _target: ...,
    to: ...
}

The tricky part is that you don’t always know what to set it to. Some setters allow you to set just a subset (for example just setting the font size of a textLayer) of the property that you want to update. But to be on the safe side, you can also just get the full property, change a value and then set the whole property.

The target also has some varying syntax… I also didn’t fully understand the pattern yet, but there are essentially two ways of targeting things.

1 Like

Hi @all.

I have tried to use the code you have provided here, but I am not clever enough :wink:

If someone would provide a complete function to set the opacity I would be deeply grateful.

After a lot of trial and error and taking bits from Alchemist and Simon’s suggestions , I got it to work. This is tricky as hell. It’s obvious that this part needs documentation in some form.

Here’s the complete code …

const batchPlay = require("photoshop").action.batchPlay;

// GET TOOL OPTIONS
async function getCurrentToolOptions () {
  const result = await batchPlay(
  [
     {
        "_obj": "get",
        "_target": [
           {
              "_property": "currentToolOptions"
           },
           {
              "_ref": "application",
              "_enum": "ordinal",
              "_value": "targetEnum"
           }
        ],
        "_options": {
           "dialogOptions": "dontDisplay"
        }
     }
  ],{
     "synchronousExecution": false
  });
  return result[0].currentToolOptions;
}

// SET TOOL OPTIONS
async function setCurrentToolOptions(currenttooloptions) {
  const result = await batchPlay(
  [
     {
        "_obj": "set",
        "_target": [
           {
             "_ref": "paintbrushTool"
           }
        ],
        "to": currenttooloptions
     }
  ],{
     "synchronousExecution": false
  });
}

async function test1() {
  let cto = await getCurrentToolOptions();
  cto.$opVr.minimum._value = 20;
  await setCurrentToolOptions(cto);
}

document.getElementById("test1").addEventListener("click", test1);
1 Like

Thanks for all the help, Simon. Much appreciated.

1 Like

The next step would be to listen to the event of changing brush settings (like brush size, opacity etc.) so I can synchronize my panel with the Photoshop settings. Otherwise this would be chaos.

I tried “eventNotifier” but most brush-related events are not showing up there. What would be the proper method to achieve that?

It is still not working. if I click the button. (brush is activated) nothing happens.

I have checked. both functions are called and able to make an alert-output.
But the opacity did not change.

Any idea?

1 Like

This one changes the “Minimum Opacity”. And here it works. The value was 0 before the click.

image

Not sure if this can be related, but I can’t change the minimum value in Ps itself, so maybe that’s the case for you too @assitburn :thinking: When I change the slider, as soon as I click outside, it jumps right back to zero. Didn’t bother to figure out why to be honest :slight_smile:

1 Like

That sounds weird and might be the cause of the issue. I guess this is a case for the general Photoshop support.

Sorry to all of you. It was a mistake from me. I do not want to set the minimum brush opacity. I want to set the brush opacity .

A litte bit silly from me…

Replace
cto.$opVr.minimum._value = 20;

by this
cto.opacity = 50;

@AndreasResch
Thank you. A very useful piece of code!

1 Like

Thank you a lot!
Now it works for me. :wink:

Btw, the “Brush Tip Shape” can be manipulated like this: (hardness, spacing, angle, etc.)

{
  _obj: 'set',
  _target: [{_ref: 'brush', _enum: 'ordinal', _value: 'targetEnum'}],
  to: {
    _obj: 'brush',
    spacing: {
      _unit: 'percentUnit',
      _value: 33
    }
  }
}

Maybe this helps someone, too.

2 Likes

Can you please find options for Clone Stamp Tool?
“Sample: Current Layer, Current & Below, All Layers”

I have looked through the whole object several times but cannot find these properties.

Inside currentToolOptions:

  • $StmS: true → Current Layer
  • $StmS: true and $StmB: true: → Current & Below
  • $StmS: false and $StmB: false: → All layers
1 Like

Aargh!
I suspected it, but didn’t decide to try it, it’s didn’t too obvious.
Thank you so much!

Have you got any idea how to capture the events of changing the brush settings?