Skip to content

Instantly share code, notes, and snippets.

@CGArtPython
Created November 11, 2024 06:00
Show Gist options
  • Save CGArtPython/0057da721ef7b64eed8a86374669eadf to your computer and use it in GitHub Desktop.
Save CGArtPython/0057da721ef7b64eed8a86374669eadf to your computer and use it in GitHub Desktop.
Code for the tutorial video "Easily Create Driver Variables with Blender Python" (https://youtu.be/m-OFyHHY4KI)
import bpy
bpy.ops.mesh.primitive_ico_sphere_add()
ico_sphere_obj = bpy.context.active_object
bpy.ops.object.empty_add()
empty_obj = bpy.context.active_object
driver_fcurve_obj = ico_sphere_obj.driver_add("location", 0)
driver_obj = driver_fcurve_obj.driver
my_var = driver_obj.variables.new()
my_var.name = "py_var"
my_var.targets[0].id = empty_obj
my_var.targets[0].data_path = "location.z"
driver_obj.expression = "py_var * 0.5"
import bpy
bpy.ops.mesh.primitive_ico_sphere_add()
ico_sphere_obj = bpy.context.active_object
bpy.ops.object.empty_add()
empty_obj = bpy.context.active_object
# https://docs.blender.org/api/current/bpy.types.bpy_struct.html#bpy.types.bpy_struct.driver_add
driver_fcurve_obj = ico_sphere_obj.driver_add("location", 0)
# https://docs.blender.org/api/current/bpy.types.FCurve.html#bpy.types.FCurve.driver
driver_obj = driver_fcurve_obj.driver
# https://docs.blender.org/api/current/bpy.types.Driver.html#bpy.types.Driver.variables
my_var = driver_obj.variables.new()
# https://docs.blender.org/api/current/bpy.types.DriverVariable.html#bpy.types.DriverVariable.name
my_var.name = "py_var"
# https://docs.blender.org/api/current/bpy.types.DriverTarget.html#bpy.types.DriverTarget.id
my_var.targets[0].id = empty_obj
# https://docs.blender.org/api/current/bpy.types.DriverTarget.html#bpy.types.DriverTarget.data_path
my_var.targets[0].data_path = "location.z"
# https://docs.blender.org/api/current/bpy.types.Driver.html#bpy.types.Driver.expression
driver_obj.expression = "py_var * 0.5"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment