Page 1 of 1

Merge Layers Script?

Posted: Tue Dec 14, 2021 2:08 am
by DK
Has anyone written a Merge Layers Script to save constantly cutting/pasting and deleting layers?
Cheers
D.K

Re: Merge Layers Script?

Posted: Tue Dec 14, 2021 2:12 am
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.

Re: Merge Layers Script?

Posted: Tue Dec 14, 2021 2:33 am
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?

Re: Merge Layers Script?

Posted: Tue Dec 14, 2021 2:52 am
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?

Re: Merge Layers Script?

Posted: Tue Dec 14, 2021 2:59 am
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..

Re: Merge Layers Script?

Posted: Tue Dec 14, 2021 8:50 am
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.

Re: Merge Layers Script?

Posted: Tue Dec 14, 2021 6:01 pm
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.

Re: Merge Layers Script?

Posted: Tue Dec 14, 2021 6:43 pm
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.

Re: Merge Layers Script?

Posted: Wed Dec 15, 2021 2:01 am
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

Re: Merge Layers Script?

Posted: Wed Dec 15, 2021 2:11 am
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

Re: Merge Layers Script?

Posted: Wed Dec 15, 2021 2:24 am
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.