I want to add a driver to multiple objects within a collection using an empty as target object. This is how far I came . I am facing two problems: 1. the script only adds a driver to the active object, not to ALL of the objects. 2. The Python tell me that linking the empty as target object should be something with DriverTarget.id but I cannot figure out where to put this within my code.
1 Answer
$\begingroup$
$\endgroup$
import bpy
empty = bpy.data.objects["Empty"]
for obj in bpy.data.collections["Collection"].all_objects:
if obj.type == "MESH":
fcs = obj.driver_add("delta_location", -1)
for i, fc in enumerate(fcs):
dr = fc.driver
v = fc.driver.variables.new()
tar0 = v.targets[0]
tar0.id = empty
tar0.data_path = f"location[{i}]"
dr.expression = v.name