I want to create a script that copies the current layer to a new empty document and saves that document in a specific folder. Close the new document and continue working on the original file.
After moho:FileNew() is it possible to edit the newly created moho document and save and close it? To return to the initial document? I've tried numerous things but failed...
Here's what I got currently:
Code: Select all
-- **************************************************
-- Provide Moho with the name of this script object
-- **************************************************
ScriptName = "LK_ExportLayerToTodoFolder"
-- **************************************************
-- General information about this script
-- **************************************************
LK_ExportLayerToTodoFolder = {}
function LK_ExportLayerToTodoFolder:Name()
	return 'Export Layer to Todo-folder'
end
function LK_ExportLayerToTodoFolder:Version()
	return '1.0'
end
function LK_ExportLayerToTodoFolder:UILabel()
	return 'Export Layer to Todo-folder'
end
function LK_ExportLayerToTodoFolder:Creator()
	return 'Lukas Krepel'
end
function LK_ExportLayerToTodoFolder:Description()
	return 'Export selected layer(s) to Todo-folder'
end
-- **************************************************
-- Is Relevant / Is Enabled
-- **************************************************
function LK_ExportLayerToTodoFolder:IsRelevant(moho)
	return true
end
function LK_ExportLayerToTodoFolder:IsEnabled(moho)
	return true
end
-- **************************************************
-- LK_ExportLayerToTodoFolderDialog
-- **************************************************
local LK_ExportLayerToTodoFolderDialog = {}
LK_ExportLayerToTodoFolderDialog.DUMMY = MOHO.MSG_BASE
LK_ExportLayerToTodoFolderDialog.BROWSE_BUTTON = MOHO.MSG_BASE + 1
function LK_ExportLayerToTodoFolderDialog:new(moho)
	local d = LM.GUI.SimpleDialog('Export Layer to Todo-folder', LK_ExportLayerToTodoFolderDialog)
	local l = d:GetLayout()
	-- *
	l:PushH()
		d.browseButton = LM.GUI.Button("Todo Folder", d.BROWSE_BUTTON)
		l:AddChild(d.browseButton)
		d.todoFolderPathText = LM.GUI.DynamicText('Room for a big loooooooooooooooooooooooooooong path', 0)
		l:AddChild(d.todoFolderPathText, LM.GUI.ALIGN_LEFT, 0)
	l:Pop()
	-- *
	l:PushH()
		l:PushV()
			l:AddChild(LM.GUI.StaticText("Filename:"), LM.GUI.ALIGN_RIGHT, 0)
			l:AddPadding(10)
			l:AddChild(LM.GUI.StaticText("Notes:"), LM.GUI.ALIGN_RIGHT, 0)
		l:Pop()
		l:PushV()
			local fileName = moho.layer:Name()..".moho"
			d.filenameTextBox = LM.GUI.TextControl(200, fileName, d.DUMMY, LM.GUI.FIELD_TEXT, nil)
			l:AddChild(d.filenameTextBox, LM.GUI.ALIGN_LEFT, 0)
			l:AddPadding(10)
			LK_ExportLayerToTodoFolder.note1 = "Origin file: "..moho.document:Path()
			LK_ExportLayerToTodoFolder.note2 = "Layer path: "..FO_Utilities:LayerStructure(moho.layer)
			LK_ExportLayerToTodoFolder.note3 = ""
			d.noteTextBox = LM.GUI.TextControl(300, LK_ExportLayerToTodoFolder.note1, d.DUMMY, LM.GUI.FIELD_TEXT, nil)
			l:AddChild(d.noteTextBox, LM.GUI.ALIGN_LEFT, 0)
			d.noteTextBox2 = LM.GUI.TextControl(300, LK_ExportLayerToTodoFolder.note2, d.DUMMY, LM.GUI.FIELD_TEXT, nil)
			l:AddChild(d.noteTextBox2, LM.GUI.ALIGN_LEFT, 0)
			d.noteTextBox3 = LM.GUI.TextControl(300, LK_ExportLayerToTodoFolder.note3, d.DUMMY, LM.GUI.FIELD_TEXT, nil)
			l:AddChild(d.noteTextBox3, LM.GUI.ALIGN_LEFT, 0)
		l:Pop()
	l:Pop()
	return d
end
function LK_ExportLayerToTodoFolderDialog:UpdateWidgets(moho)
	self.todoFolderPathText:SetValue(LK_ExportLayerToTodoFolder.todoFolderPath)
end
-- **************************************************
-- The guts of this script
-- **************************************************
LK_ExportLayerToTodoFolder.note1 = ""
LK_ExportLayerToTodoFolder.note2 = ""
LK_ExportLayerToTodoFolder.note3 = ""
LK_ExportLayerToTodoFolder.todoFolderPath = "/Volumes/megagamma_data/Club Baboo/Rigs/_TODO/"
function LK_ExportLayerToTodoFolder:Run(moho)
	local dlog = LK_ExportLayerToTodoFolderDialog:new(moho)
	if (dlog:DoModal() == LM.GUI.MSG_CANCEL) then
		return
	end
	local sourcePath = moho.document:Path()
	local exportLayer = moho.layer
	local targetPath = self.todoFolderPath..exportLayer:Name()..".moho"
	moho:FileNew()
	moho:FileSaveAs(targetPath)
	moho:FileOpen(targetPath)
	moho:PlaceLayerBehindAnother(exportLayer, moho.document:Layer(0))
	moho:DeleteLayer(moho.document:Layer(0))
	--
	local notes = { self.note1, self.note2, self.note3 }
	local i = #notes
	while i > 0 do
		local note = notes[i]
		if note ~= "" then
			local noteLayer = moho:CreateNewLayer(MOHO.LT_NOTE, false)
			noteLayer = moho:LayerAsNote(noteLayer)
			noteLayer:SetNoteText(note)
			noteLayer:SetName(note)
			local vec3 = LM.Vector3:new_local()
			vec3.y = -0.1 * (i-1)
			noteLayer.fTranslation:SetValue(0, vec3)
		end
		i = i - 1
	end
	moho:FileSave()
	moho:FileClose()
	moho:FileOpen(sourcePath)
end(It needs FO_Utilities)
(I tried a different method by saving the whole document as a duplicate and deleting 100s of layers except the selected one I needed, saving it as a new file and re-opening the initial file, but opening takes a LONG time because it's a huge file dependant on huge images from a server. So that's not an option)