Blender: Activate rigid body based on Z height avatar
2 months ago
2.1 kB
# Apply custom boolean property and a driver
# that looks at an objects Z location

import bpy

# Function to add a custom property and apply a driver to it
def add_driver_to_object(obj):
    # Add a custom property to the object
    obj["AnimationDisabled"] = False  # Default value is False

    # Add a driver to the custom property
    driver = obj.driver_add('["AnimationDisabled"]')

    # Set the type of the driver to SCRIPTED
    driver.driver.type = 'SCRIPTED'
    driver.driver.use_self = True

 # Set the expression for the driver with Z location as input
    driver.driver.expression = '1 if var + self.location.z < 0 else self["AnimationDisabled"]'

    # Manually set up the variable and target for Z location
    var =
    var.type = 'TRANSFORMS'
    # Object to get Z height for
    var.targets[0].id =["Empty"]
    var.targets[0].transform_type = 'LOC_Z'

    # Update the dependencies

# Iterate through selected objects and apply the driver
selected_objects = bpy.context.selected_objects
for obj in selected_objects:

# Iterate through all selected objects
for selected_object in bpy.context.selected_objects:
    # Check if the object has a rigid body
    if selected_object.rigid_body:
        # Access the Rigid Body settings
        rigid_body_settings = selected_object.rigid_body

        # Create a new driver for the "Animated" property
        driver = rigid_body_settings.driver_add("kinematic")

        # Access the driver data
        driver_data = driver.driver

        # Set up the driver expression
        driver.driver.use_self = True
        driver_data.expression = 'not self.id_data["AnimationDisabled"]'  # Example expression, you can customize this

        # Optionally, update the driver to rebuild the expression
        driver_data.expression = driver_data.expression

# Update the scene to reflect the changes
Leave a Comment