Skip to main content
Known Participant
January 4, 2022
Answered

Script that replaces layers but keeps opacity and blending mode of replaced layer.

  • January 4, 2022
  • 2 replies
  • 1299 views

Hello, I looking for a script that can replace a layer with several other layers but keep the opacity and blending mode of the replaced layers.

 

Link to PSD file: https://we.tl/t-JKd8v0Jx3B

 

Example: I want to replace "_ELEMENT_SPECULAR copy" with "SPECULAR PASS" layers.

Each individual "SPECULAR PASS" have different blending mode and opacity, and I want to keep those.

 

New render passes:

 

 

Old render passes:

 

 

I appreciate all the help I can get!

 

 

 

This topic has been closed for replies.
Correct answer Kukurykus

Select layer to 'replace' others by:

 

(slctn = (aD = activeDocument)
.selection).selectAll(), slctn.copy()
sp = 'SPECULAR PASS', top = aD.layerSets
lSs = [].slice.call(top.getByName('SCENE ELEMENTS')
.layerSets.getByName('ORGANIC OBJECTS').layerSets)

while(lSs.length) {
	lS = lSs.shift(), lrs = [].slice.call(lS.artLayers)
	while(lrs.length) ((shft = lrs.shift()).name == sp)
	&& (aL = aD.activeLayer = shft, slctn.selectAll(), slctn.clear(),
	aD.paste(), shft != aD.activeLayer && aD.activeLayer.merge())
}

 

2 replies

Tsu-teyAuthor
Known Participant
January 4, 2022

Okay, my bad. Seems like I wasn't specific enough.

  1.  The Green layer called "_ELEMENT_SPECULAR" needs to be replaced with the Red layers called "SPECULAR PASS".
  2. Each Replaced layer needs to contain the following attributes from each individual Red layers.

     

    - Name

    - Blending Mode

    - Opacity

    - Placement

  3. The placement of the New Replaced layer should in the same location as the Old Red layer. As shown in Explanation_02.jpg (See attached).
  4. Preferably the script can replace any layer that is selected but it is also okay to change the name of the layer within the script as I need to use it on "_ELEMENT_DIRECT copy" aswell.

 

If something is not clear or you need any extra information please let me know. Also use the PSD file in the post for testing.

Kukurykus
KukurykusCorrect answer
Legend
January 4, 2022

Select layer to 'replace' others by:

 

(slctn = (aD = activeDocument)
.selection).selectAll(), slctn.copy()
sp = 'SPECULAR PASS', top = aD.layerSets
lSs = [].slice.call(top.getByName('SCENE ELEMENTS')
.layerSets.getByName('ORGANIC OBJECTS').layerSets)

while(lSs.length) {
	lS = lSs.shift(), lrs = [].slice.call(lS.artLayers)
	while(lrs.length) ((shft = lrs.shift()).name == sp)
	&& (aL = aD.activeLayer = shft, slctn.selectAll(), slctn.clear(),
	aD.paste(), shft != aD.activeLayer && aD.activeLayer.merge())
}

 

Tsu-teyAuthor
Known Participant
January 4, 2022

This is works perfectly! Awesome Kukurykus!

Kukurykus
Legend
January 4, 2022
top = activeDocument.layerSets, lSs = []
.slice.call(top.getByName('SCENE ELEMENTS')
.layerSets.getByName('ORGANIC OBJECTS').layerSets)
top[0].artLayers[0].remove(); while(lSs.length) {
	lS = lSs.shift(), lrs = [].slice.call(lS.artLayers)
	while(lrs.length) (shft = lrs.shift())
	.name == 'SPECULAR PASS'
	&& shft.move(top[0],
	ElementPlacement
	.INSIDE)
}
Tsu-teyAuthor
Known Participant
January 4, 2022

Thank you, Kukurykus.

Is it possible for the new layer to be in the same place as the old layer in the layer structure aswell?

This is the result I get from the script:

 

Tsu-teyAuthor
Known Participant
January 4, 2022

That the layer structure stays the same like this: