loonghao / photoshop-python-api

Python API for Photoshop.
https://loonghao.github.io/photoshop-python-api/
MIT License
598 stars 68 forks source link

Replacing multiple layers with images does not work; it only replaces the same layer multiple times #334

Open andrewjosephallen opened 4 months ago

andrewjosephallen commented 4 months ago

Describe the bug I have an application that is replacing some set layers with images. I loop over every layer in the doc, and when i get to the layers I want, I replace them with a new image I just downloaded.

Here is a snippet of code.

with Session(rpath/to/file', action="open") as ps2:
        # Open the Photoshop document
        ps2.echo(ps2.active_document.name)
        for layer in ps2.active_document.layers:
            if layer.name.startswith('GK'):
                   layer_designator = layer.name.split()[1]
                         if layer_designator == 'CLUBLOGO':
                            name = layer.name
                            print('logolayer name is: ' + name)
                            url3 = str(players['Goalkeeper']['club_logo'])
                            destination_folder3 = r"path/to/save"
                            filename3 = url3.split("/")[-1]
                            destination_path3 = os.path.join(destination_folder3, filename3)

                            response3 = requests.get(url3)
                            if response3.status_code == 200:
                                with open(destination_path3, "wb") as f3:
                                    f3.write(response3.content)
                                print(f"Image downloaded and saved to {destination_path3}")

                                # Replace the image of the current active layer with the downloaded image
                                replace_contents = ps2.app.stringIDToTypeID("placedLayerReplaceContents")
                                desc = ps2.ActionDescriptor
                                idnull = ps2.app.charIDToTypeID("null")
                                desc.putPath(idnull, destination_path)
                                ps2.app.executeAction(replace_contents, desc)

                            else:
                                print("Failed to download the image")
                            layer.name = name
                        elif layer_designator == 'FLAG':
                            name = layer.name
                            print('flaglayer name is: ' + name)

                            url = str(players['Goalkeeper']['flag'])
                            destination_folder = r"path/to/file"
                            filename = url.split("/")[-1]
                            destination_path = os.path.join(destination_folder, filename)

                            response = requests.get(url)
                            if response.status_code == 200:
                                with open(destination_path, "wb") as f:
                                    f.write(response.content)
                                print(f"Image downloaded and saved to {destination_path}")

                                # Replace the image of the current active layer with the downloaded image
                                replace_contents = ps2.app.stringIDToTypeID("placedLayerReplaceContents")
                                desc = ps2.ActionDescriptor
                                idnull = ps2.app.charIDToTypeID("null")
                                desc.putPath(idnull, destination_path)
                                ps2.app.executeAction(replace_contents, desc)

                            else:
                                print("Failed to download the image")
                            layer.name = name

Expected behavior I would expect the layers to appear and replace the smart objects they are pointing to. I have confirmed the URLs are separate, and they are downloading correctly. When i added in some sleeps, I could see the same layer being replaced multiple times. I can also confirm that the layers are named correctly and are printing out correctly to the terminal when accessed. Is there some caching issues I am unaware of with selected layers?

Desktop (please complete the following information):

let me know if there is any more info you need. I could have a workaround where I just create a new layer everytime and move it to the correct spot, but this is the beauty of smart objects to just replace them I would hope