Can a script select a layer?

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
Víctor Paredes
Site Admin
Posts: 5646
Joined: Wed Jan 26, 2005 12:18 am
Location: Barcelona/Chile
Contact:

Can a script select a layer?

Post by Víctor Paredes »

There are four or five layers I need to constantly select (in a project with hundreds), do you know if there's a way to select a layer using a button script in the tool palette?

I'm thinking on this idea:
- Put a "selection1" tag in the layer I want to select
- Have a button in the tool bar which every time I press it, it will select the layer with the "selection1" tag (I could also add a shortcut for it)

Since I have room in my tools palette, I could have several buttons referring to different tags.
Do you think that is possible?
Image Image Image Image
Moho Product Manager

www.mohoanimation.com
Rigged animation supervisor in My father's dragon - Lead Moho artist in Wolfwalkers - Cartoon Saloon - My personal Youtube Channel
User avatar
hayasidist
Posts: 3492
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: Can a script select a layer?

Post by hayasidist »

yes -- there's a script interface command ScriptInterface:SetSelLayer(layer, multiSelect, allowDeselect) -- scan the layers for the required tag and use it!..

e.g.

Code: Select all

function hs_sel_by_tag (moho, tag)
		if not tag then return end
		local i = 0
		repeat
			layer = moho.document:LayerByAbsoluteID(i)
			if layer then
				i = i + 1
				if layer:UserTags() == tag  then -- or contains string (e.g. use string.find)  or ..
					moho:SetSelLayer(layer) -- (or for multiselect (layer,true) and no following "break"
					break
				end
			end
		until not layer
		return
end
What I'd do is put the above into a file as utility function i.e. hs_sel_by_tag.lua goes in the utility folder

(that core code has been tested -- but it doesn't look to see if there are multiple layers with the same tag -- many ways to address that -- e.g. multi-select; or flag an error; or start from the last selected and move on rather than always start at 0; or .. depends on what you exactly want)

and the guts of the buttons are

local required_tagname = <whatever>
hs_sel_by_tag (moho, required_tagname)

you could (additionally) have a "general" button that asked the user for the required tag

Stan's new script generator should take the strain out of that!

hope that's helpful.
User avatar
synthsin75
Posts: 9934
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Can a script select a layer?

Post by synthsin75 »

Not sure if this is exactly what you have in mind, but try this out, Victor: https://sites.google.com/site/synthsin/ ... ects=0&d=1

With this tool selected, if the first layer tag is a single-character (and available as a shortcut in Moho), hitting that key will select that layer.

It's up to the user to make sure the shortcuts they use are available and not duplicated, and uppercase (shift+key) and lowercase are separate shortcuts.
User avatar
Víctor Paredes
Site Admin
Posts: 5646
Joined: Wed Jan 26, 2005 12:18 am
Location: Barcelona/Chile
Contact:

Re: Can a script select a layer?

Post by Víctor Paredes »

hayasidist wrote:yes -- there's a script interface command ScriptInterface:SetSelLayer(layer, multiSelect, allowDeselect) -- scan the layers for the required tag and use it!
(...)
Thank you very much, hayasidist!
This will be very useful for me.
synthsin75 wrote:Not sure if this is exactly what you have in mind, but try this out, Victor: https://sites.google.com/site/synthsin/ ... ects=0&d=1
With this tool selected, if the first layer tag is a single-character (and available as a shortcut in Moho), hitting that key will select that layer.
It's up to the user to make sure the shortcuts they use are available and not duplicated, and uppercase (shift+key) and lowercase are separate shortcuts.
Wes, this is just fantastic! Thank you very very much! I will start creating all my rigs with this tool in mind!
Image Image Image Image
Moho Product Manager

www.mohoanimation.com
Rigged animation supervisor in My father's dragon - Lead Moho artist in Wolfwalkers - Cartoon Saloon - My personal Youtube Channel
User avatar
synthsin75
Posts: 9934
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Can a script select a layer?

Post by synthsin75 »

I've vastly improved this tool.
Same link: https://sites.google.com/site/synthsin/ ... ects=0&d=1
Full description here: viewtopic.php?f=12&t=32641
User avatar
Greenlaw
Posts: 9191
Joined: Mon Jun 19, 2006 5:45 pm
Location: Los Angeles
Contact:

Re: Can a script select a layer?

Post by Greenlaw »

I was just thinking there should be an easier way to select deeply nested layers, like an editable/clickable list of frequently selected layers or something like that. Then I remembered this thread.

Will give the script a try today. Sounds like it might do what I need. :)
User avatar
synthsin75
Posts: 9934
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Can a script select a layer?

Post by synthsin75 »

Hopefully it's a bit faster than opening and scrolling through a list of layers.
Post Reply