Friday, May 20, 2011

Autorig Leg Module

For my Personal Inquiry unit at the Bournemouth University, I have been researching at the modular automatic rigging system. To implement my study, I created a autorig script in python for the leg with reverse foot system. This is the screen capture of the implementation:




Please click here to download the script file-

Download File
Here is my code-

#Function to create 2 bone chain

def Create2BoneChain(index, name, side, view):
   
    oRoot = Application.ActiveSceneRoot
    x = [0,0,0]
    y = [0,0,0]
    z = [0,0,0]

    for i in range(index, index+3):
        g = "gp"+ str(i)
        oObj = oRoot.FindChild(g)
        x[i-index], y[i-index], z[i-index] = oObj.Kinematics.Global.Transform.GetTranslationValues2()
        print i-index

       

    rtPos = [x[0],y[0],z[0]]
    bn1Pos = [x[1],y[1],z[1]]
    bn2Pos = [x[2],y[2],z[2]]

    oNml = [0,0,1]
    oChain = Application.Create2DSkeleton(rtPos[0], rtPos[1], rtPos[2], bn1Pos[0], bn1Pos[1], bn1Pos[2], oNml[0], oNml[1], oNml[2], view, "", "")
    oBone = oChain.Application.AppendBone("", bn2Pos[0], bn2Pos[1], bn2Pos[2], "")

    Application.SetValue("root*.Name", "my_root", "")
    Application.SetValue("bone.bone*.Name", "my_bone2", "")
    Application.SetValue("my_root.bone*.Name", "my_bone1", "")
    Application.SetValue("my_root.eff*.Name", "my_eff", "")
   
    ort = oRoot.FindChild("my_root")
    obn1 = oRoot.FindChild("my_bone1")
    obn2 = oRoot.FindChild("my_bone2")
    oeff = oRoot.FindChild("my_eff")

    ort.Name = name+"_rt_"+side
    Application.SetValue(ort.Name+".primary_icon", 2, "")
    Application.SetValue(ort.Name+".size", 0.2, "")
    obn1.Name = name+"_bone1_"+side
    obn2.Name = name+"_bone2_"+side
    oeff.Name = name+"_eff_"+side
    #Application.SetValue(oeff.Name+".primary_icon", 2, "")
    Application.SetValue(oeff.Name+".size", 0.2, "")
   
    Application.AlignRootToFirstBone(name+"_rt_"+side)
   
    #if name == "arm":
#        Application.GetPrim("Null", name+"_UpV_"+side, "", "")
#        UpV = name+"_UpV_"+side
#       
#        Application.SetValue(UpV+".kine.local.posx", x[1], "")
#        Application.SetValue(UpV+".kine.local.posy", y[1], "")
#        Application.SetValue(UpV+".kine.local.posz", z[1]-5, "")
#        Application.ApplyOp("SkeletonUpVector", str(obn1)+";"+str(UpV), 3, "siPersistentOperation", "", 0)
   
       

#Function to create 1 bone chain

def Create1BoneChain(index, name, side, view):
   
    oRoot = Application.ActiveSceneRoot

    x = [0,0,0]
    y = [0,0,0]
    z = [0,0,0]

    for i in range(index, index+2):
        g = "gp"+ str(i)
        oObj = oRoot.FindChild(g)
        print oObj
        x[i-index], y[i-index], z[i-index] = oObj.Kinematics.Global.Transform.GetTranslationValues2()
        print i-index

       

    rtPos = [x[0],y[0],z[0]]
    print rtPos
    bn1Pos = [x[1],y[1],z[1]]
    print bn1Pos
    bn2Pos = [x[2],y[2],z[2]]
    print bn2Pos

    oNml = [0,0,1]
    oChain = Application.Create2DSkeleton(rtPos[0], rtPos[1], rtPos[2], bn1Pos[0], bn1Pos[1]+0.001, bn1Pos[2], oNml[0], oNml[1], oNml[2], view, "", "")

    Application.SetValue("root*.Name", "my_root", "")
    Application.SetValue("my_root.bone*.Name", "my_bone1", "")
    Application.SetValue("my_root.eff*.Name", "my_eff", "")
   
   
    if side == "":
        ort = oRoot.FindChild("my_root")
        ort.Name = name+"_rt"
        Application.SetValue(ort.Name+".primary_icon", 2, "")
        Application.SetValue(ort.Name+".size", 0.2, "")
   
        obn1 = oRoot.FindChild("my_bone1")
        obn1.Name = name+"_bone1"
       
        oeff = oRoot.FindChild("my_eff")
        oeff.Name = name+"_eff"
        #Application.SetValue(oeff.Name+".primary_icon", 2, "")
        Application.SetValue(oeff.Name+".size", 0.2, "")
       
    else:
        ort = oRoot.FindChild("my_root")
        ort.Name = name+"_rt_"+side
        Application.SetValue(ort.Name+".primary_icon", 2, "")
        Application.SetValue(ort.Name+".size", 0.2, "")
       
        obn1 = oRoot.FindChild("my_bone1")
        obn1.Name = name+"_bone1_"+side
           
        oeff = oRoot.FindChild("my_eff")
        oeff.Name = name+"_eff_"+side
        #Application.SetValue(oeff.Name+".primary_icon", 2, "")
        Application.SetValue(oeff.Name+".size", 0.2, "")
   
       
    Application.AlignRootToFirstBone(name+"_rt*")


#Create box

def CreateXBox(boxname):
    Application.CreatePrim("Cube", "MeshSurface", boxname, "")
    Application.SetValue(boxname+".cube.length", 1, "")
    Application.Translate("", 0, -0.5, 0, "siAbsolute", 72, "siCtr", "siY", "", "", "", "", "", "", "", "", "", 0, "")
    Application.Rotate("", 0, 0, 90, "siAbsolute", 72, "siCtr", "siZ", "", "", "", "", "", "", "", 0, "")
   
    Application.SelectGeometryComponents(boxname+".pnt[*]")
    Application.Scale("", 0.5, 1, 1, "siAbsolute", "siPivot", "siObj", "siX", "", "", "", "", "", "", "", 0, "")
    Application.Scale("", 1, 1, 0.5, "siAbsolute", "siPivot", "siObj", "siZ", "", "", "", "", "", "", "", 0, "")

    Application.SelectObj(boxname, "", True)
   

#Create 2 bone chain
Create2BoneChain(1, "leg", "R", 2)

#Create 1 bone chain
Create1BoneChain(3, "foot", "R", 2)
Create1BoneChain(4, "toes", "R", 2)


CreateXBox("My_cube")


#Create foot control

Application.SICreateCurve("Foot_R", 3, 0)
Application.SIAddPointOnCurveAtEnd("foot_R", -1, 0, 1, False, 0, "")
Application.SIAddPointOnCurveAtEnd("foot_R", 0, 0, 1, False, 0, "")
Application.SIAddPointOnCurveAtEnd("foot_R", 1, 0, 1, False, 0, "")
Application.SIAddPointOnCurveAtEnd("foot_R", 1, 0, -3, False, 0, "")
Application.SIAddPointOnCurveAtEnd("foot_R", 0, 0, -3, False, 0, "")
Application.SIAddPointOnCurveAtEnd("foot_R", -1, 0, -3, False, 0, "")
Application.ApplyTopoOp("CrvOpenClose", "foot_R", 3, "siPersistentOperation", "")


Application.CreateClusterCenterWithNull("foot_R.pnt[3-LAST]", 0)
Application.SetValue("foot_R.foot_R_Point*_ClsCtr.Name", "front", "")

Application.CreateClusterCenterWithNull("foot_R.pnt[0-2]", 0)
Application.SetValue("foot_R.foot_R_Point*_ClsCtr.Name", "back", "")

fpos = Application.GetValue("gp5.kine.global.pos.posz")
bpos = Application.GetValue("gp6.kine.global.pos.posz")

Application.SetValue("front.kine.global.posz", fpos-1, "")
Application.SetValue("back.kine.global.posz", bpos+0.5, "")
Application.MatchTransform("foot_R", "gp6", "siTrn", "")

Application.FreezeModeling("foot_R", "", "")
Application.DeleteObj("front,back")

Application.Translate("foot_R", 0, 0, bpos, "siRelative", "siLocal", -1, "siXYZ", "", "", "", "", "", "", "", "", "", 0, "")


#Create Foot Control

Application.SICreateCurve("foot_piv_R", 1, 1)
Application.SIAddPointOnCurveAtEnd("foot_piv_R", 0, 0, 0.5, False, 0, "")
Application.SIAddPointOnCurveAtEnd("foot_piv_R", 0, 1, 0.5, False, 0, "")
Application.SIAddPointOnCurveAtEnd("foot_piv_R", 0, 1, -0.5, False, 0, "")
Application.SIAddPointOnCurveAtEnd("foot_piv_R", 0, 0, -0.5, False, 0, "")
Application.ApplyTopoOp("CrvOpenClose", "foot_piv_R", "siUnspecified", "siPersistentOperation", "")

Application.SICreateCurve("toes_piv_R", 1, 1)
Application.SIAddPointOnCurveAtEnd("toes_piv_R", 0, 0, 0.4, False, 0, "")
Application.SIAddPointOnCurveAtEnd("toes_piv_R", 0, 0.75, 0.4, False, 0, "")
Application.SIAddPointOnCurveAtEnd("toes_piv_R", 0, 0.75, -0.4, False, 0, "")
Application.SIAddPointOnCurveAtEnd("toes_piv_R", 0, 0, -0.4, False, 0, "")
Application.ApplyTopoOp("CrvOpenClose", "toes_piv_R", 3, "siPersistentOperation", "")

Application.MatchTransform("foot_piv_R", "gp4", "siTrn", "")
Application.MatchTransform("toes_piv_R", "gp5", "siTrn", "")

Application.SelectObj("toes_rt_R", "BRANCH", True)
Application.Duplicate("B:toes_rt_R", "", 2, 1, 1, 0, 0, 1, 0, 1, "", "", "", "", "", "", "", "", "", "", 0)
Application.SetValue("toes_rt_R1.Name", "toes_rt_P_R", "")
Application.SetValue("toes_bone1_R1.Name", "toes_bone1_P_R", "")
Application.SetValue("toes_eff_R1.Name", "toes_eff_P_R", "")

Application.SetValue("toes_rt_P_R.root.primary_icon", 0, "")
Application.SetValue("toes_rt_P_R.root.shadow_icon", 2, "")
Application.SetValue("toes_rt_P_R.root.shadow_colour_custom", True, "")
Application.SetValue("toes_rt_P_R.root.R", 1, "")
Application.SetValue("toes_rt_P_R.root.shadow_offsetY", 6, "")
Application.MatchTransform("Foot_R,foot_piv_R,toes_piv_R", "gp4", "siRot", "")

Application.CutObj("gp2, gp3, gp4, gp5, gp6")
Application.DeleteObj("gp2")

Application.SetValue("gp1.Name", "leg_wrapper_R", "")
Application.SetValue("gp3.Name", "ankle_piv_R", "")
Application.SetValue("gp4.Name", "foot_piv_P_R", "")
Application.SetValue("gp5.Name", "toes_piv_P_R", "")
Application.SetValue("gp6.Name", "heel_piv_R", "")

Application.SetValue("heel_piv_R.gp8.Name", "foot_bank_left_R", "")
Application.SetValue("heel_piv_R.gp7.Name", "foot_bank_right_R", "")
Application.SetValue("foot_bank_right_R.kine.local.affbyscl,foot_bank_left_R.kine.local.affbyscl", [False, False], "")
Application.SetValue("foot_bank_right_R.kine.local.cnsscl,foot_bank_left_R.kine.local.cnsscl", [False, False], "")

Application.ParentObj("foot_piv_R", "ankle_piv_R")
Application.ParentObj("foot_piv_P_R", "foot_piv_R")
Application.ParentObj("toes_piv_R", "foot_piv_P_R")
Application.ParentObj("toes_piv_P_R", "toes_piv_R")

Application.ParentObj("foot_bank_right_R", "toes_piv_P_R")
Application.ParentObj("foot_bank_left_R", "foot_bank_right_R")

Application.ParentObj("Foot_R", "heel_piv_R")
Application.ParentObj("toes_rt_P_R", "toes_rt_R")

Application.ParentObj("foot_bank_right_R", "foot_rt_R")
Application.ParentObj("foot_bank_right_R", "toes_rt_P_R")


#Add Constraints

Application.ApplyCns("Position", "toes_rt_P_R", "foot_eff_R", "")
Application.ApplyCns("Position", "foot_eff_R", "foot_piv_R", "")
Application.ApplyCns("Position", "toes_eff_P_R", "toes_piv_R", "")
Application.ApplyCns("Position", "foot_rt_R", "leg_eff_R", "")
Application.ApplyCns("Position", "leg_eff_R", "ankle_piv_R", "")
Application.ApplyCns("Position", "leg_rt_R", "leg_wrapper_R", "")
Application.CopyPaste("toes_rt_P_R", "", "B:foot_piv_P_R", 1)


#Add Custom Parameters



No comments:

Post a Comment