Genete! Bone "points" at another bone

Moho allows users to write new tools and plugins. Discuss scripting ideas and problems here.

Moderators: Víctor Paredes, Belgarath, slowtiger

User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Genete! Bone "points" at another bone

Post by heyvern »

Genete,

I think I remember you talking about working on something like this in another post. I was unable to track it down.

Anyway, I just figured it out. I have a script solution that makes a bone change scale and rotation to "follow" or "point" to another bones position. It works great. I will post it as soon as I "clean it up" a bit and make it more useful. Currently I have my test bones "hard coded".

I actually reverse engineered the code from the LM_translate_bone tool. I used the code from the part that works on frame 0 with bone length and converted it to scale for frames above 0.

-vern
Genete
Posts: 3483
Joined: Tue Oct 17, 2006 3:27 pm
Location: España / Spain

Post by Genete »

Yeah! It was when I started to make the complex springy gizmo with variable length (I think...).
It would be so cool. With that you could create a full crankshaft mechanism or what ever other mechanism you can imagine...

There is an example of an embedded script bone that always points downwards made by LM in the first post of embedded script. May be it help you to polish the script.
Waiting for your script release.
Thanks!
-G
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

Well here it is. I put the link and instructions in my script thread:

viewtopic.php?t=7842

It has trouble when you use the undo. Just go back to frame 0 to "reset".

It's really cool. You can move the pointer bone or even a parent bone and it stretches to stay attached to the target. Same for the target. It works with any number of parent bones in a chain.

This is the funnest script I've done and it's fairly simple code. I think it could be very useful for a lot of things. I plan to use it with your sort shape script Genete. I can use this to cut down on the number of teeth. Instead of individual teeth shapes each with a bone, I am going to "connect" them into one shape and use an aimer bone to follow the turning of the head to scale and rotate.

Cuts the number of shapes/bones down to 5 or 6 instead of 30! ;)

-vern
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

Holy cow!!!

This script is going to be great for doing "3D" head turn stuff! It will be great for doing arms and legs!!!

Drag a bone and the arm will squash and stretch!!!

I am so excited!

-vern
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

Now my brain is spinning!

I think... uh... I am almost sure I can modify this script to create a better "bone lock" for walk cycles!

Actually it can be used as is really. Think about it. You can make the calf bone of a leg an "aimer" bone then it only rotates when you translate another bone. The calf will stay "locked" where ever you put it!!! Even if you move the hips or anything else!!!

Yeeeeehaaaaa!!!!

I want to see if a modification to limit scaling would be possible.

-vern
Genete
Posts: 3483
Joined: Tue Oct 17, 2006 3:27 pm
Location: España / Spain

Post by Genete »

Please check out the aim_bone.zip file. When I unzip it, it is empty. :(
-G
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

Genete wrote:Please check out the aim_bone.zip file. When I unzip it, it is empty. :(
-G
Very strange. The file seems okay. I downloaded it a couple of times into different places and it works. Don't know what else to do.

I re-uploaded it any way just to be sure.

It could have been a "glitch" when you downloaded it.

-vern
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

This script has a ton of potential.

I'm playing around with all kinds of weird combinations of bones to create different types of "IK" for arms and legs.

For instance I have an arm bone where the forearm is "backwards". It goes from the hand to the elbow. The target for both the bicep and forearm are at the elbow. You can drag the forearm to rotate/scale it, or you can drag the elbow bone to rotate/scale both the bicep AND the forearm. This is like having the hand at the top of the IK chain. You can lock the hand in position but then also move it as needed. Very very coooollll!!!! :)

There are some glitches I need to work out in the code to make this work well. I'll keep on working on it.

This thing is amazing. It will make creating 3D body turns a piece of cake.

-vern
Genete
Posts: 3483
Joined: Tue Oct 17, 2006 3:27 pm
Location: España / Spain

Post by Genete »

Can you paste the lua file into this thread?
I still having same problem :cry:
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

The weirdest thing ever. For some reason this stupid zip file won't extract on windows. I've done it a dozen times and just the folder won't zip correctly.

Try these links:

http://www.lowrestv.com/moho_stuff/scri ... _bone.anme
http://www.lowrestv.com/moho_stuff/scripts/aim_bone.lua

This one works now. I didn't zip it inside a folder. I don't know what the problem is with that folder.
http://www.lowrestv.com/moho_stuff/scripts/aim_bone.zip

And here is the code if they don't work:

Code: Select all

function LayerScript(moho)

    local layer = moho.layer
    if ( not layer:IsBoneType() ) then
        return
    end
    local skel = moho:Skeleton()
    local boneCount = skel:CountBones()
    if ( boneCount == 0) then
        return
    end
    
    if (moho.frame == 0) then
        aimGrps = {} -- list of bone pairs for "pointing"

        for b = 0, boneCount - 1 do
            local bone = skel:Bone(b)
            local boneName = skel:Bone(b):Name()
            local Baimer = string.find(boneName, '.aim') -- bone that points to the target
            local Btarget = string.find(boneName, '.trg') -- target of the pointer bone
            
            if ( Baimer or Btarget ) then
                local name = string.sub(boneName, 1, -5)
                if (aimGrps[name] == nil) then
                    aimGrps[name] = {}
                end
                if (Baimer) then
                    aimGrps[name].Bp = bone
                end
                if (Btarget) then
                    aimGrps[name].Bt = bone
                end
            end
        end    
     end     

    if (moho.frame > 0) then
        if (aimGrps ~= nil) then
            for b in aimGrps do
                local aimer = aimGrps[b].Bp
                local target = aimGrps[b].Bt
                local inverseAIM = LM.Matrix:new_local()
    
                local offset = LM.Vector2:new_local()
                offset.x = target.fPos.x
                offset.y = target.fPos.y
                
                local TRGBoneP = skel:Bone(target.fParent)
                if ( TRGBoneP ~= nil) then
                    TRGBoneP.fMovedMatrix:Transform(offset)
                end

                local boneVec = LM.Vector2:new_local()
                local Bonelength = aimer.fLength
                boneVec = boneVec + offset
 
                inverseAIM:Set(aimer.fMovedMatrix)
                inverseAIM:Invert()
                inverseAIM:Transform(boneVec)
                inverseAIM:Transform(offset)

                local dL = boneVec:Mag()
                local bscale = offset:Mag() / Bonelength
                local angle = aimer.fAngle
                angle = angle + math.atan2(boneVec.y, boneVec.x)
                while angle > 2 * math.pi do
                        angle = angle - 2 * math.pi
                end
                while angle < 0 do
                        angle = angle + 2 * math.pi
                end
                aimer.fScale = bscale
                aimer.fAngle = angle
                
            end
            moho:Skeleton():UpdateBoneMatrix()
        end
    end
end
User avatar
Víctor Paredes
Site Admin
Posts: 5664
Joined: Wed Jan 26, 2005 12:18 am
Location: Barcelona/Chile
Contact:

Post by Víctor Paredes »

vern, i''m looking your script and i see it have a lot of potencial. but, however, my brain and my english knowledge are limited.

can you post more anme examples explaing the different uses?
that would be great.

thanks again. even if i don't understand much, my limited brain tell me it's a great script.
Genete
Posts: 3483
Joined: Tue Oct 17, 2006 3:27 pm
Location: España / Spain

Post by Genete »

ANOTHER PRETTY SIMPLE BUT STRONG POWERFUL SCRIPT!!!!
It is a great script Vern! Congratulations!
Regarding to the aim_bone.zip, now it works. It was weird.

Selgin, I have problems understanding Vern but believe him, It would be amazing!!!!
Imagine this:
1) Create a 3D skeleton (only the extreme edges of the skeleton and the joint, not the complete surface of a 3D object) using the 3D stuff of the other threads
2) For each pt bone link at its origin a .trg bone
3) Add a .aim bone from the parent pt bone to the current pt bone. You have a "3D" bone!. Draw the limb around this .aim bone and it would squash and stretch according to the 3D limb position.

What I don't understand yet is the stuff about walk cycles...

Vern! put examples please!
-G
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

Working on it!

here is an arm I whipped up:

http://www.lowrestv.com/moho_stuff/scripts/aim_arm.mov

http://www.lowrestv.com/moho_stuff/scripts/aim_arm.zip

Look at the bone parenting. The forearm bone is "flipped" starting at the hand and ending at the elbow. The forearm is NOT a child of the bicep. Both arm bones are aiming at two bones at the elbow and parented to a "root" bone or shoulder.

So you can rotate the forearm by dragging it. This moves only the forearm. If you drag the elbow bone indicated it rotates both the bicep and the forearm.

Rotating/translating the root or shoulder bone moves the whole arm.

This could lead to some very interesting bone setups. You don't need to parent a bone to have it "linked".

I have to say this is a LOT of FUN to animate the arm!!!! :) I use to do the same thing but had to scale/rotate all those bones individually.

-------------

There are some funny strange problems I have to figure out before this will work the way I want to use it. It has to do I am pretty sure with how the script is applying the parent bone matrices. For some reason constraints don't behave properly using aimer bones as the target.

When the parenting or hierarchy of bones is "unusual" or complex the mesh isn't updated with the bone movement.

I will keep working on it. I'm so close.

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

Post by Víctor Paredes »

oh, i see, with this you get a much flexible skeleton. make some complex "3d" movements is now easier and possible. when you drag the elbow, you are "saying" that there is a deep space, and not just a cropped draw moving.

it is a great script, for sure.
Genete
Posts: 3483
Joined: Tue Oct 17, 2006 3:27 pm
Location: España / Spain

Post by Genete »

Aaaarggh! Again the zip file is empty! :(
It is only me or the file itself...?
-G
Post Reply