Mass Replace Source Image

Moho allows users to write new tools and plugins. Discuss scripting ideas and problems here.

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
User avatar
TheMinahBird
Posts: 263
Joined: Sat Jul 27, 2013 11:10 pm

Mass Replace Source Image

Post by TheMinahBird »

I got inspired by this exchange here:

viewtopic.php?p=215142#p215142

But I'm finding myself in a bit of trouble, I'm pretty sure what I'm trying to do is just too complicated. It seems to be with the detection of source layers is where it's messing up. :'((

So Moho for some reason is laggy when I use PSD files, even if it's an unrigged 500 x 500 psd it just loads incredibly slow for some reason. And so I go the idea to write this script that can detect the source image of a layer, ergo recognizing where it's coming from and then you could select a folder that has the same name as the PSD. After selecting the folder it will try and match the PNGs to the layers from that PSD file.

How might it do that? I was thinking based on name! And it's not in the script below but I was thinking if it detects doubles or PNGs that couldn't find a match you can just manually tell it where it belongs from there.

But I do realize that comes with its own issues so feel free to let me know if that should be tweaked too. (Also I used https://mohoscripting.com/introduction as reference and was gonna go back and tweak certain things when I was done, like the prefix).


Also I didn't use IsPSDImage() for that one part because Moho kept saying the method was bad so I tried that weird way you see down below ^^;

Code: Select all

ScriptName = "pfx_ReplaceImagesWithPNGs"

pfx_ReplaceImagesWithPNGs = {}

function pfx_ReplaceImagesWithPNGs:Name()
    return "Replace Images with PNGs"
end

function pfx_ReplaceImagesWithPNGs:Version()
    return "1.1" -- Updated version
end

function pfx_ReplaceImagesWithPNGs:IsBeginnerScript()
    return false
end

function pfx_ReplaceImagesWithPNGs:Description()
    return "This script replaces PSD images in the document with PNGs from a specified folder."
end

function pfx_ReplaceImagesWithPNGs:BeginnerDescription()
    return "This script replaces PSD images with PNGs."
end

function pfx_ReplaceImagesWithPNGs:BeginnerDisabledDescription()
    return "This script is not available in beginner mode."
end

function pfx_ReplaceImagesWithPNGs:Creator()
    return "chan"
end

function pfx_ReplaceImagesWithPNGs:UILabel()
    return "Replace Images with PNGs"
end

function pfx_ReplaceImagesWithPNGs:ColorizeIcon()
    return false
end

function pfx_ReplaceImagesWithPNGs:Run(moho)
    local folderPath = LM.GUI.SelectFolder("Select PNG Folder")
    if folderPath then
        ReplaceImagesWithPNGs(moho, folderPath) -- Pass 'moho' object and folder path
    end
end

function ReplaceImagesWithPNGs(moho, folderPath)
    local doc = moho.document
    if not doc then
        LM.GUI.Alert("Error: Unable to access Moho document.", "Error", LM.GUI.ALERT_OK)
        return
    end

    local layerCount = doc:CountLayers()
    if layerCount == 0 then
        LM.GUI.Alert("No layers found in the document.", "Information", LM.GUI.ALERT_OK)
        return
    end

    for i = 0, layerCount - 1 do
        local layer = doc:Layer(i)
        
        -- Check if the layer exists and is an image layer
        local sourceImage = layer:SourceImage()
        if sourceImage then
            local sourceImagePath = sourceImage:GetPath()
            if sourceImagePath:lower():match("%.psd$") then -- Check if the file extension is .psd
                local psdFileName = sourceImagePath:match("^.+/(.+)$")
                local pngFilePath = folderPath .. "/" .. psdFileName
                
                -- Check if the PNG file exists before replacing
                local pngFile = MOHO.FS:FileExists(pngFilePath)
                if pngFile then
                    layer:SetSourceImage(pngFilePath)
                else
                    local choice = LM.GUI.Alert("PNG file not found for layer: " .. layer:Name() .. ". Do you want to skip this layer replacement?", "Missing PNG", LM.GUI.ALERT_OKCANCEL)
                    if choice == 2 then -- Cancel option (user chooses to skip)
                        break
                    end
                end
            end
        end
    end
end
"Now hatred is by far the longest pleasure; men love in haste but they detest at leisure" - Lord Byron
User avatar
moe33
Posts: 8
Joined: Mon Sep 23, 2019 4:56 am
Location: Beijing

Re: Mass Replace Source Image

Post by moe33 »

The psd file opens slowly, possibly because your psd file has redundant data information.
You can open the psd at Photopea and save it again. The psd file downloaded on Photopea is very small and easier to use than using PSD Cleaner.
I made a script myself that can replace images, but it can only replace one layer at a time. You can try it Replace Media.
I remember that Moho12 had a function to convert psd files to png, but I don’t know why, this function was canceled.
User avatar
Panha
Posts: 92
Joined: Sat Oct 21, 2023 3:59 pm

Re: Mass Replace Source Image

Post by Panha »

moe33 wrote: Fri Nov 17, 2023 10:44 am The psd file opens slowly, possibly because your psd file has redundant data information.
You can open the psd at Photopea and save it again. The psd file downloaded on Photopea is very small and easier to use than using PSD Cleaner.
I made a script myself that can replace images, but it can only replace one layer at a time. You can try it Replace Media.
I remember that Moho12 had a function to convert psd files to png, but I don’t know why, this function was canceled.
Is it going to reduce the resolution of the image and make our PSD blur or lower the quality of the PSD?
Post Reply