Advertisement
Guest User

Blender: De-activate Rigid Body animation based on Z distance

a guest
Jan 6th, 2024
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | Software | 0 0
  1. # Apply custom boolean property and a driver
  2. # that looks at an objects Z location
  3.  
  4.  
  5. import bpy
  6.  
  7. # Function to add a custom property and apply a driver to it
  8. def add_driver_to_object(obj):
  9.     # Add a custom property to the object
  10.     obj["AnimationDisabled"] = False  # Default value is False
  11.  
  12.     # Add a driver to the custom property
  13.     driver = obj.driver_add('["AnimationDisabled"]')
  14.  
  15.     # Set the type of the driver to SCRIPTED
  16.     driver.driver.type = 'SCRIPTED'
  17.     driver.driver.use_self = True
  18.  
  19.  # Set the expression for the driver with Z location as input
  20.     driver.driver.expression = '1 if var + self.location.z < 0 else self["AnimationDisabled"]'
  21.  
  22.     # Manually set up the variable and target for Z location
  23.     var = driver.driver.variables.new()
  24.     var.type = 'TRANSFORMS'
  25.    
  26.     # Object to get Z height for
  27.     var.targets[0].id = bpy.data.objects["Empty"]
  28.     var.targets[0].transform_type = 'LOC_Z'
  29.  
  30.     # Update the dependencies
  31.     bpy.context.view_layer.update()
  32.  
  33.  
  34. # Iterate through selected objects and apply the driver
  35. selected_objects = bpy.context.selected_objects
  36. for obj in selected_objects:
  37.     add_driver_to_object(obj)
  38.  
  39.  
  40.  
  41.  
  42. # Iterate through all selected objects
  43. for selected_object in bpy.context.selected_objects:
  44.     # Check if the object has a rigid body
  45.     if selected_object.rigid_body:
  46.         # Access the Rigid Body settings
  47.         rigid_body_settings = selected_object.rigid_body
  48.  
  49.         # Create a new driver for the "Animated" property
  50.         driver = rigid_body_settings.driver_add("kinematic")
  51.  
  52.         # Access the driver data
  53.         driver_data = driver.driver
  54.  
  55.         # Set up the driver expression
  56.         driver.driver.use_self = True
  57.         driver_data.expression = 'not self.id_data["AnimationDisabled"]'  # Example expression, you can customize this
  58.  
  59.  
  60.         # Optionally, update the driver to rebuild the expression
  61.         driver_data.expression = driver_data.expression
  62.  
  63.  
  64. # Update the scene to reflect the changes
  65. bpy.context.view_layer.update()
Tags: blender
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement