Set brush opacity?

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

jacobolus

Set brush opacity?

Post by jacobolus »

This code is identical to the code up above, but has some explanatory comments interspersed:
Code: Select all// a shortcut for stringIDToTypeID, to save typing
var $s = function (s) { return app.stringIDToTypeID(s); };

// Accepts a "features" object as input, with possible parameters:
//     diameter, hardness, angle, roundness, spacing,
//     interpolation, flipX, flipY
//
// Example:
//     setBrushFeatures({diameter: 10, flipX: false, roundness: 50})
var setBrushFeatures = function (features) {
    var app_ref, prop, tool_opts, brush, possible_features, i, n, prop, id,
        unit_type, brush_ref, set_brush_desc;

    app_ref = new ActionReference();
    app_ref.putEnumerated($s("application"), $s("ordinal"), $s("targetEnum"));

    // get the "type" of the brush, and get the brush's settings. If the type
    // isn’t "computedBrush", then bail out of this function, because there’s
    // no currently known way to set other kinds of brush settings.
    tool_opts = (app.executeActionGet(app_ref)
                    .getObjectValue($s('currentToolOptions')));
    if (tool_opts.getObjectType($s('brush')) != $s('computedBrush')) {return;}
    brush = tool_opts.getObjectValue($s('brush'));
   
    // the list of features we might pass in here.
    possible_features = {
        diameter: 'unit', hardness: 'unit', angle: 'unit', roundness: 'unit',
        spacing: 'unit', interpolation: 'bool', flipX: 'bool', flipY: 'bool'};
   
    // loop through all the features in the passed-in object, and for any
    // of them that match our list of possible features, change that setting
    // in the 'brush' descriptor that we fetched earlier, making sure to
    // set the proper type of value (either "unit double" or "boolean")
    for (prop in features) {
        if (prop in possible_features) {
            id = $s(prop);
            if (possible_features[prop] === 'unit') {
                unit_type = brush.getUnitDoubleType(id);
                brush.putUnitDouble(id, unit_type, features[prop]);
            } else if (possible_features[prop] === 'bool') {
                brush.putBoolean(id, features[prop]);
            }
        }
    }

    // get a reference to the "target" brush (i.e. the current brush)
    brush_ref = new ActionReference();
    brush_ref.putEnumerated( $s('brush'), $s('ordinal'), $s('targetEnum'));
   
    // make an action descriptor for the action, with the target set
    // to the current brush, and the content set to our brush descriptor
    // with our custom settings.
    set_brush_desc = new ActionDescriptor();
    set_brush_desc.putReference($s('target'), brush_ref);
    set_brush_desc.putObject($s('to'), $s('brush'), brush);
   
    executeAction($s('set'), set_brush_desc, DialogModes.NO);
};

// Copyright (c) 2012 Jacob Rus

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Mike Hale

Set brush opacity?

Post by Mike Hale »

That is great, it even has more comments than I hoped it would( or I normally add to my posts ).

I have seen people just grab a function or snippet like this, put it together with something from another thread then post the whole thing asking why it doesn't work. At least with comments or a demo in the code they will grab it as well.
NirBenz

Set brush opacity?

Post by NirBenz »

Seeing this thread is quite old I am reviving it to ask if anyone had any luck applying similar properties to Bristle brushes, added in CS5 (or was that 4?).

I actually tried modifying some of the code that was posted here (i.e simply changing the parameter names) and CS5 didn't like that at all.

So, anyone successfully managed to apply Bristle Brush parameters using scripts?

Thanks