Skip to content

Instantly share code, notes, and snippets.

@CGArtPython
Created August 23, 2024 07:42
Show Gist options
  • Save CGArtPython/d446d51e08f4d6bd277134173d1311e2 to your computer and use it in GitHub Desktop.
Save CGArtPython/d446d51e08f4d6bd277134173d1311e2 to your computer and use it in GitHub Desktop.
This Blender Python script adds 5 empty objects to the current scene and then assigns drivers to each empty. The drivers are set to control the X location of each empty based on the X location of the next empty in the list, multiplied by 0.5.
import bpy # Import the Blender Python API
def add_empties():
empties = [] # Initialize an empty list to store the empty objects
for i in range(5): # Loop to create 5 empty objects
empty = bpy.data.objects.new(f"Empty_{i+1}", None) # Create a new empty object with a unique name
bpy.context.collection.objects.link(empty) # Link the empty object to the current collection
empties.append(empty) # Add the empty object to the list
return empties # Return the list of empty objects
def add_drivers(empties):
for i, empty in enumerate(empties): # Loop through each empty object with its index
driver = empty.driver_add("location", 0).driver # Add a driver to the X location of the empty object
var = driver.variables.new() # Create a new variable for the driver
var.name = "var" # Name the variable "var"
var.targets[0].id_type = 'OBJECT' # Set the target type to 'OBJECT'
var.targets[0].id = empties[(i+1) % 5] # Set the target object to the next empty in the list (circular)
var.targets[0].data_path = "location.x" # Set the data path to the X location of the target object
driver.expression = "var * 0.5" # Set the driver expression to multiply the target's X location by 0.5
# Main execution
empties = add_empties() # Call the function to add empties and store the returned list
add_drivers(empties) # Call the function to add drivers to the empties
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment