change the target object of a variable Driver

I was able to find the connection.
bpy.context.selected_objects[0].animation_data.drivers[1].data_path

find the name of the variable or change the type
bpy.context.selected_objects[0].animation_data.drivers[1].driver.variables[0].name
bpy.context.selected_objects[0].animation_data.drivers[1].driver.variables[0].type

which blender say it’s DriverVariable.type DriverVariable.name

but I can’t figure out how to change the name of the target just under those parameters, for the name of the object controling the variable, it says DriverTarget.id, but I can’t find the correct function to change it.

thank you for your help.

Mathias.

1 Like

Is the syntax the same as when you right-click and select Copy Data Path from the pop-up menu?

the data path just give me

id

and the help brings me to that page,
http://www.blender.org/documentation/blender_python_api_2_62_release/bpy.types.DriverTarget.html#bpy.types.DriverTarget.id
but I don’t know how to use that.

Here is a code snippet. In the code belo speaker is a speaker object. channel is the name of a custom prop on the speaker and also used as a variable name.


                var = driver.variables.get(channel)
                if var is None:
                    var = driver.variables.new()
                var.type = "SINGLE_PROP"
                var.name = channel
                target = var.targets[0]
                target.id_type = "SPEAKER"
                target.id = speaker.id_data
                target.data_path = '["%s"]' % channel

Thanks batFINGER
so I was able to change the name of a variable of a driver with this function.

bpy.data.objects[‘Cube’].animation_data.drivers[0].driver.variables[‘test’].targets[0].id.name = ‘Cube2’

works like a charm!

Mathias.

mathiasA, the correct solution is

bpy.data.objects[‘Cube’].animation_data.drivers[0].driver.variables[‘test’].targets[0].id = bpy.data.objects[‘Cube2’]

Because, if you change the id.name, you are changing the old target object name.