Extracting Style information programmatically

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
caderial
Posts: 12
Joined: Mon Jul 08, 2024 8:00 pm

Extracting Style information programmatically

Post by caderial »

I am struggleing to find a way to reliably extract the infromation about a style and output it to the lua console. I hoped this would be a simple solution but i keep running into walls, where either i get errored out entirely, or receive no information, or in my latest attempot which i think is close it just reports back nil or no color defined based on my pint statements.

My Lua Console Output:

Code: Select all

Style Name: BG_Skin_Tone_DEF
No Fill Color Defined
No Line Color Defined
-----
Style Name: BG_Skin_Acct_DEF
No Fill Color Defined
No Line Color Defined
-----
Style Name: BG_Skin_ToneSec_DEF
No Fill Color Defined
No Line Color Defined
-----
Style Name: BG_Skin_Tone_1
No Fill Color Defined
No Line Color Defined
-----
Style Name: BG_Skin_Acct_1
No Fill Color Defined
No Line Color Defined
-----
Style Name: BG_Skin_ToneSec_1
No Fill Color Defined
No Line Color Defined
-----
Style extraction completed.



My Script:

Code: Select all

ScriptName = "ExtractStylesInfoFromStyles"
ExtractStylesInfoFromStyles = {}

function ExtractStylesInfoFromStyles:Name()
    return "Extract Styles Info"
end

function ExtractStylesInfoFromStyles:Version()
    return "1.2"
end

function ExtractStylesInfoFromStyles:Description()
    return "Extracts and outputs fill and line colors of all styles in the current document."
end

function ExtractStylesInfoFromStyles:UILabel()
    return "Extract Styles Info"
end

function ExtractStylesInfoFromStyles:Run(moho)
    local doc = moho.document

    -- Check if a document is loaded
    if not doc then
        print("No document is loaded.")
        return
    end

    print("Starting style extraction...")

    local styleCount = doc:CountStyles()
    print("Number of styles: " .. styleCount)

    if styleCount == 0 then
        print("No styles found in the document.")
        return
    end

    -- Iterate through all styles
    for i = 0, styleCount - 1 do
        local style = doc:StyleByID(i)

        if style then
            -- Access the style name safely
            local styleName = style.fName:Buffer()
            print("Style Name: " .. styleName)

            -- Check and print the fill color
            if style.fDefineFillCol then
                local fillColor = style.fFillCol
                print(string.format("  Fill Color (RGB): R:%d G:%d B:%d", fillColor.r, fillColor.g, fillColor.b))
                print(string.format("  Fill Color (Hex): #%06X", fillColor.value))
            else
                print("  No Fill Color Defined")
            end

            -- Check and print the line color
            if style.fDefineLineCol then
                local lineColor = style.fLineCol
                print(string.format("  Line Color (RGB): R:%d G:%d B:%d", lineColor.r, lineColor.g, lineColor.b))
                print(string.format("  Line Color (Hex): #%06X", lineColor.value))
            else
                print("  No Line Color Defined")
            end
        else
            print(string.format("Style at index %d could not be accessed.", i))
        end

        print("-----")
    end

    print("Style extraction completed.")
end

function ExtractStylesInfoFromStyles:Init(moho)
    if moho then
        moho:NewScriptMenu(self:Name(), self)
    end
end

local function Main()
    if Moho then
        ExtractStylesInfoFromStyles:Init(Moho)
    end
end

Main()

I am hoping to find the simplest most elegant solution to extract style information so i can reference it in my other scripts but so far it seems impossible no matter which approach i take. Primarily I'd like to output the Fill color and Line Color, but ideally it should be able to extract and display all the information about a style included in a MOHO style.

Thoughts Ideas, all would be appreciated!
User avatar
hayasidist
Posts: 3840
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: Extracting Style information programmatically

Post by hayasidist »

as discussed -- the style data is in an AnimChannel; and even if not animated, there will be a frame zero key (which won't show unless you force the channel to be shown in the timeline); and you get the value by (e.g.) AnimColor:GetValue(moho.frame).

similarly, setting the value needs to be done via the channel.

Changing the style assigned to a shape is done through shape.fInheritedStyle etc. Note that these are of type M_String and that to change them you'll need to use String:Set(str) -- i.e. they're not Lua strings where it's just a simple matter of attribute = NewStyleName (of type "string").

Note also that the UUID will need to be set.

There are also booleans that indicate if the style attribute (i.e. fill colour, line colour, line width) are to be taken from the inherited style or from the shape's base attributes (in fMyStyle)

That along with the sample code PM'd should, hopefully, get you moving!

Let me know if not.
Post Reply