Created
November 21, 2023 21:32
-
-
Save CGArtPython/77fcd50949af18b580770414912e060f to your computer and use it in GitHub Desktop.
An add-on example for creating a persistent library of Python-powered Blender driver functions (find out more about Blender Python drivers here https://www.youtube.com/watch?v=yiMGxlRv8h4&lc=UgwlEGLUyWsiKhPPlml4AaABAg )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# name this file __init__.py | |
import bpy | |
ADDON_NAME = "My Diver Library" | |
bl_info = { | |
"name": "My Diver Library", | |
"author": "Viktor Stepanov", | |
"version": (0, 0, 1), | |
"blender": (2, 83, 0), | |
"description": "A collection of Python diver functions.", | |
"category": "Development", | |
} | |
def custom_driver_1(v): | |
return v * 2 | |
def custom_driver_2(v): | |
return v * 3 | |
DRIVERS = { | |
"my_driver_1": custom_driver_1, | |
"my_driver_2": custom_driver_2, | |
} | |
def register_drivers(): | |
print(f"Delayed driver registration:") | |
for driver_name, driver_function in DRIVERS.items(): | |
print(f"\tRegistering driver {driver_name}") | |
bpy.app.driver_namespace[driver_name] = driver_function | |
def register(): | |
print(f'ENABLED "{ADDON_NAME}" addon') | |
# HACK: if we register the drivers right away they will not appear in the bpy.app.driver_namespace | |
# adding a 2 second delay to register them | |
bpy.app.timers.register(register_drivers, first_interval=2.0) | |
def unregister(): | |
print(f'DISABLE "{ADDON_NAME}" addon') | |
for driver_name, _ in DRIVERS.items(): | |
print(f"\tDeregistering driver {driver_name}") | |
bpy.app.driver_namespace.pop(driver_name, None) | |
if __name__ == "__main__": | |
register() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment