How do I get the original angle relative to a bone's parent

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

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
ruibjr
Posts: 113
Joined: Mon Apr 20, 2009 12:29 am
Location: Brazil

How do I get the original angle relative to a bone's parent

Post by ruibjr »

How do I do, in Lua, to get the bone's original angle, relative to its parent bone?

For instance, if I have boneA and boneB, being boneB a child of boneA, and being boneB originaly rotated 30 degrees (that is, rotate 30 degrees relative to boneA, in frame 0).

I am looking for a way to request that angle (30 degrees, or its equivalent in radians), from the Lua API.

The script I am working on (based on HeyVern's original script and later modified by Genete) is working in almost every aspect I need, except that it only works 100% when the original angle is 0 degrees (0 radians).

Cheers.
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

Download "The Flipper" script and poke through the code:

viewtopic.php?t=7842

You have to scroll down pretty far on the first post to find it (I need to fix that page).

It doesn't specifically answer your question, however the code is in there shows you how to do this. I would just explain it in this post but... I'm bad at math and can't remember exactly.

-vern
ruibjr
Posts: 113
Joined: Mon Apr 20, 2009 12:29 am
Location: Brazil

Post by ruibjr »

Hello Vern.
Thank you so much for your script.

I made the chanages that I needed to it, and here is the version I am going to be using:

Code: Select all

function LayerScript(moho) 
    ------------------------------------------------------------------------------------------ 
    local layer = moho.layer 
    if ( not layer:IsBoneType() ) then 
        return 
    end 
    ------------------------------------------------------------------------------------------ 
    local group = moho:LayerAsGroup(layer) 
    local layerCount = group:CountLayers() 
    if ( layerCount == 0 ) then 
        return 
    end 
    ------------------------------------------------------------------------------------------ 
    local skel = moho:Skeleton() 
    local boneCount = skel:CountBones() 
    if ( boneCount == 0) then 
        return 
    end 
    ------------------------------------------------------------------------------------------ 
    local ctrlLayer = nil 
    local ctrlBone = nil 
    local keySwitch = nil 
    local Lcount = nil 
    local SWunits = nil 
    local keySwitch = nil 
    local nextBone = nil 
    local theBoneName = nil 
    local tUnit = nil 
    ------------------------------------------------------------------------------------------ 
    for nextBone = 0, boneCount - 1 do 
        ctrlBone = skel:Bone(nextBone) 
        theBoneName = ctrlBone:Name() 
        if (string.sub(theBoneName, 1, 3) == "cb.") then 
            tUnit = ctrlBone.fMaxConstraint - ctrlBone.fMinConstraint 
            if (moho.frame > 0) then 
                local subSwitch = nil 
                if (ctrlBone.fAnimAngle:HasKey(moho.frame)) then 
                    keySwitch = ctrlBone.fAngle 
                    keySwitch = keySwitch - ctrlBone.fAnimAngle:GetValue(0) - ctrlBone.fMinConstraint 
                end 
                Lcount = -1 
                for i = 0, layerCount - 1 do 
                    local testlayer = group:Layer(i) 
                    if (testlayer:LayerType() == MOHO.LT_SWITCH) then 
                   if (testlayer:Name() == theBoneName) then 
                            group2 = moho:LayerAsGroup(testlayer) 
                            subSwitch = moho:LayerAsSwitch(testlayer) 
                            Lcount = group2:CountLayers() 
                            SWunits = tUnit / (Lcount) 
                            break 
                        end 
                    end 
                end 
                if (Lcount > 0) then 
                    for b = 0, Lcount - 1 do 
                        local swLayers = group2:Layer(b) 
                        local SWname = swLayers:Name() 
                        local swUnit = (b * SWunits) 
                        local swUnitB = swUnit + SWunits 
                        if ( keySwitch ~= nil) then 
                            if (keySwitch <= swUnitB and keySwitch >= swUnit) then 
                                subSwitch:SwitchValues():SetValue(moho.frame, SWname) 
                            end 
                        elseif ( keySwitch == nil) then 
                            subSwitch:SwitchValues():DeleteKey(moho.frame) 
                        end 
                    end 
                end 
            end 
        end 
    end 
    ------------------------------------------------------------------------------------------ 
end 
The limbs control their own switches, based on the angle relative to their parent bones.
It also takes into consideration the angle constraints.
Thank you for helping me out.

BTW: You have written zillions of scripts.
Are you a programmer and animator?

Cheers.
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

Ha!

I'm not even a programmer. I know enough to be dangerous. I use to do a lot of JS when I did web design full time. Before that I did some Actionscripting. Then when I bought Moho... I had to learn some lua. The transition was simple enough.

Animator? Well I like to think so although that isn't my full time day job... yet. ;)

You seem to have gotten adept at lua... or were you before? Do you have programming experience? We NEED SCRIPTERS!!! The more we have the better. Scripting features don't get as much attention because so few of us use that feature.

-vern
ruibjr
Posts: 113
Joined: Mon Apr 20, 2009 12:29 am
Location: Brazil

Post by ruibjr »

Hello Vern.

I work as a computer programmer, down here, in Brazil.
Well, I am a system analyst, actualy, but I do programming day in and day out.
Currently, mainly ActionScript, JavaScript and PHP.

I like Lua, but I have not been able to find extensive documentation on the ASP Lua API.

Anyways, I am loving ASP, so far.

Now, here is a question for you: :)

I have made some experiments with this script, and the results turned out fantastic.
I was wondering what kind of results one can get by modifying the script so that it can control more than one switch with a songle limb.
It would work like this...

1. The controlling limb would compute its angle relative to its parent bone.
2. Based on that angle, it would select the best drawing.
3. It would also modify the switch for the parent limb.

1 and 2 work beautifuly.
I am going to try 3, but am just wondering... would this really help.
I believe it so, as long as the arm or leg or tail or whatever is designed with that in mind.

I feel that this would result in perfect matching and even a pretty good simulation of musculature.

I am going to try it and will let you know what kind of results I get.

Cheers.
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

Every once in a while some new idea comes along that is really cool. The idea of using bones to control switch layers to fix joints is one of those cool ideas. I'm surprised none of us have thought of that before. I look forward to seeing the results.

Have you downloaded the Moho Scripting Reference from here?

viewtopic.php?t=1031

This is all I have for reference... well this and all the other scripts that others wrote. This reference is old but scripting in AS has not changed much over the years.

-vern
chucky
Posts: 4650
Joined: Sun Jan 28, 2007 4:24 am

Post by chucky »

Another scripter? Yay.
Good on you ruibjr.
Image
The more the merrier, Hey, Vern have you made any advances with any of your most ambitious script ideas?
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

chucky wrote:Hey, Vern have you made any advances with any of your most ambitious script ideas?
Yes, actually, one of my favorites. You guys will be the first to get it when it's ready. ;)

-vern
Post Reply