-- **************************************************
-- Provide Moho with the name of this script object
-- **************************************************

ScriptName = "SZ_BakePoints"

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

SZ_BakePoints = {}

function SZ_BakePoints:Name()
	return self:Localize('UILabel')
end

function SZ_BakePoints:Version()
	return '1.0'
end

function SZ_BakePoints:UILabel()
	return self:Localize('UILabel')
end

function SZ_BakePoints:Creator()
	return 'Stan from 2danimator.ru'
end

function SZ_BakePoints:Description()
	return self:Localize('Description')
end

function SZ_BakePoints:ColorizeIcon()
	return false
end

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

function SZ_BakePoints:IsRelevant(moho)
	return true
end

function SZ_BakePoints:IsEnabled(moho)
	return true
end

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

SZ_BakePoints.keyPosition = true
SZ_BakePoints.keyWidth = true
SZ_BakePoints.keyCurvature = true

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

function SZ_BakePoints:LoadPrefs(prefs)
	self.keyPosition = prefs:GetBool("SZ_BakePoints.keyPosition", true)
	self.keyWidth = prefs:GetBool("SZ_BakePoints.keyWidth", true)
	self.keyCurvature = prefs:GetBool("SZ_BakePoints.keyCurvature", true)
end

function SZ_BakePoints:SavePrefs(prefs)
	prefs:SetBool("SZ_BakePoints.keyPosition", self.keyPosition)
	prefs:SetBool("SZ_BakePoints.keyWidth", self.keyWidth)
	prefs:SetBool("SZ_BakePoints.keyCurvature", self.keyCurvature)
end

function SZ_BakePoints:ResetPrefs()
	self.keyPosition = true
	self.keyWidth = true
	self.keyCurvature = true
end

-- **************************************************
-- SZ_BakePointsDialog
-- **************************************************

local SZ_BakePointsDialog = {}


function SZ_BakePointsDialog:new()
	local d = LM.GUI.SimpleDialog(SZ_BakePoints:Localize('UILabel') .. ' v' .. SZ_BakePoints:Version(), SZ_BakePointsDialog)
	local l = d:GetLayout()

	d.bakeTheFollowingChannelsText = LM.GUI.DynamicText(SZ_BakePoints:Localize('Bake the following channels:'), 0)
	l:AddChild(d.bakeTheFollowingChannelsText, LM.GUI.ALIGN_LEFT, 0)

	d.positionCheckbox = LM.GUI.CheckBox(SZ_BakePoints:Localize('Position'), 0)
	l:AddChild(d.positionCheckbox, LM.GUI.ALIGN_LEFT, 0)
	
	d.widthCheckbox = LM.GUI.CheckBox(SZ_BakePoints:Localize('Width'), 0)
	l:AddChild(d.widthCheckbox, LM.GUI.ALIGN_LEFT, 0)

	d.curvatureCheckbox = LM.GUI.CheckBox(SZ_BakePoints:Localize('Curvature'), 0)
	l:AddChild(d.curvatureCheckbox, LM.GUI.ALIGN_LEFT, 0)
	return d
end

function SZ_BakePointsDialog:UpdateWidgets(moho)
	self.positionCheckbox:SetValue(SZ_BakePoints.keyPosition)
	self.widthCheckbox:SetValue(SZ_BakePoints.keyWidth)
	self.curvatureCheckbox:SetValue(SZ_BakePoints.keyCurvature)
end

function SZ_BakePointsDialog:OnOK(moho)
	SZ_BakePoints.keyPosition = self.positionCheckbox:Value()
	SZ_BakePoints.keyWidth = self.widthCheckbox:Value()
	SZ_BakePoints.keyCurvature = self.curvatureCheckbox:Value()
end

function SZ_BakePointsDialog:HandleMessage(msg)
	
end


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

function SZ_BakePoints:Run(moho)
	local dlog = SZ_BakePointsDialog:new(moho)
	if (dlog:DoModal() == LM.GUI.MSG_CANCEL) then
		return
	end
	
	moho.document:PrepMultiUndo()
	moho.document:SetDirty()
	
	local count = 0
	repeat
		local layer = moho.document:LayerByAbsoluteID(count)
		if layer then
			count = count + 1
			local vectorLayer = moho:LayerAsVector(layer)
			if vectorLayer and (vectorLayer:SecondarySelection() or vectorLayer:IsAncestorSelected()) then
				self:BakeLayer(moho, vectorLayer)
			end
		end
	until not layer
		
	local vectorLayer = moho:LayerAsVector(moho.layer)	
end


function SZ_BakePoints:BakeLayer(moho, vectorLayer)
	local keyPosition = self.keyPosition
	local keyWidth = self.keyWidth
	local keyCurvature = self.keyCurvature
	
	local mesh = vectorLayer:Mesh()
	for i=0, mesh:CountCurves()-1 do
		local curve = mesh:Curve(i)
		for q=0, curve:CountPoints()-1 do
			if keyCurvature then
				local c = curve:GetCurvature(q, moho.layerFrame)
				curve:SetCurvature(q, c, moho.layerFrame)
			end
			if keyPosition or keyWidth then
				local point = curve:Point(q)
				if keyPosition then
					self:CreateKeyframe(moho, point.fAnimPos)
				end
				if keyWidth then
					local width = point.fWidth
					if (width.value < 0) then
						local lineWidth = 0
						for f = 0, mesh:CountShapes() - 1 do
							local shape = mesh:Shape(f)
							if (shape.fHasOutline and shape:ContainsPoint(mesh:PointID(point))) then
								lineWidth = shape.fMyStyle.fLineWidth
								if ((shape.fInheritedStyle ~= nil) and shape.fInheritedStyle.fDefineLineWidth and (not shape.fMyStyle.fDefineLineWidth)) then
									lineWidth = shape.fInheritedStyle.fLineWidth
								end
								if ((shape.fInheritedStyle2 ~= nil) and shape.fInheritedStyle2.fDefineLineWidth and (not shape.fMyStyle.fDefineLineWidth)) then
									lineWidth = shape.fInheritedStyle2.fLineWidth
								end
								break
							end
						end
						width:SetValue(moho.layerFrame, lineWidth)
					else
						width:SetValue(moho.layerFrame, width:GetValue(moho.layerFrame))
					end
				end
			end
		end
	end
	if keyCurvature then 
		moho:NewKeyframe(CHANNEL_CURVE)
	end
end


function SZ_BakePoints:CreateKeyframe(moho, channel)
	--[[
	NOTE:
	The StoreValue() method is buggy if used along with smart bones!
	That's why SetValue() is used instead.
	--]]
	 	
	-- Check if channel has the AreDimensionsSplit method
	if channel.AreDimensionsSplit then
		local andTheyAreSplit = channel:AreDimensionsSplit()
		if andTheyAreSplit then
			for i = 0, 2 do
				subChannel = channel:DimensionChannel(i)
				if subChannel then
					subChannel:SetValue(moho.layerFrame, subChannel:GetValue(moho.layerFrame))
				end
			end
		else
			channel:SetValue(moho.layerFrame, channel:GetValue(moho.layerFrame))
		end
	else
		channel:SetValue(moho.layerFrame, channel:GetValue(moho.layerFrame))
	end
end

-- **************************************************
-- Localization
-- **************************************************

function SZ_BakePoints:Localize(text)
	local phrase = {}

	phrase['Description'] = 'Bakes all points in the selected layer(s) in the current frame.'
	phrase['UILabel'] = 'Bake Points'

	phrase['Bake the following channels:'] = 'Bake the following channels:'
	phrase['Position'] = 'Position'
	phrase['Width'] = 'Width'
	phrase['Curvature'] = 'Curvature'

	local fileWord = MOHO.Localize("/Menus/File/File=File")
	if fileWord == "Файл" then
		phrase['Description'] = 'Запекает точки в выделенных слоях в текущем кадре.'
		phrase['UILabel'] = 'Запечь точки'

		phrase['Bake the following channels:'] = 'Запечь следующие каналы:'
		phrase['Position'] = 'Позиция'
		phrase['Width'] = 'Ширина'
		phrase['Curvature'] = 'Кривизна'
	end

	return phrase[text]
end

Icon
SZ Bake Points
Listed

Author: Stan View Script
Script type: Button/Menu

Uploaded: Jan 06 2022, 20:43

Bakes all points in the selected layer(s) in the current frame.
Select one or several layers (vector, bone, or other group) and run the script. It will create keyframes for all points in the current frame. You can choose what channels to bake - Position, Width, Curvature.

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: 61