Merge Layers Script?

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

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
User avatar
DK
Posts: 2854
Joined: Mon Aug 09, 2004 6:06 am
Location: Australia

Merge Layers Script?

Post by DK »

Has anyone written a Merge Layers Script to save constantly cutting/pasting and deleting layers?
Cheers
D.K
User avatar
synthsin75
Posts: 9973
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Merge Layers Script?

Post by synthsin75 »

DK wrote: Tue Dec 14, 2021 2:08 am Has anyone written a Merge Layers Script to save constantly cutting/pasting and deleting layers?
Cheers
D.K
Not that I know of. Would you just need all selected vector layers to be merged to one new layer? If so, that's pretty easy to whip up.
User avatar
SimplSam
Posts: 1048
Joined: Thu Mar 13, 2014 5:09 pm
Location: London, UK
Contact:

Re: Merge Layers Script?

Post by SimplSam »

The only real gotcha I can think of is: What do you do with Layer differences? i.e. Transforms, Properties, Anim Keyframes, References ... or are these just ignored?
Moho 14.1 » Win 11 Pro 64GB » NVIDIA GTX 1080ti 11GB
Moho 14.1 » Mac mini 2012 8GB » macOS 10.15 Catalina
Tube: SimplSam


Sam
User avatar
Greenlaw
Posts: 9269
Joined: Mon Jun 19, 2006 5:45 pm
Location: Los Angeles
Contact:

Re: Merge Layers Script?

Post by Greenlaw »

I was reading about A Evseeva's Reset Layer Origin. I haven't tried it yet but it sounds like it resets the origin position while preserving the visual position of the vectors, so I'm assuming it's resetting the origin and also offsetting the vector art with a transform.

I guess something like that would have to occur for each layer selected for the merge?
User avatar
synthsin75
Posts: 9973
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Merge Layers Script?

Post by synthsin75 »

EDIT: This version doesn't select the curves for you. Please use the second version below.


Real quick, just doing as described, e.g. cut, paste, and delete layers:

Code: Select all

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

ScriptName = "Syn_MergeVectors"

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

Syn_MergeVectors = {}

function Syn_MergeVectors:Name()
	return "Merge Vectors"
end

function Syn_MergeVectors:Version()
	return "1.0"
end

function Syn_MergeVectors:Description()
	return "Merge all selected vector layers"
end

function Syn_MergeVectors:Creator()
	return "(c)2021 J.Wesley Fowler (synthsin75)"
end

function Syn_MergeVectors:UILabel()
	return("SYN: Merge Vectors")
end

-- **************************************************
-- Recurring values
-- **************************************************

Syn_MergeVectors.delete = true		--true to delete merged layers, otherwise false

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

function Syn_MergeVectors:Run(moho)
	moho.document:PrepUndo()
	moho.document:SetDirty()
	
	local layers = {}
	local count = 0
	repeat
		local layer = moho.document:LayerByAbsoluteID(count)
		if layer then
			count = count + 1
			if (layer:SecondarySelection() and layer:LayerType() == MOHO.LT_VECTOR) then
				table.insert(layers, moho:LayerAsVector(layer))
			end
		end
	until not layer
	local merge = moho:CreateNewLayer(MOHO.LT_VECTOR, false)
	merge:SetName("Merged "..count.." vector layers")
	moho:SetSelLayer(merge)
	for i,v in ipairs(layers) do
		moho:Copy(v:Mesh())
		moho:Paste()
	end
	if (not self.delete) then
		moho:SetSelLayer(layers[#layers])
	end
	for i=#layers, 1, -1 do
		if (self.delete) then
			moho:DeleteLayer(layers[i])
		else
			layers[i]:SetSecondarySelection(true)
		end
	end
end
If you don't want it automatically deleting the layers, you can change Syn_MergeVectors.delete to false.
This doesn't handle any layer transforms, effects, parent transforms, animation, etc..
Last edited by synthsin75 on Wed Dec 27, 2023 8:48 pm, edited 1 time in total.
User avatar
SimplSam
Posts: 1048
Joined: Thu Mar 13, 2014 5:09 pm
Location: London, UK
Contact:

Re: Merge Layers Script?

Post by SimplSam »

Haven't tried it (yet) - but you appear to be merging down into a new Layer. I would have thought a preference might be to merge into an existing (last selected) target layer.
Moho 14.1 » Win 11 Pro 64GB » NVIDIA GTX 1080ti 11GB
Moho 14.1 » Mac mini 2012 8GB » macOS 10.15 Catalina
Tube: SimplSam


Sam
User avatar
synthsin75
Posts: 9973
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Merge Layers Script?

Post by synthsin75 »

SimplSam wrote: Tue Dec 14, 2021 8:50 am Haven't tried it (yet) - but you appear to be merging down into a new Layer. I would have thought a preference might be to merge into an existing (last selected) target layer.
If that works better for people, DK?, I can always do that instead. But if anyone wants to leave the merged layers unaltered and undeleted (non-destructive merge), making a new layer keeps that an option.
User avatar
synthsin75
Posts: 9973
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Merge Layers Script?

Post by synthsin75 »

Here's a version with just a destructive option:

Code: Select all

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

ScriptName = "Syn_MergeVectors"

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

Syn_MergeVectors = {}

function Syn_MergeVectors:Name()
	return "Merge Vectors"
end

function Syn_MergeVectors:Version()
	return "1.1"
end

function Syn_MergeVectors:Description()
	return "Merge all selected vector layers"
end

function Syn_MergeVectors:Creator()
	return "(c)2021 J.Wesley Fowler (synthsin75)"
end

function Syn_MergeVectors:UILabel()
	return("SYN: Merge Vectors")
end

-- **************************************************
-- Recurring values
-- **************************************************

Syn_MergeVectors.destructive = true		--true to delete merged layers, otherwise false

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

function Syn_MergeVectors:Run(moho)
	moho.document:PrepUndo()
	moho.document:SetDirty()
	
	local layers = {}
	local count = 0
	repeat
		local layer = moho.document:LayerByAbsoluteID(count)
		if layer then
			count = count + 1
			if (layer:SecondarySelection() and layer:LayerType() == MOHO.LT_VECTOR) then
				table.insert(layers, moho:LayerAsVector(layer))
			end
		end
	until not layer
	local merge
	if (not self.destructive) then
		merge = moho:CreateNewLayer(MOHO.LT_VECTOR, false)
		merge:SetName("Merged "..count.." vector layers")
	else
		merge = moho.layer
	end
	moho:SetSelLayer(merge)
	for i,v in ipairs(layers) do
		if (v ~= merge) then
			v:Mesh():SelectAll()
			moho:Copy(v:Mesh())
			moho:Paste()
		end
	end
	if (not self.destructive) then
		moho:SetSelLayer(layers[1])
	end
	for i=#layers, 1, -1 do
		if (self.destructive and layers[i] ~= merge) then
			moho:DeleteLayer(layers[i])
		elseif (not self.destructive) then
			layers[i]:SetSecondarySelection(true)
		end
	end
end
If Syn_MergeVectors.destructive is set to true, it merges to the last selected layer, if false, it creates a new layer, so it's completely non-destructive.
User avatar
DK
Posts: 2854
Joined: Mon Aug 09, 2004 6:06 am
Location: Australia

Re: Merge Layers Script?

Post by DK »

Wow...didn't think I would get this response....reason I asked is that I have been importing AI and SVG layers from other illustration softwares in order to assemble a single Moho layer and it gets rather slow going when you have to cut/paste elements onto one layer then go back and delete the unused layers. It struck me how easy it could be just to simple merge them, destructively of course. Wasn't really concerned about added elements such as references etc.
Thanks so much for this Wes and SimpleSam. I will give it a go.
Cheers
D.K
User avatar
DK
Posts: 2854
Joined: Mon Aug 09, 2004 6:06 am
Location: Australia

Re: Merge Layers Script?

Post by DK »

Wes...that works perfectly. Thanks so much. I put it in the menu scripts under Layer effects.
It doesn't sound like a huge time saving script but every little bit counts when you are under
the pump.
Cheers
D.K
User avatar
synthsin75
Posts: 9973
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Merge Layers Script?

Post by synthsin75 »

Glad to help, David. I'm sure the little bit of time it took me to write will save you a huge amount of time. That's time well spent, IMO.
Post Reply