1
$\begingroup$

I am trying to copy shape keys drivers from one mesh to another. For some reason when I copy over the variables attributes the transform_space attribute doesn't change. I am printing the attribute value after I copied it over and it shows it has changed from "WORLD_SPACE" to "LOCAL_SPACE", but the driver's Variable property are still in world space. Everything else is working so I don't think I am picking the wrong data block. Any help is highly appreciated. Here is the code I wrote:

import bpy
import codecs

target_obj = bpy.data.objects["2ndMesh"]
source_obj =  bpy.data.objects["1stMesh"]
source_f_curves = source_obj.data.shape_keys.animation_data.drivers


target_shape_keys_names = [x.name for x in target_obj.data.shape_keys.key_blocks]
for source_f_curve in source_f_curves:
    source_driver = source_f_curve.driver

    # finding the input

    source_shape_key = '['.join(source_f_curve.data_path.split('[')[1:])
    source_shape_key = "]".join(source_shape_key.split("]")[:-1])[1:-1]
    source_shape_key = codecs.decode(source_shape_key, 'unicode_escape')
    if source_shape_key not in target_shape_keys_names:
        continue
    source_channel = source_f_curve.data_path.split(".")[-1]
    # create the target driver

    target_driver = target_obj.data.shape_keys.key_blocks[source_shape_key].driver_add(source_channel).driver
    # copying the data over to the target driver
    target_driver.type = source_driver.type
    # copying variables over
    target_vars = list()
    
    for source_var in source_driver.variables:
        target_var = target_driver.variables.new()
        for i, source_var_target in source_var.targets.items():
            target_var_target =  target_var.targets[i]
            target_var_target.id_type = source_var_target.id_type
            source_target_id = source_var_target.id
            # replace the shape key target id
            if source_target_id == source_obj.data.shape_keys:
                source_target_id = target_obj.data.shape_keys
            target_var_target.id = source_target_id
            print("Current space is: ",target_var.targets[i].transform_space )
            target_var_target.transform_type = source_var_target.transform_type
            target_var_target.data_path = source_var_target.data_path
            target_var_target.bone_target = source_var_target.bone_target
            target_var_target.rotation_mode = source_var_target.rotation_mode
            target_var_target.transform_space = source_var_target.transform_space
            print("After space is: ", target_var.targets[i].transform_space)
            
        
        target_var.name = source_var.name
        target_var.type = source_var.type
    target_driver.expression = source_driver.expression
$\endgroup$

1 Answer 1

0
$\begingroup$

You need change variable type first.

import bpy
import codecs

target_obj = bpy.data.objects["2ndMesh"]
source_obj =  bpy.data.objects["1stMesh"]
source_f_curves = source_obj.data.shape_keys.animation_data.drivers


target_shape_keys_names = [x.name for x in target_obj.data.shape_keys.key_blocks]
for source_f_curve in source_f_curves:
    source_driver = source_f_curve.driver

    # finding the input

    source_shape_key = '['.join(source_f_curve.data_path.split('[')[1:])
    source_shape_key = "]".join(source_shape_key.split("]")[:-1])[1:-1]
    source_shape_key = codecs.decode(source_shape_key, 'unicode_escape')
    if source_shape_key not in target_shape_keys_names:
        continue
    source_channel = source_f_curve.data_path.split(".")[-1]
    # create the target driver

    target_driver = target_obj.data.shape_keys.key_blocks[source_shape_key].driver_add(source_channel).driver
    # copying the data over to the target driver
    target_driver.type = source_driver.type
    # copying variables over
    target_vars = list()
    
    for source_var in source_driver.variables:
        target_var = target_driver.variables.new()
        target_var.name = source_var.name
        target_var.type = source_var.type
        for i, source_var_target in source_var.targets.items():
            target_var_target =  target_var.targets[i]
            # target_var_target.id_type = source_var_target.id_type
            source_target_id = source_var_target.id
            # replace the shape key target id
            if source_target_id == source_obj.data.shape_keys:
                source_target_id = target_obj.data.shape_keys
            target_var_target.id = source_target_id
            print("Current space is: ",target_var.targets[i].transform_space )
            target_var_target.transform_type = source_var_target.transform_type
            target_var_target.data_path = source_var_target.data_path
            target_var_target.bone_target = source_var_target.bone_target
            target_var_target.rotation_mode = source_var_target.rotation_mode
            target_var_target.transform_space = source_var_target.transform_space
            print("After space is: ", target_var.targets[i].transform_space)

    target_driver.expression = source_driver.expression
$\endgroup$

You must log in to answer this question.

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