Created
June 15, 2024 11:09
-
-
Save gyosit/74878dea9cd8a2e0ddf653b2fe4d2008 to your computer and use it in GitHub Desktop.
This file contains 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
"""Shape key syncronization | |
* This script create drivers for shape keys in selected objects to reference the same named shape keys in the base object. | |
1. Select the objects for which you want to create drivers. | |
2. While holding the shift key, select the base object. | |
3. Run this script. | |
4. When you change any shape key of the base object, the corresponding shape key in the other objects will follow! | |
* Disclaimer: This script is provided "as is" without any warranties. | |
Use it at your own risk. | |
The author assumes no responsibility for any issues or damages that may arise from using this script. | |
""" | |
import bpy | |
from mathutils import Vector | |
def get_objects(): | |
""" | |
Get active object and selected objects | |
Returns: | |
Object: Active object | |
List[Object]: Selected objects | |
""" | |
active_obj = bpy.context.active_object | |
selected_objects = bpy.context.selected_objects | |
return active_obj, selected_objects | |
def set_key_drivers(active_obj, selected_objects, cleanup=False): | |
""" | |
Create drivers for shape keys that reference the same named shape key of the active object for the selected objects | |
Args: | |
active_obj (Object): Active object | |
selected_objects (List[Object]): Selected objects | |
cleanup (bool): If remove existing drivers from the shape key. Default is False | |
""" | |
for obj in selected_objects: | |
mesh = obj.data | |
shape_keys = mesh.shape_keys | |
for shape_key in shape_keys.key_blocks.keys(): | |
print(shape_key) | |
if shape_key == "Basis": | |
continue | |
if cleanup: | |
clear_key_driver(targ_obj=obj, shape_key=shape_key) | |
set_key_driver(ref_obj=active_obj, targ_obj=obj, shape_key=shape_key) | |
def clear_key_driver(targ_obj, shape_key): | |
""" | |
Remove all drivers from the shape key | |
Args: | |
targ_obj (Object): The object which you want to remove drivers | |
shape_key (str): The name of shape key | |
""" | |
if targ_obj.data.shape_keys.key_blocks.get(shape_key, None) is None: | |
print(f"Shape key {shape_key} is not found") | |
return | |
targ_obj.data.shape_keys.key_blocks[shape_key].driver_remove("value") | |
def set_key_driver(ref_obj, targ_obj, shape_key): | |
""" | |
Create driver for the shape key that reference the same named shape key of the active object for the selected objects | |
Args: | |
ref_obj (Object): The object which have the drivers that you want copy | |
targ_obj (Object): The object which you want to set the drivers | |
shape_key (str): The name of shape key | |
""" | |
if ref_obj == targ_obj: | |
return | |
d = targ_obj.data.shape_keys.key_blocks[shape_key].driver_add("value") | |
d.driver.type = 'AVERAGE' | |
var = d.driver.variables.new() | |
var.name = "value" | |
var.type = "SINGLE_PROP" | |
var.targets[0].id_type = "KEY" | |
var.targets[0].id = ref_obj.data.shape_keys | |
var.targets[0].data_path = f"key_blocks[\"{shape_key}\"].value" | |
if __name__ == "__main__": | |
active_obj, selected_objects = get_objects() | |
set_key_drivers(active_obj, selected_objects, True) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment