[solved] [Python] How to access Smart Objects?

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

alonza1
Posts: 2
Joined: Mon Jul 06, 2020 10:42 pm

[solved] [Python] How to access Smart Objects?

Post by alonza1 »

Hi,

I am at a disadvantage since I am not proficient in JS. I primarily use Python.
Even so, I have successfully opened a PSB file, can read the layer names, figured out how to toggle visibility in the active document as well.

My question is how do I access the embedded smart objects? I'm trying to write a tool that will toggle visibility on ALL smart objects in a massive PSB. Each embedded graphic has 3 layers all with the same names. I want a tool that will cycle visibility on all embedded layers without having to open each one and switch it.

Thanks in advance for any help!
Last edited by alonza1 on Tue Jul 14, 2020 6:45 pm, edited 1 time in total.
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: [Python] How to access Smart Objects?

Post by Kukurykus »

Before you create smart object create layers composition for layers. Create then from them SO and switch visibility without opening embedded layers by properties panel. Probably you can also open smart object to set layers compositions for the first time, then save document once for keeps and access layers compositions from outside the same way.
alonza1
Posts: 2
Joined: Mon Jul 06, 2020 10:42 pm

Re: [Python] How to access Smart Objects?

Post by alonza1 »

Figured it out. Here is roughly how it works:

Code: Select all

def AccessSmartObjects():
    psApp = comtypes.client.CreateObject('Photoshop.Application', dynamic=True)
    psDoc = psApp.Application.ActiveDocument
    smartObjects = []
    for layerGrp in psDoc.layerSets:
        for layer in layerGrp.artLayers:
            if layer.kind == 17:	# 17 means it's a smart object
                smartObjects.append(layer)

    for layer in smartObjects:
        try:
            psDoc.activeLayer = layer
            psApp.executeAction(psApp.stringIDToTypeID("placedLayerEditContents"))
            smartDoc = psApp.activeDocument
            print 'Run some function here on the smart object'
            smartDoc.save()
            smartDoc.close()
            # after closing the smartDoc the main psd will now be the active document

        except:
            print 'passed on something here.'
            pass
Last edited by alonza1 on Mon Jul 13, 2020 9:03 pm, edited 1 time in total.