Page 1 of 1

Bone A randomly driven by bone B

Posted: Thu May 11, 2023 2:07 pm
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

Re: Bone A randomly driven by bone B

Posted: Thu May 11, 2023 3:13 pm
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

Re: Bone A randomly driven by bone B

Posted: Thu May 11, 2023 3:19 pm
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:

Re: Bone A randomly driven by bone B

Posted: Thu May 11, 2023 6:21 pm
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.

Re: Bone A randomly driven by bone B

Posted: Thu May 11, 2023 6:39 pm
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!

Re: Bone A randomly driven by bone B

Posted: Fri May 12, 2023 3:38 pm
by Víctor Paredes
Nice! I'm happy this idea was helpful for you :)