Skip to main content
RoyBoySolorio
Inspiring
February 9, 2024
Answered

Copying layer comp list/directory from one doc to another

  • February 9, 2024
  • 4 replies
  • 697 views

Hi all,

 

Is there a way to copy the list of layer comps and paste into a fresh list? 

 

Next bigger question, can you access the Layer Comp list within a smart object, as seen in the properties panel, and copy that?

 

I have a script that that culls layers by name, and makes layer comps from that data.  Unfortumanetly those layer comps need to be copied to the main worker, not just inside the smart object.

 

Thanks for any guidance.

This topic has been closed for replies.
Correct answer c.pfaffenbichler

 

// create layer comps for layer comps in smart object;
// 2024, use it at your own risk;
if (app.documents.length > 0) {
var myDocument = activeDocument;
// remove existing layer comps;
myDocument.layerComps.removeAll();
var theSmartObject = collectSmartObjectsLayerComps ();
// process layer comps;
for (var y = 0; y < theSmartObject[2].length; y++) {
var thisOne = theSmartObject[2][y];
applyLayerCompToSmartObject (theSmartObject[1], thisOne[1]);
createLayerComp (thisOne[0]);
}
};
////////////////////////////////////#
////// collect smart object’s layer comps, probably based on code by paul, mike or x //////
function collectSmartObjectsLayerComps () {
try {
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
//ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if not layer group collect values;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
// is smart object;
if(layerDesc.hasKey(stringIDToTypeID('smartObject'))) {
// comps;
    var theSO = layerDesc.getObjectValue(stringIDToTypeID("smartObject"));
    var theCompsList = theSO.getObjectValue(stringIDToTypeID("compsList"));
    if (theCompsList.count > 2) {
    var theCompsList = theCompsList.getList(stringIDToTypeID("compList"));
    var theSOComps = new Array;
    for (var n = 0; n < theCompsList.count; n++) {
    var thisOne = theCompsList.getObjectValue(n);
    var theCompName = thisOne.getString(stringIDToTypeID("name"));
    var theCompID = thisOne.getInteger(stringIDToTypeID("ID"));
    var theComment = thisOne.getString(stringIDToTypeID("comment"));
    theSOComps.push([theCompName, theCompID, theComment]);
    };
// add to array;
    return [theName, theID, theSOComps]
}
}
}
} catch (e) {};
};
////// apply layercomp to smart object //////
function applyLayerCompToSmartObject (theLayerID, theID) {
selectLayerByID(theLayerID, false);
    var desc9 = new ActionDescriptor();
        var ref3 = new ActionReference();
        ref3.putEnumerated(charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ));
    desc9.putReference( charIDToTypeID( "null" ), ref3 );
    desc9.putInteger( stringIDToTypeID( "compID" ), theID );
executeAction( stringIDToTypeID( "setPlacedLayerComp" ), desc9, DialogModes.NO );
};
////// based on code by mike hale and paul riggott //////
function selectLayerByID(index,add){ 
add = undefined ? add = false:add 
var ref = new ActionReference();
    ref.putIdentifier(charIDToTypeID("Lyr "), index);
    var desc = new ActionDescriptor();
    desc.putReference(charIDToTypeID("null"), ref );
        if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); 
        desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 
    try{
    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message); 
}
};
////// create layer comp //////
function createLayerComp (theName) {
// =======================================================
var desc4 = new ActionDescriptor();
var ref2 = new ActionReference();
ref2.putClass( stringIDToTypeID( "compsClass" ) );
desc4.putReference( stringIDToTypeID( "null" ), ref2 );
var desc5 = new ActionDescriptor();
desc5.putBoolean( stringIDToTypeID( "useVisibility" ), true );
desc5.putBoolean( stringIDToTypeID( "usePosition" ), true );
desc5.putBoolean( stringIDToTypeID( "useAppearance" ), true );
desc5.putBoolean( stringIDToTypeID( "useChildLayerCompState" ), true );
desc5.putString( stringIDToTypeID( "title" ), theName );
var idcompsClass = stringIDToTypeID( "compsClass" );
desc4.putObject( stringIDToTypeID( "using" ), idcompsClass, desc5 );
executeAction( stringIDToTypeID( "make" ), desc4, DialogModes.NO );
};

edited

 

4 replies

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
February 11, 2024

 

// create layer comps for layer comps in smart object;
// 2024, use it at your own risk;
if (app.documents.length > 0) {
var myDocument = activeDocument;
// remove existing layer comps;
myDocument.layerComps.removeAll();
var theSmartObject = collectSmartObjectsLayerComps ();
// process layer comps;
for (var y = 0; y < theSmartObject[2].length; y++) {
var thisOne = theSmartObject[2][y];
applyLayerCompToSmartObject (theSmartObject[1], thisOne[1]);
createLayerComp (thisOne[0]);
}
};
////////////////////////////////////#
////// collect smart object’s layer comps, probably based on code by paul, mike or x //////
function collectSmartObjectsLayerComps () {
try {
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
//ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if not layer group collect values;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
// is smart object;
if(layerDesc.hasKey(stringIDToTypeID('smartObject'))) {
// comps;
    var theSO = layerDesc.getObjectValue(stringIDToTypeID("smartObject"));
    var theCompsList = theSO.getObjectValue(stringIDToTypeID("compsList"));
    if (theCompsList.count > 2) {
    var theCompsList = theCompsList.getList(stringIDToTypeID("compList"));
    var theSOComps = new Array;
    for (var n = 0; n < theCompsList.count; n++) {
    var thisOne = theCompsList.getObjectValue(n);
    var theCompName = thisOne.getString(stringIDToTypeID("name"));
    var theCompID = thisOne.getInteger(stringIDToTypeID("ID"));
    var theComment = thisOne.getString(stringIDToTypeID("comment"));
    theSOComps.push([theCompName, theCompID, theComment]);
    };
// add to array;
    return [theName, theID, theSOComps]
}
}
}
} catch (e) {};
};
////// apply layercomp to smart object //////
function applyLayerCompToSmartObject (theLayerID, theID) {
selectLayerByID(theLayerID, false);
    var desc9 = new ActionDescriptor();
        var ref3 = new ActionReference();
        ref3.putEnumerated(charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ));
    desc9.putReference( charIDToTypeID( "null" ), ref3 );
    desc9.putInteger( stringIDToTypeID( "compID" ), theID );
executeAction( stringIDToTypeID( "setPlacedLayerComp" ), desc9, DialogModes.NO );
};
////// based on code by mike hale and paul riggott //////
function selectLayerByID(index,add){ 
add = undefined ? add = false:add 
var ref = new ActionReference();
    ref.putIdentifier(charIDToTypeID("Lyr "), index);
    var desc = new ActionDescriptor();
    desc.putReference(charIDToTypeID("null"), ref );
        if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); 
        desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 
    try{
    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message); 
}
};
////// create layer comp //////
function createLayerComp (theName) {
// =======================================================
var desc4 = new ActionDescriptor();
var ref2 = new ActionReference();
ref2.putClass( stringIDToTypeID( "compsClass" ) );
desc4.putReference( stringIDToTypeID( "null" ), ref2 );
var desc5 = new ActionDescriptor();
desc5.putBoolean( stringIDToTypeID( "useVisibility" ), true );
desc5.putBoolean( stringIDToTypeID( "usePosition" ), true );
desc5.putBoolean( stringIDToTypeID( "useAppearance" ), true );
desc5.putBoolean( stringIDToTypeID( "useChildLayerCompState" ), true );
desc5.putString( stringIDToTypeID( "title" ), theName );
var idcompsClass = stringIDToTypeID( "compsClass" );
desc4.putObject( stringIDToTypeID( "using" ), idcompsClass, desc5 );
executeAction( stringIDToTypeID( "make" ), desc4, DialogModes.NO );
};

edited

 

RoyBoySolorio
Inspiring
February 12, 2024

Stunned and amazed c.pfaffenbichler.  

That was exactly what I needed.  Your screen grabs thru me off, but when I ran it, it made the list as needed, and did the syncing too. I'm updsting the actions and will beta test the new pipeline later today.  Thanks again for your help on this.  Maybe when complete I could send a training vid that shows the work inprogress.  Of course with prereleased assets.

~Roy
Stephen Marsh
Community Expert
Community Expert
February 10, 2024

@RoyBoySolorio Creating matching layer comps in the parent document would only seem to make sense if the child smart object contained the same named layers as the parent.

 

Can you post a screenshot of the parent doc's layers panel, and also a screenshot of the child smart object doc's layers panel.

c.pfaffenbichler
Community Expert
Community Expert
February 10, 2024

»what do you mean by »copy«?« may seem like a strange question. 

 

Let me elaborate:

What you described as »the Layer Comp list within a smart object, as seen in the properties panel, and copy that« is probably not the end-point of the intended automation process, because what good is the list in the clipboard? 

How do you intend to use that list subsequently? 

rsolorio
Participating Frequently
February 10, 2024

exactly correct Steven.  


Just getting back to this thread. I have a functional modified script that exports layer comps to files. Great. The trick has been to drive a predefined layer comp list, inside a smart object with the same list outside of the smart object using the properties panel. Also good. 

 
For example. I have a tv screen, and there will be 20 localized welcome screens in a psd with layer comps. I bring in that psd into tv file as a smart object. I recreate that list of layer comps that are in the smart object with another list in the tv worker. Scripts and actions do the rest. 
 
Through the last few years that list of 20 locales has morphed and changed. I have new scripts that builds the layer comp list based on the name of the locales, but I wanted to copy that list and for the main worker. 
 
Just so you understand, there are 30 -50 unique UI screens with 1  to 27 locales. Doing it by hand is time consuming and prone to mistakes. So that’s the gist. 
L
c.pfaffenbichler
Community Expert
Community Expert
February 10, 2024

1) What do you mean? Please post meaningful screenshots including all pertinent Panels to clarify. 

2) One can assess the Layer Comps of a Smart Object, what do you mean by »copy«? 

 

// 2023, use it at your own risk;
if (app.documents.length > 0) {
var theThings = collectSmartObjectsWithLayerComps();
alert (theThings.length+" smart object instances with layer comps\n\n"+theThings.join("\n\n"));
};
////////////////////////////////////
////// collect smart objects, probably based on code by paul, mike or x //////
function collectSmartObjectsWithLayerComps () {
// the file;
var myDocument = app.activeDocument;
// get number of layers;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if not layer group collect values;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
if(layerDesc.hasKey(stringIDToTypeID('smartObject'))) {
    var soDesc = layerDesc.getObjectValue(stringIDToTypeID('smartObject'));
    var theFileRef = soDesc.getString(stringIDToTypeID('fileReference'));
    var theDocID = soDesc.getString(stringIDToTypeID('documentID'));
    var x = soDesc.getList(stringIDToTypeID("compsList"));
    var theCompsList = soDesc.getObjectValue(stringIDToTypeID("compsList"));
    if (theCompsList.count > 2) {
        var theCompsList = theCompsList.getList(stringIDToTypeID("compList"));
        var theSOComps = new Array;
        for (var n = 0; n < theCompsList.count; n++) {
        var thisOne = theCompsList.getObjectValue(n);
        var compName = thisOne.getString(stringIDToTypeID("name"));
        var compID = thisOne.getInteger(stringIDToTypeID("ID"));
        var theComment = thisOne.getString(stringIDToTypeID("comment"));
        theSOComps.push([compName, compID, theComment]);
        };
    theLayers.push([theName, theID, theFileRef, theDocID, theSOComps])
    };
}
};
}
catch (e) {};
};
return theLayers
};