I need a run once simple script that creates a driver.
I want a simple 1 bone in a rig to drive 1 shape key.
I know how to modify a script but I have not found an existing script that I can edit to suit my own project.
I need a run once simple script that creates a driver.
I want a simple 1 bone in a rig to drive 1 shape key.
I know how to modify a script but I have not found an existing script that I can edit to suit my own project.
You don’t need any scripts to create a driver.
Take a look on this video:
I know I don’t NEED a script but I want a script to alleviate creating hundreds of drivers. Where just one character has around 30 of them.
well, previously you’ve said
anyway, here is the code:
import bpy
def newDriverByBoneLocation(object, shapeKeyName, armature, boneName, transformType, modifier):
sk = object.data.shape_keys
driver = sk.driver_add('key_blocks["'+shapeKeyName+'"].value',-1).driver
driver.type = 'SCRIPTED'
var = driver.variables.new()
var.type ='TRANSFORMS'
var.targets[0].id = armature
var.targets[0].bone_target = boneName
var.targets[0].transform_type = transformType
var.targets[0].transform_space = 'LOCAL_SPACE'
driver.expression = var.name + modifier
newDriverByBoneLocation(
object = bpy.data.objects['Cube'],
shapeKeyName = 'Key 1',
armature = bpy.data.objects['Armature'],
boneName = 'Bone',
transformType = 'LOC_X',
modifier = ' * 0.5'
)
and here is a blend file to test it:
create driver example.blend (741.6 KB)
I hope this solves your problem
This is truly delightful. This is a simple script but will save me a ton of time than manually editing every single driver. But not just that but as you can see playing around with the script I’m learning a bit of scripting also. As the best way I learn is by example.
Thank you so much!
# Big thanks to Jerzy Górski for writing this handy script.
import bpy
def newDriver(object, shapeKeyName, armature, boneName, transformType, modifier):
sk = object.data.shape_keys
driver = sk.driver_add('key_blocks["'+shapeKeyName+'"].value',-1).driver
driver.type = 'SCRIPTED'
var = driver.variables.new()
var.type ='TRANSFORMS'
var.targets[0].id = armature
var.targets[0].bone_target = boneName
var.targets[0].transform_type = transformType
var.targets[0].transform_space = 'LOCAL_SPACE'
driver.expression = modifier
newDriver(
object = bpy.data.objects['Body'],
shapeKeyName = 'Eye Blink L',
armature = bpy.data.objects['Renka'],
boneName = 'Blink.L',
transformType = 'LOC_Y',
modifier = '(var *-20) if var <0 else 0'
)
newDriver(
object = bpy.data.objects['Body'],
shapeKeyName = 'Eye Blink R',
armature = bpy.data.objects['Renka'],
boneName = 'Blink.R',
transformType = 'LOC_Y',
modifier = '(var *-20) if var <0 else 0'
)