Skip to main content
Participating Frequently
January 24, 2013
Question

LayerSet.Merge() throwing COM exception

  • January 24, 2013
  • 2 replies
  • 1827 views

Hi all,

    I'm using the COM interop for photoshop in my C# assembly to automate some actions in photoshop. The problem is that LayerSet.Merge() throws a COM exception in certain situations. I don't know if I have all the cases, but in general, I assumed that LayerSet.Merge() would collapse a LayerSet, regardless of how many nested LayerSets and ArtLayers were in it to an ArtLayer that I could assign to the ActiveLayer. That appears to not be the case. So I do something like this:

// store state

HistoryState state = d.ActiveHistoryState;

d.ActiveLayer = set.Merge();

d.Selection.SelectAll();

// process the layer in some way...

// restore state           

d.ActiveHistoryState = state;

where d is the ActiveDocument and set is the LayerSet I'd like to merge. I checked in the debugger and the ActiveDocument (d) is not null and is the right document opened in Photoshop. The LayerSet I'm calling Merge() on (set) is also not null and the correct LayerSet, but for some reason, I get this exception:

System.Runtime.InteropServices.COMException was unhandled by user code

Message: General Photoshop error occurred. This functionality may not be available in this version of Photoshop.

- The object "layer 38 of document 37" is not currently available.

My photoshop file looks like this:

myFile.psd

|-mySet1

     |-mySet1_1

          |-artLayer1

     |-mySet1_2

          |-artLayer2

|-artLayer3

so my code loops through and merges each LayerSet starting at the deepest point. In other words, mySet1_2 is merged first and assigned to the ActiveLayer (then processed etc) and that works. Then mySet1_2 is merged and assigned to ActiveLayer, then mySet1 is merged and that's when I hit the COM exception.

I assumed LayerSet.Merge() was exactly the same as using Layer->MergeLayers in the Photoshop UI, but that's apparently not the case since I can merge all of those LayerSets using the UI, but the COM object seems to balk.

does anyone have any idea what the issue is?

Thanks!

-C

This topic has been closed for replies.

2 replies

Inspiring
January 25, 2013

I also can't help with the C# part. But that type of error message is common in javascript when there is a bad reference. That seems the case here as your code works until a point.

I don't see where 'set' is being assisgned but my guess is at some point in the line

d.ActiveLayer = set.Merge();

set is refering to a layerSet that no longer exists because it has already been merged.

Participating Frequently
January 28, 2013

d.ActiveLayer = set;

d.ActiveLayer.Merge()

ended up doing the job. I assumed that the ActiveLayer was an ArtLayer, not a LayerSet so it never occurred to me to assign the set to it prior to calling Merge().

thanks for the help!

Inspiring
January 28, 2013

So that others reading this are clear. layerSet.merge() and artLayer.merge() do two different things.

layerSet.merge() replaces the layerSet with a new artLayer that contains a merged version of the layerSet's contents. If the layerSet is locked the returned artLayer has the same lock(s).

artLayer.merge() merges the layer into the layer below and returns the bottom artLayer. It will throw an error is the layer below is a layerSet, either artLayer has a lock, the activeLayer is the background or bottom layer.

Paul Riggott
Inspiring
January 25, 2013

Sorry can't answer your question as I rarely use C#.

As a work around you might try...

 

app.DoJavaScript(

"app.activeDocument.activeLayer.merge();", null, null);