Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Find and Replace colours in whole project

  • Find and Replace colours in whole project

    Posted by Harry Hoag on January 31, 2019 at 2:39 pm

    Hi

    Anyone know of a script that will let me find and replace all the colour fields in shape layers throughout a massive project?

    Classic client error, been given the wrong colour pallet at the start of the job, now at the end of the job I need to swap around 6 or so stroke and fill colours on a gazillion shape layers through a gazillion precomps. A script would be mighty hand right now.

    And ideas would be much appreciated ☺

    thanks
    H

    Jeremiah Bratene replied 5 years, 10 months ago 5 Members · 6 Replies
  • 6 Replies
  • Oleg Pirogov

    February 2, 2019 at 9:28 am

    This script substitutes one color for another in all feasible properties (like effects, shapes’ fill colors etc) in all comps and layers:

    //converts a color channel from [0,1] range to hex string
    function channelToHex(channel){
    if (channel<0) return "00";
    if (channel>1) return "FF";

    var channelInt = Math.round(channel*255);
    var hex = channelInt.toString(16);
    if (hex.length == 1) hex = "0" + hex;
    return hex.toUpperCase();
    }

    //recursive function to look for color properties; "path" variable is rudimental
    function colorSearch(thePropertyGroup, path, targetColor, substituteColor){
    for (var i=1; i<=thePropertyGroup.numProperties; i++){
    currentProperty = thePropertyGroup.property(i);

    //checking, if it's a property and not a property group
    if (currentProperty.propertyType == PropertyType.PROPERTY){
    //checking, if it's a color property
    if (currentProperty.propertyValueType == PropertyValueType.COLOR){
    //checking, if color value equals the targetColor (8 bpc) or is its 8 bpc equivalent (16, 32 bpc)
    if ([channelToHex(currentProperty.value[0]), channelToHex(currentProperty.value[1]), channelToHex(currentProperty.value[2])].join("") == targetColor){
    try{
    currentProperty.setValue(substituteColor);
    }
    //mostly for handling hidden color layers
    catch(err){
    }
    }
    }
    }
    else{
    colorSearch(currentProperty, path+" -> "+currentProperty.name, targetColor, substituteColor);
    }
    }
    }

    function main(){
    //dialog window
    var dialogWindow = new Window ("dialog", "Color Substitution");

    var targetColorGroup = dialogWindow.add("group");
    targetColorGroup.alignment = "right";
    targetColorGroup.add("statictext", undefined, "Target color:");

    var targetColorBoxGroup = targetColorGroup.add("group");
    targetColorBoxGroup.spacing = 0;
    targetColorBoxGroup.add("statictext", undefined, "#");
    var targetColorHex = targetColorBoxGroup.add("edittext", undefined, "FFFFFF");

    var substitutionColorGroup = dialogWindow.add("group");
    substitutionColorGroup.alignment = "right";
    substitutionColorGroup.add("statictext", undefined, "Substitute with:");

    var substitutionColorBoxGroup = substitutionColorGroup.add("group");
    substitutionColorBoxGroup.spacing = 0;
    substitutionColorBoxGroup.add("statictext", undefined, "#");
    var substitutionColorHex = substitutionColorBoxGroup.add("edittext", undefined, "0000FF");

    var buttonsGroup = dialogWindow.add("group");
    buttonsGroup.add("button", undefined, "Ok");
    buttonsGroup.add("button", undefined, "Cancel");

    var resp = dialogWindow.show();

    if (resp == 2){
    return;
    }

    var prj = app.project;

    //converts hex string to [r, g, b, a] color; a = 1
    var hexToRGBa = function(hexColorText){
    hex = parseInt(hexColorText, 16);
    return [hex >> 16, hex >> 8 & 0xFF, hex & 0xFF, 255]/255;
    }

    var targetColor = targetColorHex.text;
    var substituteColor = hexToRGBa(substitutionColorHex.text);

    //searching in every comp...
    for (i=1; i<=prj.numItems; i++){
    if (prj.item(i).typeName == "Composition"){
    currentComp = prj.item(i);

    //...and in every layer
    for (j=1; j<=currentComp.layers.length; j++){
    colorSearch(currentComp.layer(j), currentComp.layer(j).name, targetColor, substituteColor);
    }
    }
    }

    }

    main();

    Works fine as far as I’ve tested it.

  • Harry Hoag

    February 3, 2019 at 9:24 pm

    wow thanks man, I’ll give it a go!

  • Michael Szalapski

    February 7, 2019 at 10:25 pm

    In the future, you might check out Ray Dynamic Color. It’s a very handy tool that does this and a lot more.

    – The Great Szalam
    (The \’Great\’ stands for \’Not So Great, in fact, Extremely Humble\’)

    No trees were harmed in the creation of this message, but several thousand electrons were mildly inconvenienced.

  • Alex Restuccia

    April 23, 2019 at 2:36 pm

    Thanks, this is super helpful! But unfortunately it seems that it only works with layers using a Fill color, and it doesn’t change the Stroke color. Any idea how to change the script to support Stroke colors? Thanks!

  • Oleg Pirogov

    April 24, 2019 at 12:13 am

    As far as I’ve tested, it works for any unhidden color property: fills, strokes, effects, lights, – whatever.
    Could you please give more info on what exactly happens in your project?

    I suspect, it may just raise an exception on a half-way, simply not getting to the stroke color property.

  • Jeremiah Bratene

    June 26, 2020 at 12:29 am

    Just coming across this in 2020. I’m trying to replace the colors in several gradients, and this script would immensely speed up my process…however, I get this error when trying to use it:

    I’ve never scripted before, but I assume I’m supposed to replace that “undefined” with something…could somebody please help me figure out what to replace it with to get this script running?

  • Axel Kraut

    August 26, 2021 at 4:27 pm

    Hi Oleg, unfortunately the images don’t load for me (anymore). Can you me again how to get the script running? Would be a huge help!

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy