from pyfbsdk import *
import math

'''
This file is to read all branches of both target and source skeleton
This should be using motion-builder

I used People.FBX as a testcase
'''

def get_banch(parents, children, index, branches):
    parents.append(children.Name)

    # if there is no children, append this branch to branches
    if len(children.Children) == 0:
        branches.append(parents)

    # if there is a children, then go to the child
    elif len(children.Children) == 1:
        parents = get_banch(parents, children.Children[0], index+1, branches)

    # if there are several leaves, then search each leaf
    else:
        for i in range(len(children.Children)):
            new = []
            new = get_banch(parents[:index+1], children.Children[i], index+1, branches)
    
    return parents


def get_branches(root):
    branches = []

    if len(root.Children) > 0:
        # you need to check len(root.Children)
        for i in range(len(root.Children)): # this is to stop the loop
            branch = []
            branch.append(root.Name) # skeleton[0] -> root

            # initialize the node and get its children
            parents = branch[:len(branch)]
            children = root.Children[i]
            
            # start the loop to find all leaves
            # the initial index may be wrong, you'd better check it.
            branch = get_banch(parents, children, 1, branches)

            #print()
    #print("\n\n\n\n")

    return branches


# Chose the node that has the highest betweeness
root = FBFindModelByLabelName('Bip01') 
branches = []
branches = get_branches(root)
print(len(branches)) # this tells you how many branches we have
print(branches)      # this shows all branches in a list


@@ -1486,16 +1486,386 @@
 nches%0A%0A%0A
+def get_branches_posi(branches):%0A    branches_posi = %5B%5D%0A    node = FBVector3d()%0A    for b in branches:%0A        bran_posi = %5B%5D%0A        for name in b:%0A            n = FBFindModelByLabelName(name)%0A            n.GetVector(node, FBModelTransformationType.kModelTranslation)%0A            bran_posi.append(node)%0A        branches_posi.append(bran_posi)%0A    return branches_posi%0A%0A
 # Chose 
@@ -1945,30 +1945,16 @@
 ip01') %0A
-branches = %5B%5D%0A
 branches
@@ -1975,16 +1975,62 @@
 s(root)%0A
+branches_posi = get_branches_posi(branches)%0A%0A#
 print(le
@@ -2085,16 +2085,17 @@
 we have%0A
+#
 print(br
@@ -2101,17 +2101,16 @@
 ranches)
- 
      # t
@@ -2142,9 +2142,30 @@
  a list%0A
+#print(branches_posi)
 %0A

save all points as a FBVector