0
$\begingroup$

I would like to know how to get a list of drivers used in a certain bone. like how many and what type and what kind of variables it's using. I know I need to use bpy.context.object.animation_data.drivers but somehow I can't achive what I want.

If you know how to do that, please tell me.

Thank you in advance

$\endgroup$

1 Answer 1

2
$\begingroup$

I haven't found a super elegant way to access a driver directly from the bone, if someone knows that please let me know in the comments. But what you are trying to do can be achieved by building your own Python Dictionary over the armature in question. I assume that you have an armature with drivers on it, and it is the current active object. I further assume you have a console open (on Windows you can open it from the 3D View Main Menu, on Linux and Mac you need to start Blender from the console). Now run this from the Text Editor:

import bpy

# create an empty dictionary to store all found bones and drivers in
boneDict = {}

# iterate over all bones of the active object
for bone in bpy.context.object.pose.bones:

    # iterate over all drivers now
    # this should give better performance than the other way around
    # as most armatures have more bones than drivers
    foundDrivers = []
    for d in bpy.context.object.animation_data.drivers:

        # a data path looks like this: 'pose.bones["Bone.002"].scale'
        # search for the full bone name including the quotation marks!
        if ('"%s"' % bone.name) in d.data_path:

            # we now have identified that there is a driver 
            # which refers to a bone channel
            foundDrivers.append(d)

    # if there are drivers, add an item to the dictionary
    if foundDrivers:
        print ('adding drivers of bone %s to Dictionary' % bone.name)

        # the dictionary uses the bone name as the key, and the
        # found FCurves in a list as the values
        boneDict[bone.name] = foundDrivers

# now you have a dictionary in hand, which you can use to
# retrieve all driver info from
print ('bonedict: %s' % boneDict)



# usage examples

# get all drivers of the active bone, if it has some
boneName = bpy.context.active_bone.name

# first check if it has drivers at all
if boneName in boneDict.keys():
    # if so, access the drivers list by using the bone name as the key
    activeBoneDrivers = boneDict[boneName]

    # get number of drivers:
    print('Number of drivers for bone %s: %i' % (boneName, len(activeBoneDrivers)))

    # iterate over those drivers only:
    for d in activeBoneDrivers:
        # print the expression
        print('expression: %s' % d.driver.expression)

        # print if driver actually works:
        print('driver is valid: %s' % d.driver.is_valid)

        # get the type
        print('driver type: %s' % d.driver.type)

        # print the variables
        for var in d.driver.variables:
            print('variables: name %s, type %s' % (var.name, var.type))

            for target in var.targets:
                # you can also iterate over the targets
                # if you use Pythons dir() function, you will see what methods and properties you
                # can access:
                print ('targets methods: %s' % dir(target))

I tried to comment the code as completely as possible, so all explanation how this works is in there. Also, I chose to add the FCurve to the dictionary instead of the Driver directly. FCurves can sometimes be used to further tweak the driver, so you might want to access the curve and its Modifiers too. If you have further questions, feel free to ask :)

$\endgroup$
2
  • $\begingroup$ Thank you very much for the detailed answer! I will try this! $\endgroup$
    – Miumiu
    Aug 1, 2016 at 16:53
  • $\begingroup$ S far iterating through the object driver list and checking the the bone name vs the driver datapath is the only way I could figure out. Then I searched here and found your same solution. $\endgroup$
    – patmo141
    Nov 19, 2017 at 15:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .