Created
November 18, 2024 08:56
-
-
Save CGArtPython/3d40eadf5c28f886a9e3c8bd3c801058 to your computer and use it in GitHub Desktop.
[Blender Python] How to copy Drivers with Python? (explanation https://www.skool.com/cgpython/how-to-copy-drivers-with-python?p=fc7b213c)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
def copy_drivers(): | |
# Get the active object (the one with the drivers to copy) | |
source_obj = bpy.context.active_object | |
print(f"Active object: {source_obj.name}") | |
if not source_obj.animation_data or not source_obj.animation_data.drivers: | |
print("No drivers found on the active object.") | |
return | |
# Get all selected objects excluding the active one | |
selected_objs = [obj for obj in bpy.context.selected_objects if obj != source_obj] | |
if not selected_objs: | |
print("No objects selected.") | |
return | |
for obj in selected_objs: | |
if not obj.animation_data: | |
obj.animation_data_create() | |
drivers_copied = False | |
# Loop over each axis (0=X, 1=Y, 2=Z) | |
for axis in range(3): | |
# Check if a driver exists for the current axis | |
source_driver_fcurve = source_obj.animation_data.drivers.find("location", index=axis) | |
if not source_driver_fcurve: | |
continue | |
print(f"Copying driver for axis {axis}...") | |
for target_obj in selected_objs: | |
# Create an fcurve data block from the source to the target | |
target_obj.animation_data.drivers.from_existing(src_driver=source_driver_fcurve) | |
drivers_copied = True | |
if drivers_copied: | |
print("Drivers copied successfully!") | |
copy_drivers() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment