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

ScriptName = "SZ_AddKeyframe"

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

SZ_AddKeyframe = {}

function SZ_AddKeyframe:Name()
	return "SZ_AddKeyframe"
end

function SZ_AddKeyframe:Version()
	return "1.2"
end

function SZ_AddKeyframe:Description()
	return "Adds a keyframe in bone/point/layer channels"
end

function SZ_AddKeyframe:Creator()
	return "Stan from 2danimator.ru"
end

function SZ_AddKeyframe:UILabel()
	return "Add Keyframe"
end

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


function SZ_AddKeyframe:IsEnabled(moho)
	return true
end

function SZ_AddKeyframe:IsRelevant(moho)
	return true
end

function SZ_AddKeyframe:Run(moho)
	moho.document:PrepUndo(moho.layer)
	moho.document:SetDirty()
	
	local keyBones = SZ_AddKeyframe_Settings.keyBones
	local keyPoints = SZ_AddKeyframe_Settings.keyPoints
	local keyWidth = SZ_AddKeyframe_Settings.keyWidth
	local keyCurvature = SZ_AddKeyframe_Settings.keyCurvature
	local keyLayer = SZ_AddKeyframe_Settings.keyLayer
	local keyRotation = SZ_AddKeyframe_Settings.keyRotation
		
	local boneLayer = moho:LayerAsBone(moho.layer)
	if boneLayer and keyBones then
		local skel = boneLayer:Skeleton()
		for i=0, skel:CountBones()-1 do
			local bone = skel:Bone(i)
			self:CreateKeyframe(moho, bone.fAnimPos)
			self:CreateKeyframe(moho, bone.fAnimAngle)
			self:CreateKeyframe(moho, bone.fAnimScale)
		end
	end
	
	local vectorLayer = moho:LayerAsVector(moho.layer)
	if vectorLayer then
		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 keyPoints or keyWidth then
					local point = curve:Point(q)
					if keyPoints 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
	
	local layer = moho.layer
	if keyRotation then
		self:CreateKeyframe(moho, layer.fRotationX)
		self:CreateKeyframe(moho, layer.fRotationY)
	end
	if keyLayer then
		self:CreateKeyframe(moho, layer.fTranslation)
		self:CreateKeyframe(moho, layer.fRotationZ)
		self:CreateKeyframe(moho, layer.fScale)
	end
		
end

function SZ_AddKeyframe: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

function SZ_AddKeyframe:Localize(text)
	local phrase = {}
	
	phrase['description'] = "Creates keyframes in the given channels"
	phrase['uiLabel'] = "Add Keyframe"
	
	local fileWord = MOHO.Localize("/Menus/File/File=File")
    if fileWord == "Файл" then
        phrase['description'] = "Пробивает ключи в заданных каналах"
		phrase['uiLabel'] = "Пробивка Ключей"
    end

    return phrase[text]
end


Icon
SZ Add Keyframe
Listed

Author: Stan View Script
Script type: Button/Menu

Uploaded: Sep 21 2020, 22:24

An alternative to the built-in Crl+F command.
Unlike the Ctrl+F command, this button adds keyframes to the defined channels even if they are split or if there's no animation added yet. It works together with the Add Keyframe Settings tool which holds the options on what should be keyframed and what shouldn't.

You can assign a hotkey for the keys creation.

Image

Image

Image
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: 17