2
$\begingroup$

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?

$\endgroup$

2 Answers 2

3
$\begingroup$

A much more simple solution would be:

  1. Select "object b", over shape key Value, right mouse click and Copy as New Driver

enter image description here

  1. Select "object a", Rotation Z value and Paste Driver

enter image description here

  1. Now change driver settings to Scripted Expression and change expression to value*2*pi

enter image description here

result:

enter image description here

$\endgroup$
5
  • $\begingroup$ That's the way! Thanks! $\endgroup$
    – Franson Wu
    Commented Jul 7, 2022 at 23:36
  • $\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 get active_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$
    – Franson Wu
    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
1
$\begingroup$

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

enter image description here

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

enter image description here

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

enter image description here

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.

$\endgroup$
2
  • 1
    $\begingroup$ Cool! Many thanks! $\endgroup$
    – Franson Wu
    Commented Jul 7, 2022 at 9:04
  • $\begingroup$ you're welcome! glad it worked out :) $\endgroup$
    – Harry McKenzie
    Commented Jul 7, 2022 at 9:05

You must log in to answer this question.

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