Created
October 2, 2023 21:34
-
-
Save arif-pandu/422ee86c26057e3aebe65ba93e155620 to your computer and use it in GitHub Desktop.
Blender Script to Extract Collection children with type of Collection instances into new Collection and categorized as individual object
This file contains 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
import bpy | |
# Name of the main collection | |
main_collection_name = "MainCollection" | |
new_collection_name = "NewCollection" | |
# Get the main collection by name | |
main_collection = bpy.data.collections.get(main_collection_name) | |
if main_collection: | |
# Create a new collection for the extracted objects | |
new_collection = bpy.data.collections.new(new_collection_name) | |
bpy.context.scene.collection.children.link(new_collection) | |
# Iterate through all objects in the main collection | |
for obj in main_collection.objects: | |
# Check if the object is an instance (type 'COLLECTION') | |
if obj.instance_type == 'COLLECTION': | |
# Iterate through linked objects in the instance | |
for linked_obj in obj.instance_collection.objects: | |
# Duplicate the object and link it to the new collection | |
new_obj = linked_obj.copy() | |
# new_obj.matrix_world = obj.matrix_world.copy() | |
new_obj.location = obj.location.copy() + linked_obj.location.copy() | |
new_obj.scale = obj.scale.copy() * new_obj.scale.copy() | |
euler_new = linked_obj.rotation_euler.copy() | |
euler_new.x = linked_obj.rotation_euler.copy().x + obj.rotation_euler.copy().x | |
euler_new.y = linked_obj.rotation_euler.copy().y + obj.rotation_euler.copy().y | |
euler_new.z = linked_obj.rotation_euler.copy().z + obj.rotation_euler.copy().z | |
new_obj.rotation_euler = euler_new.copy() | |
new_collection.objects.link(new_obj) | |
# Clear the selection to avoid selecting objects in the new collection | |
bpy.ops.object.select_all(action='DESELECT') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment