Bone A randomly driven by bone B

Wondering how to accomplish a certain animation task? Ask here.

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
User avatar
keepfungus
Posts: 50
Joined: Fri Jun 10, 2011 10:15 am
Location: Rome, Italy
Contact:

Bone A randomly driven by bone B

Post by keepfungus »

Hi "mohonians",
I hope you can help me with this.
Let's say we have two bones: A and B.
I need bone A to rotate randomly (and automatically put a keyframe on rotations) every time that a keyframe is put on whatever channel of bone B (it could be rotations, positions, switch channel etc).
I hope it's clear enough.
Thanks in advance for your mind squeezing.

Matteo
vimeo.com/matteoloi
instagram.com/keepfungus/
justeleven.it
twitter.com/justeleven
User avatar
hayasidist
Posts: 3525
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: Bone A randomly driven by bone B

Post by hayasidist »

layerscript.
for example: (this uses fixed bone names; only looks for angle and position and doesn't really try to randomise Bone "A")

It will benefit from enhancement e.g. to have bones selectable and to add all the channels and proper randomisation but I hope you get the idea ...

Code: Select all

function LayerScript(moho)
--	v0.0
	if moho.frame == 0 then
		return
	end

	local skel = moho:Skeleton()
	if not skel then
		return false
	end
	local checkBoneName = "B1"
	local checkBone = skel:BoneByName(checkBoneName)
	if not checkBone then
		return false
	end

	local controlledBoneName = "B2"
	local controlledBone = skel:BoneByName(controlledBoneName)
	if not controlledBone then
		return false
	end

	local channels = {checkBone.fAnimAngle, checkBone.fAnimPos}

	for i = 1, #channels do
		if channels[i]:HasKey(moho.frame) then
			if not controlledBone.fAnimAngle:HasKey(moho.frame) then
				controlledBone.fAnimAngle:SetValue(moho.frame, math.rad(moho.frame/10))
			end
			break
		end
	end


end
User avatar
Víctor Paredes
Site Admin
Posts: 5665
Joined: Wed Jan 26, 2005 12:18 am
Location: Barcelona/Chile
Contact:

Re: Bone A randomly driven by bone B

Post by Víctor Paredes »

I have a setup that could create something similar to what you are looking for (if I understood the idea).
Please check this file.

I have these 4 Bones:
Image

Here's how it's setup:
Bone B's angle is controlled by Bone C by a very high number (3477, but it can be any big value, really).
This makes that every time you rotate Bone C, Bone B will also rotate, but a lot more. So Bone B will end at a random angle.

Then, I created Bone A and, child of it, the bone named Target.
So if you scale, rotate or translate Bone A, then Target will also move with it.

Finally, I set Target as the target for Bone C.
So Bone C will always rotate to point to the Target bone.

At the end, you rotate/scale/translate Bone A. That changes the position of the bone Target. That changes the angle of Bone C and that change of angle is multiplied on Bone B.

I hope it helps (and makes sense!)

PS: I just saw the script there. Yes, probably that's better than what I wrote here :lol:
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
keepfungus
Posts: 50
Joined: Fri Jun 10, 2011 10:15 am
Location: Rome, Italy
Contact:

Re: Bone A randomly driven by bone B

Post by keepfungus »

hayasidist wrote: Thu May 11, 2023 3:13 pm layerscript.
for example: (this uses fixed bone names; only looks for angle and position and doesn't really try to randomise Bone "A")

It will benefit from enhancement e.g. to have bones selectable and to add all the channels and proper randomisation but I hope you get the idea ...

Code: Select all

function LayerScript(moho)
--	v0.0
	if moho.frame == 0 then
		return
	end

	local skel = moho:Skeleton()
	if not skel then
		return false
	end
	local checkBoneName = "B1"
	local checkBone = skel:BoneByName(checkBoneName)
	if not checkBone then
		return false
	end

	local controlledBoneName = "B2"
	local controlledBone = skel:BoneByName(controlledBoneName)
	if not controlledBone then
		return false
	end

	local channels = {checkBone.fAnimAngle, checkBone.fAnimPos}

	for i = 1, #channels do
		if channels[i]:HasKey(moho.frame) then
			if not controlledBone.fAnimAngle:HasKey(moho.frame) then
				controlledBone.fAnimAngle:SetValue(moho.frame, math.rad(moho.frame/10))
			end
			break
		end
	end


end
Thanks a lot! I'll definitely try it.
vimeo.com/matteoloi
instagram.com/keepfungus/
justeleven.it
twitter.com/justeleven
User avatar
keepfungus
Posts: 50
Joined: Fri Jun 10, 2011 10:15 am
Location: Rome, Italy
Contact:

Re: Bone A randomly driven by bone B

Post by keepfungus »

Victor, I can only say...GENIAL!
It works just fine.
Since the "final" bone needed to rotate between two limits (0°-180°) I added a final bone (the green one in the video) with limits which follows a target bone parented to the "random" bone.



Your lateral thinking is always inspiring.
Thanks so much!
vimeo.com/matteoloi
instagram.com/keepfungus/
justeleven.it
twitter.com/justeleven
User avatar
Víctor Paredes
Site Admin
Posts: 5665
Joined: Wed Jan 26, 2005 12:18 am
Location: Barcelona/Chile
Contact:

Re: Bone A randomly driven by bone B

Post by Víctor Paredes »

Nice! I'm happy this idea was helpful for you :)
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
Post Reply