def getvals(bonename, dataref, index):

    props=armature.getAllProperties()

    fullref=""
    if dataref in lookup and lookup[dataref]:
        (path, n)=lookup[dataref]
        fullref=path
    else:
        for prop in props:
            if prop.name.strip()==dataref and prop.type=='STRING' and prop.data:
                if prop.data.endswith('/'):
                    fullref=prop.data+dataref
                else:
                    fullref=prop.data+'/'+dataref
                break
        else:
            fullref=dataref

    # find last frame
    framecount=2    # zero based
    action=armature.getAction()
    if action and bonename in action.getChannelNames():
        ipo=action.getChannelIpo(bonename)
        for icu in ipo:
            for bez in icu.bezierPoints:
                f=bez.pt[0]
                if f>100:
                    pass    # silently stop
                elif f>int(f):
                    framecount=max(framecount,int(f)+1) # like math.ceil()
                else:
                    framecount=max(framecount,int(f))
    vals=[0.0]+[1.0 for i in range(framecount-1)]
    loop=0.0

    sname=make_short_name(fullref)
    seq=[dataref]
    seq.append(sname)
    if index: seq.append('%s[%d]' % (dataref, index))
    if index: seq.append('%s[%d]' % (sname, index))

    for tmpref in seq:
        for val in range(framecount):
            valstr="%s_v%d" % (tmpref, val+1)
            for prop in props:
                if prop.name.strip()==valstr:
                    if prop.type=='INT':
                        vals[val]=float(prop.data)
                    elif prop.type=='FLOAT':
                        vals[val]=round(prop.data, Vertex.ROUND)
        valstr="%s_loop" % tmpref
        for prop in props:
            if prop.name.strip()==valstr:
                if prop.type=='INT':
                    loop=float(prop.data)
                elif prop.type=='FLOAT':
                    loop=round(prop.data, Vertex.ROUND)

    return (fullref,vals,loop)
def doapply(evt, val):
    global bonecount

    editmode = Window.EditMode()
    if editmode: Window.EditMode(0)
    armobj = armature.getData()
    armobj.makeEditable()
    armbones = armobj.bones

    # rescan object's parents - hope that the user hasn't reparented
    bone = armbones[theobject.getParentBoneName()]
    editbones = [bone]
    while bone.parent:
        editbones.append(bone.parent)
        bone = bone.parent
    bonecount = min(bonecount, len(editbones))  # in case user has reparented

    # Rename bones - see armature_bone_rename in editarmature.c
    oldnames = [bone.name for bone in editbones]
    othernames = armbones.keys()
    for name in oldnames:
        othernames.remove(name)
    newnames = []

    action = armature.getAction()
    if action:
        for boneno in range(bonecount):
            # rename this Action's channels to prevent error on dupes
            if oldnames[boneno] in action.getChannelNames():
                action.renameChannel(oldnames[boneno], 'TmpChannel%d' % boneno)

    for boneno in range(bonecount - 1, -1, -1):
        # do in reverse order in case of duplicate names
        name = datarefs[boneno].split('/')[-1]
        # bone name getting up toward trouble?  use PT name.  We'd rather be ambiguous - and readable.
        if len(name) > 26:
            name = make_short_name(datarefs[boneno])
        if indices[boneno] != None: name = '%s[%d]' % (name, indices[boneno])
        # Have to manually avoid duplicate names
        i = 0
        base = name
        while True:
            if name in othernames:
                i += 1
                name = '%s.%03d' % (base, i)
            else:
                break

        editbones[boneno].name = name
        othernames.append(name)
        newnames.insert(0, name)

        # Update this Action's channels
        if action:
            oldchannel = 'TmpChannel%d' % boneno
            if oldchannel in action.getChannelNames():
                # Delete keys
                ipo = action.getChannelIpo(oldchannel)
                for icu in ipo:
                    i = 0
                    while i < len(icu.bezierPoints):
                        if icu.bezierPoints[i].pt[0] > len(vals[boneno]):
                            icu.delBezier(i)
                        else:
                            i += 1
                # Rename
                action.renameChannel(oldchannel, name)
        # Update any other Actions' channels?

    armobj.update()  # apply new bone names

    # Reparent children - have to do this after new bone names are applied
    for obj in Scene.GetCurrent().objects:
        if obj.parent == armature and obj.parentbonename in oldnames:
            obj.parentbonename = newnames[oldnames.index(obj.parentbonename)]

    # Now do properties
    props = {}

    # First do dataref paths for datarefs and hide/show
    for dataref in datarefs + hideshow:
        ref = dataref.split('/')
        if len(ref) > 1 and (ref[-1] not in lookup or not lookup[ref[-1]]):
            # not a standard dataref
            props[ref[-1]] = '/'.join(ref[:-1])

    # datarefs values
    for boneno in range(len(datarefs)):
        ref = datarefs[boneno].split('/')
        name = make_short_name(datarefs[boneno])
        #name=ref[-1]
        if indices[boneno] != None: name = '%s[%d]' % (name, indices[boneno])
        if len(ref) > 1 or name in lookup:
            # write vals for ambiguous and unusable datarefs, but not invalid
            for frameno in range(len(vals[boneno])):
                if not ((frameno == 0 and vals[boneno][frameno] == 0) or
                        (frameno == (len(vals[boneno]) - 1)
                         and vals[boneno][frameno] == 1)):
                    props['%s_v%d' %
                          (name, frameno + 1)] = vals[boneno][frameno]
                if loops[boneno]:
                    props[name + '_loop'] = loops[boneno]

    # Apply
    armature.removeAllProperties()
    keys = props.keys()
    keys.sort()
    for key in keys:
        armature.addProperty(key, props[key])

    # Create properties for the manipulators
    manipulator = manipulators[0]
    sub_dict = sorted(manipulator_dict[manipulator].keys())
    armature.addProperty('manipulator_type', manipulator)

    for field_id in sub_dict:
        field_name = field_id.split('@')[1]
        field_val = manipulator_dict[manipulator][field_id]
        property_name = manipulator + '_' + field_name
        if field_name == 'cursor':
            armature.addProperty(property_name, cursors[0])
        else:
            armature.addProperty(property_name, field_val)

    # Hide/Show - order is significant
    h = 1
    s = 1
    for hs in range(len(hideshow)):
        name = hideshow[hs].split('/')[-1]
        if hideshowindices[hs] != None:
            name = '%s[%d]' % (name, hideshowindices[hs])
        if hideorshow[hs]:
            armature.addProperty('%s_show_v%d' % (name, s), hideshowfrom[hs])
            armature.addProperty('%s_show_v%d' % (name, s + 1), hideshowto[hs])
            s += 2
        else:
            armature.addProperty('%s_hide_v%d' % (name, h), hideshowfrom[hs])
            armature.addProperty('%s_hide_v%d' % (name, h + 1), hideshowto[hs])
            h += 2

    Draw.Exit()
    if editmode: Window.EditMode(1)
    Window.RedrawAll()  # in case bone names have changed
    return
def getvals(bonename, dataref, index):

    props = armature.getAllProperties()

    fullref = ""
    if dataref in lookup and lookup[dataref]:
        (path, n) = lookup[dataref]
        fullref = path
    else:
        for prop in props:
            if prop.name.strip(
            ) == dataref and prop.type == 'STRING' and prop.data:
                if prop.data.endswith('/'):
                    fullref = prop.data + dataref
                else:
                    fullref = prop.data + '/' + dataref
                break
        else:
            fullref = dataref

    # find last frame
    framecount = 2  # zero based
    action = armature.getAction()
    if action and bonename in action.getChannelNames():
        ipo = action.getChannelIpo(bonename)
        for icu in ipo:
            for bez in icu.bezierPoints:
                f = bez.pt[0]
                if f > 100:
                    pass  # silently stop
                elif f > int(f):
                    framecount = max(framecount,
                                     int(f) + 1)  # like math.ceil()
                else:
                    framecount = max(framecount, int(f))
    vals = [0.0] + [1.0 for i in range(framecount - 1)]
    loop = 0.0

    sname = make_short_name(fullref)
    seq = [dataref]
    seq.append(sname)
    if index: seq.append('%s[%d]' % (dataref, index))
    if index: seq.append('%s[%d]' % (sname, index))

    for tmpref in seq:
        for val in range(framecount):
            valstr = "%s_v%d" % (tmpref, val + 1)
            for prop in props:
                if prop.name.strip() == valstr:
                    if prop.type == 'INT':
                        vals[val] = float(prop.data)
                    elif prop.type == 'FLOAT':
                        vals[val] = round(prop.data, Vertex.ROUND)
        valstr = "%s_loop" % tmpref
        for prop in props:
            if prop.name.strip() == valstr:
                if prop.type == 'INT':
                    loop = float(prop.data)
                elif prop.type == 'FLOAT':
                    loop = round(prop.data, Vertex.ROUND)

    return (fullref, vals, loop)
def doapply(evt,val):
    global bonecount

    editmode=Window.EditMode()
    if editmode: Window.EditMode(0)
    armobj=armature.getData()
    armobj.makeEditable()
    armbones=armobj.bones

    # rescan object's parents - hope that the user hasn't reparented
    bone=armbones[theobject.getParentBoneName()]
    editbones=[bone]
    while bone.parent:
        editbones.append(bone.parent)
        bone=bone.parent
    bonecount=min(bonecount, len(editbones))	# in case user has reparented

    # Rename bones - see armature_bone_rename in editarmature.c
    oldnames=[bone.name for bone in editbones]
    othernames=armbones.keys()
    for name in oldnames: othernames.remove(name)
    newnames=[]

    action=armature.getAction()
    if action:
        for boneno in range(bonecount):
            # rename this Action's channels to prevent error on dupes
            if oldnames[boneno] in action.getChannelNames():
                action.renameChannel(oldnames[boneno], 'TmpChannel%d' % boneno)

    for boneno in range(bonecount-1,-1,-1):
        # do in reverse order in case of duplicate names
        name=datarefs[boneno].split('/')[-1]
        # bone name getting up toward trouble?  use PT name.  We'd rather be ambiguous - and readable.
        if len(name) > 26:
            name=make_short_name(datarefs[boneno])
        if indices[boneno]!=None: name='%s[%d]' % (name, indices[boneno])
        # Have to manually avoid duplicate names
        i=0
        base=name
        while True:
            if name in othernames:
                i+=1
                name='%s.%03d' % (base, i)
            else:
                break

        editbones[boneno].name=name
        othernames.append(name)
        newnames.insert(0, name)

        # Update this Action's channels
        if action:
            oldchannel='TmpChannel%d' % boneno
            if oldchannel in action.getChannelNames():
                # Delete keys
                ipo=action.getChannelIpo(oldchannel)
                for icu in ipo:
                    i=0
                    while i<len(icu.bezierPoints):
                        if icu.bezierPoints[i].pt[0]>len(vals[boneno]):
                            icu.delBezier(i)
                        else:
                            i+=1
                # Rename
                action.renameChannel(oldchannel, name)
        # Update any other Actions' channels?

    armobj.update()    # apply new bone names

    # Reparent children - have to do this after new bone names are applied
    for obj in Scene.GetCurrent().objects:
        if obj.parent==armature and obj.parentbonename in oldnames:
            obj.parentbonename=newnames[oldnames.index(obj.parentbonename)]

    # Now do properties
    props={}

    # First do dataref paths for datarefs and hide/show
    for dataref in datarefs+hideshow:
        ref=dataref.split('/')
        if len(ref)>1 and (ref[-1] not in lookup or not lookup[ref[-1]]):
            # not a standard dataref
            props[ref[-1]]='/'.join(ref[:-1])

    # datarefs values
    for boneno in range(len(datarefs)):
        ref=datarefs[boneno].split('/')
        name=make_short_name(datarefs[boneno])
        #name=ref[-1]
        if indices[boneno]!=None: name='%s[%d]' % (name, indices[boneno])
        if len(ref)>1 or name in lookup:
            # write vals for ambiguous and unusable datarefs, but not invalid
            for frameno in range(len(vals[boneno])):
                if not ((frameno==0 and vals[boneno][frameno]==0) or
                        (frameno==(len(vals[boneno])-1) and vals[boneno][frameno]==1)):
                    props['%s_v%d' % (name, frameno+1)]=vals[boneno][frameno]
                if loops[boneno]:
                    props[name+'_loop']=loops[boneno]

    # Apply
    armature.removeAllProperties()
    keys=props.keys()
    keys.sort()
    for key in keys:
        armature.addProperty(key, props[key])

    # Create properties for the manipulators
    manipulator = manipulators[0]
    sub_dict = sorted(manipulator_dict[manipulator].keys())
    armature.addProperty('manipulator_type', manipulator )

    for field_id in sub_dict:
        field_name = field_id.split('@')[1]
        field_val = manipulator_dict[manipulator][field_id]
        property_name = manipulator + '_' + field_name
        if field_name == 'cursor':
            armature.addProperty(property_name, cursors[0])
        else:
            armature.addProperty(property_name, field_val)

    # Hide/Show - order is significant
    h=1
    s=1
    for hs in range(len(hideshow)):
        name=hideshow[hs].split('/')[-1]
        if hideshowindices[hs]!=None: name='%s[%d]' % (name, hideshowindices[hs])
        if hideorshow[hs]:
            armature.addProperty('%s_show_v%d' % (name, s), hideshowfrom[hs])
            armature.addProperty('%s_show_v%d' % (name, s+1), hideshowto[hs])
            s+=2
        else:
            armature.addProperty('%s_hide_v%d' % (name, h), hideshowfrom[hs])
            armature.addProperty('%s_hide_v%d' % (name, h+1), hideshowto[hs])
            h+=2

    Draw.Exit()
    if editmode: Window.EditMode(1)
    Window.RedrawAll()    # in case bone names have changed
    return