Scripting newb here.

I have thousands of unique objects named plane.001, plane.002 etc. They all unique meshes with a single shapekey apart from the basis. The shapekey has a driver on its evaluation time and the variable type of the driver is distance, with an empty as the first target.

I'm trying to write a script that changes the second target of my distance variable to the same object that contains the driver. Since I simply duplicated the original object thousands of times, they all have the original object as the second target.

I'm hoping someone will throw me a bone here, show me a viable script and explain what's going on. I do have experience with programming languages but I'm very much an amateur.

Thanks a lot lot lot!


1 Answer 1

import bpy
context = bpy.context
scene = context.scene

for obj in scene.objects:
    mesh = obj.data
    if not mesh or not hasattr(mesh, "shape_keys"):

    # shape key collection
    shapekeys = mesh.shape_keys

    # animation data
    ad = getattr(shapekeys, "animation_data", None)    
    # no animtion data no drivers
    if not ad:

    #select the driver part of fcurves in drivers list
    drivers = [fc.driver for fc in ad.drivers]

    for d in drivers:
        #variables of type distance
        vars = [v for v in d.variables if v.type == 'LOC_DIFF']
        for v in vars:
            # set the target 2 to obj that has driver.
            v.targets[1].id = obj
  • $\begingroup$ Works perfectly. Thanks so much for the taking the time! I probably wouldn't have got here by myself. $\endgroup$
    – HowToBlend
    Mar 17, 2016 at 17:11

You must log in to answer this question.

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