Image
-- **************************************************
-- Provide Moho with the name of this script object
-- **************************************************
ScriptName = "SS_RefLayerUpdate"

-- **************************************************
-- Reference Layer Update - Same as bulit-in, but remembers settings, shortcut-keyable + sync points
-- version:	01.10 AS12.5+ #520914, #530817 M14R
-- by Sam Cogheil (SimplSam)
-- **************************************************

--[[ ***** Licence & Warranty *****

    __NONE__  -- Use and Abuse freely

]]

--[[
    ***** SPECIAL THANKS to:
	*    Stan (and team) @ MOHO Scripting -- https://mohoscripting.com
	*    The friendly faces @ Lost Marble / Moho Forum -- https://www.lostmarble.com/forum
]]

-- **************************************************
-- General information about this script
-- **************************************************

SS_RefLayerUpdate = {}

local SS_RefLayerUpdateDialog = {}

function SS_RefLayerUpdate:Name()
    return 'Reference Layer Update'
end

function SS_RefLayerUpdate:Version()
    return '1.1 #5308'
end

function SS_RefLayerUpdate:UILabel()
    return 'Update Reference Layer'
end

function SS_RefLayerUpdate:Creator()
    return "Sam Cogheil (SimplSam)"
end

function SS_RefLayerUpdate:Description()
    return "Update Reference Layer/s"
end

function SS_RefLayerUpdate:ColorizeIcon()
    return true
end

-- **************************************************
-- Is Relevant / Is Enabled
-- **************************************************

function SS_RefLayerUpdate:IsRelevant(moho)
    -- return moho.layer:IsReferencedLayer()
    return true
end

function SS_RefLayerUpdate:IsEnabled(moho)
    return true
end

-- **************************************************
-- Recurring Values
-- **************************************************

SS_RefLayerUpdate.addNewLayers = true
SS_RefLayerUpdate.removeLayers = true
SS_RefLayerUpdate.replaceVectors = false
SS_RefLayerUpdate.replaceBones = true
SS_RefLayerUpdate.replaceBinding = false

-- **************************************************
-- Prefs
-- **************************************************

function SS_RefLayerUpdate:LoadPrefs(prefs)
    self.addNewLayers = prefs:GetBool("SS_RefLayerUpdate.addNewLayers", true)
    self.removeLayers = prefs:GetBool("SS_RefLayerUpdate.removeLayers", true)
    self.replaceVectors = prefs:GetBool("SS_RefLayerUpdate.replaceVectors", false)
    self.replaceBones = prefs:GetBool("SS_RefLayerUpdate.replaceBones", true)
    self.replaceBinding = prefs:GetBool("SS_RefLayerUpdate.replaceBinding", false)
end

function SS_RefLayerUpdate:SavePrefs(prefs)
    prefs:SetBool("SS_RefLayerUpdate.addNewLayers", self.addNewLayers)
    prefs:SetBool("SS_RefLayerUpdate.removeLayers", self.removeLayers)
    prefs:SetBool("SS_RefLayerUpdate.replaceVectors", self.replaceVectors)
    prefs:SetBool("SS_RefLayerUpdate.replaceBones", self.replaceBones)
    prefs:SetBool("SS_RefLayerUpdate.replaceBinding", self.replaceBinding)
end

function SS_RefLayerUpdate:ResetPrefs()
    self.addNewLayers = true
    self.removeLayers = true
    self.replaceVectors = false
    self.replaceBones = true
    self.replaceBinding = false
end

-- **************************************************
-- The guts of this script
-- **************************************************

function SS_RefLayerUpdate:Run(moho)
    local mohodoc = moho.document
    local uuids = {}

    local dlog = SS_RefLayerUpdateDialog:new(moho)
    if (dlog:DoModal() == LM.GUI.MSG_CANCEL) then
        return
    end

    -- Get All Layer UUIDs
    if (self.replaceBinding) then
        local i = 1
        local _layer = mohodoc:LayerByAbsoluteID(i)
        repeat
            uuids[_layer:UUID()] = _layer
            i = i + 1
            _layer = mohodoc:LayerByAbsoluteID(i)
        until _layer == nil
    end

    local refSyncOpts = MOHO.MohoLayerRefSyncOptions:new_local()
    refSyncOpts.fAddNewLayers = self.addNewLayers
    refSyncOpts.fRemoveMissingLayers = self.removeLayers
    refSyncOpts.fReplaceMismatchedBones = self.replaceBones
    refSyncOpts.fReplaceMismatchedVectors = self.replaceVectors

    mohodoc:PrepUndo(nil)
    mohodoc:SetDirty()
    local layerRefInfo = MOHO.MohoLayerRef:new_local()

    local didoBind = {}
    local vecLayer, vecSelect
    local wasLayer = moho.layer

    local function ProcessLayer(layer)
        if (layer:IsReferencedLayer() and layer:IsReferenceOutdated()) then
            layer:UpdateReferencedLayer(refSyncOpts)
        end

        if (layer:IsGroupType()) then
            local groupLayer = moho:LayerAsGroup(layer)
            for j = 0, groupLayer:CountLayers() - 1 do
                ProcessLayer(groupLayer:Layer(j))
            end
        end

        if (self.replaceBinding and layer:LayerType() == MOHO.LT_VECTOR and layer:IsReferencedLayer() and not layer:IsReferenceOutdated() and not didoBind[mohodoc:LayerAbsoluteID(layer)]) then
            layer:GetLayerRefInfo(layerRefInfo)
            if (layerRefInfo.fSameDoc) then
                local refLayer = uuids[layerRefInfo.fLayerUUID:Buffer()]
                if (refLayer) then
                    local mesh = moho:LayerAsVector(layer):Mesh()
                    local refMesh = moho:LayerAsVector(refLayer):Mesh()
                    for j = 0, refMesh:CountPoints() - 1 do
                        mesh:Point(j).fParent = refMesh:Point(j).fParent
                    end
                    didoBind[mohodoc:LayerAbsoluteID(layer)] = true
                    layer:UpdateCurFrame()
                    vecLayer = layer
                    vecSelect = vecSelect or vecLayer:SecondarySelection()
                end
            end
        end
    end

    for i = 0, mohodoc:CountSelectedLayers() - 1 do
        local layer = mohodoc:GetSelectedLayer(i)
        ProcessLayer(mohodoc:GetSelectedLayer(i))
    end

    -- Fix View Refresh if Vector updated but none of selected are Vector (and parental not Ref)
    if (vecLayer and not vecSelect) then
        moho:SetSelLayer(vecLayer, true)
        moho:UpdateUI()
        wasLayer:SetSecondarySelection(false) -- < if not - SetSel deselected if group ??
        moho:SetSelLayer(wasLayer, true)
        vecLayer:SetSecondarySelection(false)
    end
end

-- **************************************************
-- SS_RefLayerUpdateDialog
-- **************************************************

SS_RefLayerUpdateDialog.ADD_NEWMISSING_LAYERS = MOHO.MSG_BASE + 01
SS_RefLayerUpdateDialog.REMOVE_LAYERS = MOHO.MSG_BASE + 02
SS_RefLayerUpdateDialog.REPLACE_MISMATCHED_VECTORS = MOHO.MSG_BASE + 03
SS_RefLayerUpdateDialog.REPLACE_MISMATCHED_BONES = MOHO.MSG_BASE + 04
SS_RefLayerUpdateDialog.RESYNC_POINT_BINDING = MOHO.MSG_BASE + 05

function SS_RefLayerUpdateDialog:new()
    local d = LM.GUI.SimpleDialog('Reference Layer Update', SS_RefLayerUpdateDialog)
    local l = d:GetLayout()

    d.dynamicText1Text = LM.GUI.DynamicText('Layer differences:', 0)
    l:AddChild(d.dynamicText1Text, LM.GUI.ALIGN_LEFT, 0)

    d.addNewLayersCheck = LM.GUI.CheckBox('Add new/missing layers from source', d.ADD_NEWMISSING_LAYERS)
    l:AddChild(d.addNewLayersCheck, LM.GUI.ALIGN_LEFT, 0)

    d.removeLayersCheck = LM.GUI.CheckBox('Remove layers that don\'t exist at source', d.REMOVE_LAYERS)
    l:AddChild(d.removeLayersCheck, LM.GUI.ALIGN_LEFT, 0)

    d.dynamicText2Text = LM.GUI.DynamicText('Vector differences:', 0)
    l:AddChild(d.dynamicText2Text, LM.GUI.ALIGN_LEFT, 0)

    d.replaceVectorsCheck = LM.GUI.CheckBox('Replace mismatched vectors', d.REPLACE_MISMATCHED_VECTORS)
    l:AddChild(d.replaceVectorsCheck, LM.GUI.ALIGN_LEFT, 0)

    d.replaceBindingCheck = LM.GUI.CheckBox('Resync point bindings', d.RESYNC_POINT_BINDING)
    l:AddChild(d.replaceBindingCheck, LM.GUI.ALIGN_LEFT, 0)

    d.dynamicText3Text = LM.GUI.DynamicText('Bone differences:', 0)
    l:AddChild(d.dynamicText3Text, LM.GUI.ALIGN_LEFT, 0)

    d.replaceBonesCheck = LM.GUI.CheckBox('Replace mismatched bones', d.REPLACE_MISMATCHED_BONES)
    l:AddChild(d.replaceBonesCheck, LM.GUI.ALIGN_LEFT, 0)
    return d
end

function SS_RefLayerUpdateDialog:UpdateWidgets(moho)
    self.addNewLayersCheck:SetValue(SS_RefLayerUpdate.addNewLayers)
    self.removeLayersCheck:SetValue(SS_RefLayerUpdate.removeLayers)
    self.replaceVectorsCheck:SetValue(SS_RefLayerUpdate.replaceVectors)
    self.replaceBonesCheck:SetValue(SS_RefLayerUpdate.replaceBones)
    self.replaceBindingCheck:SetValue(SS_RefLayerUpdate.replaceBinding)
end

function SS_RefLayerUpdateDialog:HandleMessage(msg)
end

function SS_RefLayerUpdateDialog:OnOK(moho)
    SS_RefLayerUpdate.addNewLayers = self.addNewLayersCheck:Value()
    SS_RefLayerUpdate.removeLayers = self.removeLayersCheck:Value()
    SS_RefLayerUpdate.replaceVectors = self.replaceVectorsCheck:Value()
    SS_RefLayerUpdate.replaceBones = self.replaceBonesCheck:Value()
    SS_RefLayerUpdate.replaceBinding = self.replaceBindingCheck:Value()
end

Icon
SS - Reference Layer Update+
Listed

Script type: Button/Menu

Uploaded: Sep 15 2022, 10:00

Last modified: Aug 18 2023, 02:38

Script Version: 1.1 #5308

Update Reference Layers + Point Bindings. Accessible via button/keyboard shortcut, and remembers settings (ver 1.10)
Same functionality as the built-in right-click layer menu. But accessible via tool button/keyboard shortcut, and remembers last-used option settings.

V1.10 - ReSync Point Bindings (same file reference only)

Image

Installation Options:

This script, and all other scripts on this site are distributed as free software under the GNU General Public License 3.0 or later.
Downloads count: 728