I have two objects, mesh A and mesh B, where mesh B has a shape key named shape-change with a value range of 0 to 1.
I added a driver to the rotation Z
property of mesh A which should make it rotate when the value of the shape key of mesh B changes. How do I implement the driver script?
2 Answers
A much more simple solution would be:
- Select "object b", over shape key Value, right mouse click and Copy as New Driver
- Select "object a", Rotation Z value and Paste Driver
- Now change driver settings to Scripted Expression and change expression to
value*2*pi
result:
-
-
$\begingroup$ awesome! that's good to know that there is paste driver. but for some reason it does not paste it correctly in my blender 3.1. i get path "active_shape_key_index" but i see in your path you get "keyblock... something" $\endgroup$– Harry McKenzie ♦Commented Jul 7, 2022 at 23:47
-
$\begingroup$ @FransonWu can you confirm what value you get in
Path
after you do Paste Driver? I only getactive_shape_key_index
so it doesn't work for me. $\endgroup$– Harry McKenzie ♦Commented Jul 7, 2022 at 23:53 -
1$\begingroup$ I get this " key_blocks["Key 1"].value ", works. Blender version 3.0.1. $\endgroup$ Commented Jul 8, 2022 at 1:02
-
$\begingroup$ @FransonWu thank you for checking. That's really odd. I also checked on 2.93, 3.0, 3.1 and it also doesn't work. Maybe because i'm on Windows 7. $\endgroup$– Harry McKenzie ♦Commented Jul 8, 2022 at 1:16
Follow my setup: make sure object A is named mesh-obj-a and its mesh is named mesh-a and object B is named mesh-obj-b and its mesh is named mesh-b. Also make sure that object B has a shape key under Shape Keys named shape-change
Now, there's two ways to do this. The first method or easy way is to execute this python script in the Text Editor and you're done.
import bpy
obj_a = bpy.data.objects['mesh-obj-a']
obj_b = bpy.data.objects['mesh-obj-b']
drivers = obj_a.animation_data.drivers
for d in drivers:
obj_a.driver_remove(d.data_path, -1)
fcurve = obj_a.driver_add("rotation_euler", 2)
d = fcurve.driver
v = d.variables.new()
v.name = "keys"
target = v.targets[0]
target.id_type = 'MESH'
target.id = obj_b.data
target.data_path = "shape_keys"
d.expression = "radians(keys.key_blocks['shape-change'].value*360)"
The second method is the manual way by doing the following:
Select object A and right click its Rotation Z
property in the Object Properties and select Add Driver
Click the X
icon to delete the default input variable and click Add Input Variable. Change the variable name from var to keys. Then change the ID-Block type from Object to Mesh and select the mesh mesh-b
In the Expression field input the following script which converts the shape key value 0-1
to 0-360
as radians (0.0174533 is the factor for degrees to radians conversion):
keys.key_blocks['shape-change'].value*360*0.0174533
or better:
radians(keys.key_blocks['shape-change'].value*360)
In the Path field, input the value shape_keys
Now when you change the value of the shape key of object B from 0 to 1, it will drive or rotate object A on the z-axis from 0 to 360 degrees.
-
1
-
$\begingroup$ you're welcome! glad it worked out :) $\endgroup$– Harry McKenzie ♦Commented Jul 7, 2022 at 9:05