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
blender [2023/05/16 15:17] ssm2017blender [2023/05/17 09:27] (current) ssm2017
Line 759: Line 759:
  
     bpy.data.collections.remove(coll)     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> </sxh>
  
Line 765: Line 783:
 import bpy import bpy
  
-# deselect all +def duplicate_collection_by_name(collection_name): 
-bpy.ops.object.select_all(action='DESELECT') +    original_collection = bpy.data.collections.get(collection_name) 
-# get collection +     
-col = bpy.data.collections["tdg"]+    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>
  
-for window in bpy.context.window_manager.windows: +==== delete empty collection ==== 
-    screen window.screen+<sxh python> 
 +import bpy
  
-    for area in screen.areas+def delete_empty_collections(): 
-        if area.type == 'OUTLINER': +    collections = bpy.data.collections 
-            override = {'window': window, 'screen': screen, 'area': area} +     
-            bpy.ops.outliner.collection_duplicate(override) +    for collection in collections
-            break +        if len(collection.objects) == 0
-         +            bpy.data.collections.remove(collection, do_unlink=True)
-# bpy.ops.outliner.item_activate(deselect_all=True)+
  
-# bpy.ops.outliner.delete(hierarchy=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.1684243078.txt.gz · Last modified: by ssm2017