I have come up with an easy workaround that will lessen your burden with three (3) easy steps using keying_sets
to do the coyping of data path.
1.) Right click ( RMB ) the property to drive then select Add to Keying Set
2.) Replace CUSTOM_PROPERTY
in script with the name
3.) Run Script
This will automatically add a driver to the property you have right clicked and is driven by the custom property CUSTOM_PROPERTY
. Note that the script will only run if you have done step 1. It automatically removes the keying_sets
after execution. Also note that if this thread can be answered then we can eliminate step 2 and it's fully automated.
import bpy
import re
CUSTOM_PROPERTY = "Surface Emission"
# First right click property and select "Add to Keying Set"
ks = bpy.context.scene.keying_sets[0].paths[0]
ks_use_entire_array = ks if ks.use_entire_array else False
modifier_id = re.findall('"([^"]*)"', ks.data_path)
is_modifier = len(modifier_id) > 0
if is_modifier:
dpath = ks.data_path.split(']')[1]
dpath = dpath[1:len(dpath)]
DATA_PATH = dpath
else:
DATA_PATH = ks.data_path
INDEX = -1 if ks_use_entire_array else ks.array_index
modifier_id = re.findall('"([^"]*)"', ks.data_path)
is_modifier = len(modifier_id) > 0
MODIFIER_NAME = modifier_id[0] if is_modifier else ''
obj = bpy.data.objects['Cube']
if CUSTOM_PROPERTY in obj:
print("WARNING: to prevent overwriting custom property, substituting with data_path name")
CUSTOM_PROPERTY = DATA_PATH
bpy.ops.anim.keying_set_remove()
obj[CUSTOM_PROPERTY] = 1 # you can add the custom property like this
if not obj.animation_data is None:
drivers = obj.animation_data.drivers
for d in drivers:
if d.data_path == DATA_PATH:
obj.driver_remove(d.data_path, INDEX)
def create_driver(fcurve, obj):
d = fcurve.driver
v = d.variables.new()
v.name = "myvar"
target = v.targets[0]
target.id_type = 'OBJECT'
target.id = obj
target.data_path = '["' + CUSTOM_PROPERTY + '"]'
d.expression = "myvar"
return target
if is_modifier:
mod = obj.modifiers[MODIFIER_NAME]
fcurve_or_list = mod.driver_add(DATA_PATH, INDEX)
else:
fcurve_or_list = obj.driver_add(DATA_PATH, INDEX)
if type(fcurve_or_list) is list:
index = 0
for fcurve in fcurve_or_list:
target = create_driver(fcurve, obj)
else:
target = create_driver(fcurve_or_list, obj)