LK_AutoShowHideBones: Auto show/hide shy bones and vitruvian bones on entering/leaving frame 0

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
Lukas
Posts: 1297
Joined: Fri Apr 09, 2010 9:00 am
Location: Netherlands
Contact:

LK_AutoShowHideBones: Auto show/hide shy bones and vitruvian bones on entering/leaving frame 0

Post by Lukas »

This might be useful depending on your workflow and rigging style.
Save this script as LK_AutoShowHideBones.lua in your custom content folder in: /moho pro/scripts/tool/
It won't show up in your tools panel, but it will automatically reveal all shy bones and vitruvian bones on entering frame 0, and it will hide them on leaving frame 0.
🙂 - Lukas

Code: Select all

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

ScriptName = "LK_AutoShowHideBones"

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

LK_AutoShowHideBones = {}

function LK_AutoShowHideBones:Name()
	return "LK_AutoShowHideBones"
end

function LK_AutoShowHideBones:Version()
	return "0.1"
end

function LK_AutoShowHideBones:Description()
	return "LK_AutoShowHideBones"
end

function LK_AutoShowHideBones:Creator()
	return "Lukas Krepel, Frame Order"
end

function LK_AutoShowHideBones:UILabel()
	return("LK_AutoShowHideBones")
end

-- **************************************************
-- The guts of this script
-- **************************************************
function LK_AutoShowHideBones:IsRelevant(moho)
	if MohoMode ~= nil then
		if MohoMode:Animation() then
			self:Run(moho)
		end
	else
		self:Run(moho)
	end
	return false
end

function LK_AutoShowHideBones:Run(moho)
	-- * Disable in smartbone actions:
	local action = moho.document:GetSelectedLayer(i):CurrentAction()
	if moho.layer:IsSmartBoneAction(action) then
		return
	end
	
	local hideBones = true
	if moho.frame == 0 and self.prevFrame ~= 0 then
		-- * Entered frame 0!
		hideBones = false
	elseif moho.frame ~= 0 and self.prevFrame == 0 then
		-- * Left frame 0!
		hideBones = true
	else
		-- * Do nothing:
		return false
	end
	-- *
	local skeletons = self:DocumentSkeletons(moho)
	for i = 1, #skeletons do
		local skel = skeletons[i]
		for j = 0, skel:CountBones() - 1 do
			local bone = skel:Bone(j)
			if bone.fShy then
				bone.fHidden = hideBones
			else
				bone.fHidden = false
			end
		end
		for j = 0, skel:CountGroups() - 1 do
			local group = skel:Group(j)
			group.fEnabled = hideBones
		end
	end
	-- *
	if hideBones then
		moho.layer:UpdateCurFrame()
	end
	self.prevFrame = moho.frame
end

-- **************************************************
-- Returns a table with all skeletons in the current document
-- **************************************************
function LK_AutoShowHideBones:DocumentSkeletons(moho)
	local skeletons = {}
	local count = 0
	repeat
		local layer = moho.document:LayerByAbsoluteID(count)
		if layer then
    		count = count + 1
			local skel = layer:ControllingSkeleton()
			--
			if not skel and layer:IsBoneType() then
				skel = moho:LayerAsBone(layer):Skeleton()
			end
			--
			if not table.contains(skeletons, skel) then
			  	table.insert(skeletons, skel)
			end
		end
	until not layer
	return skeletons
end

-- **************************************************
-- Check whether a table contains an element
-- **************************************************
function table.contains(table, element)
	if table ~= nil then
		for _, value in pairs(table) do
			if value == element then
	    		return true
	    	end
	  	end
	end
  	return false
end
Last edited by Lukas on Fri May 21, 2021 2:14 pm, edited 3 times in total.
User avatar
Greenlaw
Posts: 9262
Joined: Mon Jun 19, 2006 5:45 pm
Location: Los Angeles
Contact:

Re: LK_AutoShowHideBones: Auto show/hide shy bones and vitruvian bones on entering/leaving frame 0

Post by Greenlaw »

This is a very interesting idea Lukas.

I frequently use a Shy button on my MQC panel to toggle bone visibility, but now that I'm thinking about it, I typically do that only on frame 0.

Will give your script a try. Thanks for creating it and making it available.
User avatar
synthsin75
Posts: 9973
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: LK_AutoShowHideBones: Auto show/hide shy bones and vitruvian bones on entering/leaving frame 0

Post by synthsin75 »

Doesn't work until ControllingSkeleton() finds a layer controlled by the skeleton.
Instead of: local skel = layer:ControllingSkeleton()
it's simpler to do:

Code: Select all

			local skel
			if layer:IsBoneType() then
				skel = moho:LayerAsBone(layer):Skeleton()
			end
Also, what is MohoMode:Animation()?
User avatar
Lukas
Posts: 1297
Joined: Fri Apr 09, 2010 9:00 am
Location: Netherlands
Contact:

Re: LK_AutoShowHideBones: Auto show/hide shy bones and vitruvian bones on entering/leaving frame 0

Post by Lukas »

Greenlaw wrote: Sun May 02, 2021 4:02 pmWill give your script a try. Thanks for creating it and making it available.
Let me know how it works for you. It also reveals non-shy hidden bones by the way. Just something to be aware of. I like it that way, but it might not suit all your files if you hide stuff intentionally (I always make bones Shy if I don't intend to animate them on the main timeline). Also note, you can still toggle shy bones manually.
synthsin75 wrote: Sun May 02, 2021 5:38 pm Doesn't work until ControllingSkeleton() finds a layer controlled by the skeleton.
To fix that [...]
Thanks, I had not bumped into that situation yet, added it to the original post.
synthsin75 wrote: Sun May 02, 2021 5:38 pmAlso, what is MohoMode:Animation()?
Ah you don't really need it, that's why it checks if MohoMode == nil first. I use it to give the users 4 modes to choose from. 'Vanilla', 'Animation', 'Rigging' and 'Experimental'. MohoMode:Animation() returns true if it's set to either animation, rigging or experimental... 'Vanilla' hides all custom tools and unhides all Mike's tools, sometimes I use it to see if my own tools are the problem or if it's Moho. 'Animation' (hides a bunch of stuff like add bone), basically something I'd give to a freelancers that only needs to animate without too much moho knowledge. 'Rigging' (what we use almost all the time) and 'Experimental' mode which has a bunch of tools that are WIP or buggy. I can share it if anyone's interested, but I figured it was probably more confusing than useful to throw it out here. 🙃
User avatar
synthsin75
Posts: 9973
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: LK_AutoShowHideBones: Auto show/hide shy bones and vitruvian bones on entering/leaving frame 0

Post by synthsin75 »

Lukas wrote: Sun May 02, 2021 6:07 pm I can share it if anyone's interested, but I figured it was probably more confusing than useful to throw it out here. 🙃
Yeah, if I did that, I'd want to customize it to my own needs. I assume you're just setting the various tools as relevant/non-relevant?
User avatar
Greenlaw
Posts: 9262
Joined: Mon Jun 19, 2006 5:45 pm
Location: Los Angeles
Contact:

Re: LK_AutoShowHideBones: Auto show/hide shy bones and vitruvian bones on entering/leaving frame 0

Post by Greenlaw »

Lukas wrote: Sun May 02, 2021 6:07 pm Let me know how it works for you. It also reveals non-shy hidden bones by the way. Just something to be aware of. I like it that way, but it might not suit all your files if you hide stuff intentionally (I always make bones Shy if I don't intend to animate them on the main timeline). Also note, you can still toggle shy bones manually.
Good to know! I routinely hide bones in a rig but I use Shy for that because it's so easy to globally toggle this option on and off (especially when using MQC and a stylus.) I figure I'll leave Hide for special situations, but TBH, I haven't really used it in any special way yet.

Ideally, I'd like to see individual Bones and 'Bone Groups' get a keyframeable visibility property (natively, I mean). But, to a large degree, Vitruvian Bones actually addresses why I've wanted this feature. :P

I'll try the script later today and let you know how it goes.
User avatar
Lukas
Posts: 1297
Joined: Fri Apr 09, 2010 9:00 am
Location: Netherlands
Contact:

Re: LK_AutoShowHideBones: Auto show/hide shy bones and vitruvian bones on entering/leaving frame 0

Post by Lukas »

synthsin75 wrote: Sun May 02, 2021 7:47 pmYeah, if I did that, I'd want to customize it to my own needs. I assume you're just setting the various tools as relevant/non-relevant?
Yeah exactly. Here's the tool to choose modes: LK_MohoMode.zip
And next to it there's a file called LK_DisableVanilla.lua in which there's a bunch of stuff like this. It's basically an over complicated way of managing available tools 😅 Can't recommend it for most users to be honest.

Code: Select all

-- **************************************************
--  LM_AddBone is replaced by LK_AddBone.
-- **************************************************
function LM_AddBone:IsRelevant(moho)
	if MohoMode:Vanilla() then
		if (moho:DisableDrawingTools()) then
			return false
		end
		local skel = moho:Skeleton()
		if (skel == nil) then
			return false
		end
		return true
	else
		return false
	end
end
User avatar
synthsin75
Posts: 9973
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: LK_AutoShowHideBones: Auto show/hide shy bones and vitruvian bones on entering/leaving frame 0

Post by synthsin75 »

Yeah, that makes for a lot of modded tools.
User avatar
Lukas
Posts: 1297
Joined: Fri Apr 09, 2010 9:00 am
Location: Netherlands
Contact:

Re: LK_AutoShowHideBones: Auto show/hide shy bones and vitruvian bones on entering/leaving frame 0

Post by Lukas »

synthsin75 wrote: Mon May 03, 2021 10:51 am Yeah, that makes for a lot of modded tools.
Yeah, that's why I'm not recommending it. But just to be clear: I only replace the IsRelevant functions of the original tools that I'm hiding or replacing, all in 1 single file. So it's not a huge hassle. I made the mode system mostly to be able to hide tools that animators just won't ever need to worry about and to save up screenspace, because there's too many icons to dock the tool panel on lower resolutions sometimes: LK_MohoMode.mov 🙈
User avatar
synthsin75
Posts: 9973
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: LK_AutoShowHideBones: Auto show/hide shy bones and vitruvian bones on entering/leaving frame 0

Post by synthsin75 »

Lukas wrote: Mon May 03, 2021 1:58 pm But just to be clear: I only replace the IsRelevant functions of the original tools that I'm hiding or replacing, all in 1 single file. So it's not a huge hassle.
I hadn't thought of that. That's clever. Technically, you could even have a script edit that file. And you could use moho:CurrentTool() to let the user select tools for a button script to assign to different tool palettes. The only issue there is prompting the user to reload scripts.
User avatar
Lukas
Posts: 1297
Joined: Fri Apr 09, 2010 9:00 am
Location: Netherlands
Contact:

Re: LK_AutoShowHideBones: Auto show/hide shy bones and vitruvian bones on entering/leaving frame 0

Post by Lukas »

Updated the script in the original post, it now no longer hides shy bones when leaving frame 0 in a smartbone-action.

I also use shy bones a lot for things I only animate with smart-bones but don't want the animator to touch in the main timeline. But especially for single frame smartbones, the auto-hiding can be frustrating. So now it doesn't happen in smartbone actions anymore. 🙂
Post Reply