User Tools

Site Tools


blender

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
blender [2023/05/10 14:21] ssm2017blender [2023/05/17 09:27] (current) ssm2017
Line 728: Line 728:
 writeCSV(inventory) writeCSV(inventory)
 print("========================================") print("========================================")
 +</sxh>
 +
 +==== script as module ====
 +<sxh python>
 +import bpy
 +my_module = bpy.data.texts["Text"].as_module()
 +
 +my_module.toto()
 +</sxh>
 +
 +==== delete collection ====
 +<sxh python>
 +# source : https://blender.stackexchange.com/a/173894
 +
 +import bpy
 +#from bpy import context
 +
 +
 +name = "Collection 1"
 +remove_collection_objects = True
 +
 +#coll = context.collection # 
 +coll = bpy.data.collections.get(name)
 +
 +if coll:
 +    if remove_collection_objects:
 +        obs = [o for o in coll.objects if o.users == 1]
 +        while obs:
 +            bpy.data.objects.remove(obs.pop())
 +
 +    bpy.data.collections.remove(coll)
 +</sxh>
 +
 +==== duplicate collection linked ====
 +<sxh python>
 +import bpy
 +
 +def duplicate_collection_by_name(collection_name):
 +    original_collection = bpy.data.collections.get(collection_name)
 +    
 +    if original_collection is None:
 +        print(f"Collection '{collection_name}' does not exist.")
 +        return
 +    
 +    new_collection = original_collection.copy()
 +    new_collection.name = f"{collection_name}_duplicate"
 +    
 +    bpy.context.scene.collection.children.link(new_collection)
 +
 +</sxh>
 +
 +==== duplicate collection ====
 +<sxh python>
 +import bpy
 +
 +def duplicate_collection_by_name(collection_name):
 +    original_collection = bpy.data.collections.get(collection_name)
 +    
 +    if original_collection is None:
 +        print(f"Collection '{collection_name}' does not exist.")
 +        return
 +    
 +    new_collection = bpy.data.collections.new(f"{collection_name}_duplicate")
 +    bpy.context.scene.collection.children.link(new_collection)
 +    
 +    for obj in original_collection.objects:
 +        new_object = obj.copy()
 +        new_object.data = obj.data.copy()
 +        new_collection.objects.link(new_object)
 +
 +</sxh>
 +
 +==== delete empty collection ====
 +<sxh python>
 +import bpy
 +
 +def delete_empty_collections():
 +    collections = bpy.data.collections
 +    
 +    for collection in collections:
 +        if len(collection.objects) == 0:
 +            bpy.data.collections.remove(collection, do_unlink=True)
 +
 +</sxh>
 +
 +==== delete collection and its content ====
 +<sxh python>
 +import bpy
 +
 +def delete_collection_by_name(collection_name):
 +    collection = bpy.data.collections.get(collection_name)
 +    
 +    if collection is not None:
 +        bpy.data.collections.remove(collection, do_unlink=True)
 +    else:
 +        print(f"Collection '{collection_name}' does not exist.")
 </sxh> </sxh>
  
 {{tag>blender}} {{tag>blender}}
blender.1683721272.txt.gz · Last modified: by ssm2017