Last active
June 24, 2025 20:51
-
-
Save dmlary/c5f0aea0f9befdf78f9b2b21277e31b7 to your computer and use it in GitHub Desktop.
Blender Addon: Copy Collection Custom Properties to Collection Instance
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
# work-around for https://projects.blender.org/blender/blender/issues/140819 | |
bl_info = { | |
"name": "Copy Collection Custom Properties", | |
"description": "Copy Collection custom properties when Collection instances are created", | |
"author": "David M. Lary", | |
"version": (0.2,4), | |
"blender": (2, 80, 0), | |
"category": "Development" | |
} | |
import bpy, _bpy | |
from bpy.app.handlers import persistent | |
# Keep track of known objects | |
known_objects = set() | |
@persistent | |
def copy_collection_properties(scene): | |
global known_objects | |
# pull the list of current objects, and get the list of new objects from it | |
current_objects = set(obj.name for obj in bpy.data.objects) | |
new_object_names = current_objects - known_objects | |
# replace the known objects list because one may have been removed | |
known_objects = current_objects | |
# iterate through new objects, for the collection instances, copy the | |
# custom properties from the collection to the instance object. | |
for name in new_object_names: | |
obj = bpy.data.objects.get(name) | |
if not hasattr(obj, "instance_type") or obj.instance_type != 'COLLECTION': | |
continue | |
col = obj.instance_collection | |
# print(f"copy collection properties: {name}") | |
for key in col.keys(): | |
obj[key] = col[key] | |
@persistent | |
def on_file_loaded(_path): | |
# update the known object list | |
global known_objects | |
known_objects = set(obj.name for obj in bpy.data.objects) | |
def register(): | |
# Use unrestricted data to get the current object list. | |
# If we don't do this, the first time we add an object, we'll overwrite any | |
# existing values | |
global known_objects | |
known_objects = set(obj.name for obj in _bpy.data.objects) | |
if copy_collection_properties not in bpy.app.handlers.depsgraph_update_post: | |
bpy.app.handlers.depsgraph_update_post.append(copy_collection_properties) | |
if on_file_loaded not in bpy.app.handlers.load_post: | |
bpy.app.handlers.load_post.append(on_file_loaded) | |
def unregister(): | |
if on_file_loaded in bpy.app.handlers.load_post: | |
bpy.app.handlers.load_post.remove(on_file_loaded) | |
if copy_collection_properties in bpy.app.handlers.depsgraph_update_post: | |
bpy.app.handlers.depsgraph_update_post.remove(copy_collection_properties) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment