How do I create a new moho file and copy a layer to it via script?

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

Moderators: Víctor Paredes, Belgarath, slowtiger

User avatar
hayasidist
Posts: 3525
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: How do I create a new moho file and copy a layer to it via script?

Post by hayasidist »

Thanks Wes. I'll aim to put a bug report together on this re FileClose with an active Lua console window...
Lukas wrote: Sat Dec 02, 2023 11:34 am I do wish we could do this without Ctrl+N and running the script twice, because the purpose of this script was actually to keep the animator focussed on the shot. Just hit a button and the layer is sent away for later.
I think this might do it IF you're ok with copy paste the layer's contents rather than duplicate the layer (I have to say that I'm not 100% sure what gets "left behind" in either scenario -- at first sight it looks as though styles, animation etc all get copied over)
User avatar
synthsin75
Posts: 9978
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: How do I create a new moho file and copy a layer to it via script?

Post by synthsin75 »

I thought of copying the contents, but I didn't know if this could occasionally be used on group layers too.
User avatar
synthsin75
Posts: 9978
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: How do I create a new moho file and copy a layer to it via script?

Post by synthsin75 »

This should be a reliable way to get the last selected layer in the original, saved file:

Code: Select all

function LK_ExportLayerToTodoFolder:IsEnabled(moho)
	if (moho.document:Path() ~= "") then
		self.layerID = moho.document:LayerID(moho.layer)
		self.docPath = moho.document:Path()
	end
	self.lastLayer = moho.layer
	self.lastdoc = moho.document:Path()
	return true
end
This only assumes that the newly opened file hasn't been saved yet. So this should work good with just Ctril+N and run the script once.

I also added a check to make sure it won't run on the saved file:

Code: Select all

function LK_ExportLayerToTodoFolder:Run(moho)
	if (moho.document:Path() ~= "") then
		return
	end
	if (self.docPath) then
		local origdoc = moho:LoadDocument(self.docPath)
		local name = origdoc:Layer(self.layerID):Name()
		local newlayer = moho:DuplicateLayer(origdoc:Layer(self.layerID), false)
		newlayer:SetName(name)
		local targetPath = self.todoFolderPath..name..".moho"
		moho:DestroyDocument(origdoc)
		moho:FileSaveAs(targetPath)
		moho:FileClose()
	end
end
User avatar
hayasidist
Posts: 3525
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: How do I create a new moho file and copy a layer to it via script?

Post by hayasidist »

Issue J305 raised regarding the odd behaviours of FileClose() when the Lua console window is open.
Post Reply