100 lines
4.1 KiB
Python
100 lines
4.1 KiB
Python
import bpy
|
|
|
|
def apply_subdiv_to_wgt_objects():
|
|
"""Apply subdivision surface modifier to all objects starting with 'WGT-'"""
|
|
|
|
# Find the WGT collection
|
|
wgt_collection = None
|
|
wgt_layer_collection = None
|
|
|
|
for collection in bpy.data.collections:
|
|
if collection.name.startswith("WGT"):
|
|
wgt_collection = collection
|
|
break
|
|
|
|
# Find the corresponding layer collection
|
|
if wgt_collection:
|
|
def find_layer_collection(layer_collection, target_collection):
|
|
if layer_collection.collection == target_collection:
|
|
return layer_collection
|
|
for child in layer_collection.children:
|
|
result = find_layer_collection(child, target_collection)
|
|
if result:
|
|
return result
|
|
return None
|
|
|
|
wgt_layer_collection = find_layer_collection(bpy.context.view_layer.layer_collection, wgt_collection)
|
|
|
|
# Store original collection visibility settings
|
|
original_settings = {}
|
|
if wgt_collection and wgt_layer_collection:
|
|
original_settings = {
|
|
'collection_hide_viewport': wgt_collection.hide_viewport,
|
|
'collection_hide_render': wgt_collection.hide_render,
|
|
'collection_hide_select': wgt_collection.hide_select,
|
|
'layer_exclude': wgt_layer_collection.exclude,
|
|
'layer_hide_viewport': wgt_layer_collection.hide_viewport
|
|
}
|
|
|
|
# Temporarily enable all visibility settings
|
|
wgt_collection.hide_viewport = False
|
|
wgt_collection.hide_render = False
|
|
wgt_collection.hide_select = False
|
|
wgt_layer_collection.exclude = False
|
|
wgt_layer_collection.hide_viewport = False
|
|
print(f"Temporarily enabled WGT collection '{wgt_collection.name}' visibility")
|
|
|
|
try:
|
|
# Get all objects in the scene
|
|
all_objects = bpy.context.scene.objects
|
|
|
|
# Filter objects that start with "WGT-"
|
|
wgt_objects = [obj for obj in all_objects if obj.name.startswith("WGT-")]
|
|
|
|
if not wgt_objects:
|
|
print("No objects found starting with 'WGT-'")
|
|
return
|
|
|
|
print(f"Found {len(wgt_objects)} objects starting with 'WGT-':")
|
|
|
|
applied_count = 0
|
|
for obj in wgt_objects:
|
|
print(f" - {obj.name}")
|
|
|
|
# Find and apply subdivision surface modifier if it exists
|
|
subdiv_mod = None
|
|
for mod in obj.modifiers:
|
|
if mod.type == 'SUBSURF':
|
|
subdiv_mod = mod
|
|
break
|
|
|
|
if subdiv_mod:
|
|
# Make object active
|
|
bpy.context.view_layer.objects.active = obj
|
|
|
|
# Make object single-user if it has multi-user data
|
|
if obj.data.users > 1:
|
|
obj.data = obj.data.copy()
|
|
print(f" Made {obj.name} single-user")
|
|
|
|
# Apply the subdivision modifier
|
|
bpy.ops.object.modifier_apply(modifier=subdiv_mod.name)
|
|
print(f" Applied subdivision modifier to {obj.name}")
|
|
applied_count += 1
|
|
else:
|
|
print(f" {obj.name} has no subdivision modifier to apply")
|
|
|
|
print(f"\nApplied subdivision to {applied_count} objects")
|
|
|
|
finally:
|
|
# Restore original collection visibility settings
|
|
if wgt_collection and wgt_layer_collection and original_settings:
|
|
wgt_collection.hide_viewport = original_settings['collection_hide_viewport']
|
|
wgt_collection.hide_render = original_settings['collection_hide_render']
|
|
wgt_collection.hide_select = original_settings['collection_hide_select']
|
|
wgt_layer_collection.exclude = original_settings['layer_exclude']
|
|
wgt_layer_collection.hide_viewport = original_settings['layer_hide_viewport']
|
|
print(f"Restored WGT collection '{wgt_collection.name}' visibility settings")
|
|
|
|
# For Serpens compatibility - execute the function
|
|
apply_subdiv_to_wgt_objects() |