222 lines
8.1 KiB
Python
222 lines
8.1 KiB
Python
import bpy
|
|
|
|
print("=== Mask Settings Script Starting ===")
|
|
|
|
# Check if auto-execution is enabled for drivers
|
|
print(f"Auto-execution enabled: {bpy.context.preferences.filepaths.use_scripts_auto_execute}")
|
|
if not bpy.context.preferences.filepaths.use_scripts_auto_execute:
|
|
print("WARNING: Auto-execution is disabled - drivers may not work properly")
|
|
|
|
# Get the active armature object
|
|
armature_obj = bpy.context.active_object
|
|
if not armature_obj or armature_obj.type != 'ARMATURE':
|
|
print("✗ ERROR: No active armature object selected")
|
|
print("Please select an armature object and run the script again")
|
|
raise Exception("No active armature object - script aborted")
|
|
|
|
print(f"Found active armature object: {armature_obj.name}")
|
|
|
|
# Get the armature data
|
|
armature = armature_obj.data
|
|
print(f"Using armature data: {armature.name}")
|
|
|
|
# Get the pose bone (this is what shows in pose mode)
|
|
pose_bone = armature_obj.pose.bones.get('Settings')
|
|
if not pose_bone:
|
|
print("✗ ERROR: Settings pose bone not found in armature")
|
|
raise Exception("Settings pose bone not found - script aborted")
|
|
|
|
print("✓ Settings pose bone found")
|
|
|
|
# Find the Work_gloves object
|
|
work_gloves_obj = bpy.data.objects.get('Work_gloves')
|
|
if not work_gloves_obj:
|
|
print("✗ ERROR: Work_gloves object not found")
|
|
raise Exception("Work_gloves object not found - script aborted")
|
|
|
|
print(f"✓ Found Work_gloves object: {work_gloves_obj.name}")
|
|
|
|
# Find the CC_Base_Body object
|
|
base_body_obj = bpy.data.objects.get('CC_Base_Body')
|
|
if not base_body_obj:
|
|
print("✗ ERROR: CC_Base_Body object not found")
|
|
raise Exception("CC_Base_Body object not found - script aborted")
|
|
|
|
print(f"✓ Found CC_Base_Body object: {base_body_obj.name}")
|
|
|
|
# Check for Main_Mask modifier
|
|
main_mask_modifier = None
|
|
for modifier in base_body_obj.modifiers:
|
|
if modifier.type == 'MASK' and modifier.name == 'Main_Mask':
|
|
main_mask_modifier = modifier
|
|
break
|
|
|
|
if not main_mask_modifier:
|
|
print("✗ ERROR: Main_Mask modifier not found on CC_Base_Body")
|
|
print("Available modifiers:")
|
|
for mod in base_body_obj.modifiers:
|
|
print(f" - {mod.name} ({mod.type})")
|
|
raise Exception("Main_Mask modifier not found - script aborted")
|
|
|
|
print(f"✓ Found Main_Mask modifier on CC_Base_Body")
|
|
|
|
# Remove any existing Gloves property to avoid duplication
|
|
if 'Gloves' in pose_bone:
|
|
del pose_bone['Gloves']
|
|
print("Removed existing Gloves property")
|
|
|
|
# Create custom property as boolean
|
|
pose_bone['Gloves'] = True # Default to gloves on
|
|
|
|
# Set up the property UI as boolean checkbox
|
|
ui_data = pose_bone.id_properties_ui('Gloves')
|
|
ui_data.update(
|
|
description="Toggle gloves visibility",
|
|
default=True
|
|
)
|
|
|
|
# Make the property overridable for linked rigs
|
|
try:
|
|
# Mark the custom property as overridable
|
|
pose_bone.property_overridable_library_set('["Gloves"]', True)
|
|
print("✓ Set Gloves property as library overridable")
|
|
except Exception as e:
|
|
print(f"Note: Could not set library override: {e}")
|
|
|
|
print("✓ Created 'Gloves' custom property with library override support")
|
|
|
|
# Set up drivers for Work_gloves object visibility
|
|
print("Setting up drivers for Work_gloves object...")
|
|
|
|
try:
|
|
# Clear any existing drivers
|
|
try:
|
|
work_gloves_obj.driver_remove('hide_render')
|
|
work_gloves_obj.driver_remove('hide_viewport')
|
|
except:
|
|
pass
|
|
|
|
# Create hide_render driver (hide when Gloves = 0, show when Gloves = 1)
|
|
driver_fcurve = work_gloves_obj.driver_add('hide_render')
|
|
driver = driver_fcurve.driver
|
|
driver.type = 'SUM'
|
|
|
|
var = driver.variables.new()
|
|
var.name = 'gloves_val'
|
|
var.type = 'SINGLE_PROP'
|
|
var.targets[0].id_type = 'OBJECT'
|
|
var.targets[0].id = armature_obj
|
|
var.targets[0].data_path = 'pose.bones["Settings"]["Gloves"]'
|
|
|
|
# Use polynomial modifier to invert: hide_render = 1 - gloves_val
|
|
mod = driver_fcurve.modifiers.new('GENERATOR')
|
|
mod.mode = 'POLYNOMIAL'
|
|
mod.poly_order = 1
|
|
mod.coefficients = (1.0, -1.0) # 1 - x
|
|
|
|
print(" ✓ Created hide_render driver for Work_gloves")
|
|
|
|
# Create hide_viewport driver (same logic)
|
|
driver_fcurve = work_gloves_obj.driver_add('hide_viewport')
|
|
driver = driver_fcurve.driver
|
|
driver.type = 'SUM'
|
|
|
|
var = driver.variables.new()
|
|
var.name = 'gloves_val'
|
|
var.type = 'SINGLE_PROP'
|
|
var.targets[0].id_type = 'OBJECT'
|
|
var.targets[0].id = armature_obj
|
|
var.targets[0].data_path = 'pose.bones["Settings"]["Gloves"]'
|
|
|
|
# Use polynomial modifier to invert: hide_viewport = 1 - gloves_val
|
|
mod = driver_fcurve.modifiers.new('GENERATOR')
|
|
mod.mode = 'POLYNOMIAL'
|
|
mod.poly_order = 1
|
|
mod.coefficients = (1.0, -1.0) # 1 - x
|
|
|
|
print(" ✓ Created hide_viewport driver for Work_gloves")
|
|
|
|
except Exception as e:
|
|
print(f" Error creating Work_gloves drivers: {e}")
|
|
|
|
# Set up drivers for Main_Mask modifier visibility
|
|
print("Setting up drivers for Main_Mask modifier...")
|
|
|
|
try:
|
|
# Clear any existing drivers
|
|
try:
|
|
main_mask_modifier.driver_remove('show_render')
|
|
main_mask_modifier.driver_remove('show_viewport')
|
|
except:
|
|
pass
|
|
|
|
# Create show_render driver (show when Gloves = 1, hide when Gloves = 0)
|
|
driver_fcurve = main_mask_modifier.driver_add('show_render')
|
|
driver = driver_fcurve.driver
|
|
driver.type = 'SUM'
|
|
|
|
var = driver.variables.new()
|
|
var.name = 'gloves_val'
|
|
var.type = 'SINGLE_PROP'
|
|
var.targets[0].id_type = 'OBJECT'
|
|
var.targets[0].id = armature_obj
|
|
var.targets[0].data_path = 'pose.bones["Settings"]["Gloves"]'
|
|
|
|
print(" ✓ Created show_render driver for Main_Mask")
|
|
|
|
# Create show_viewport driver (same logic)
|
|
driver_fcurve = main_mask_modifier.driver_add('show_viewport')
|
|
driver = driver_fcurve.driver
|
|
driver.type = 'SUM'
|
|
|
|
var = driver.variables.new()
|
|
var.name = 'gloves_val'
|
|
var.type = 'SINGLE_PROP'
|
|
var.targets[0].id_type = 'OBJECT'
|
|
var.targets[0].id = armature_obj
|
|
var.targets[0].data_path = 'pose.bones["Settings"]["Gloves"]'
|
|
|
|
print(" ✓ Created show_viewport driver for Main_Mask")
|
|
|
|
except Exception as e:
|
|
print(f" Error creating Main_Mask drivers: {e}")
|
|
|
|
print("✓ Driver setup complete")
|
|
|
|
# Test the toggle functionality
|
|
print("\n=== Testing gloves functionality ===")
|
|
|
|
# Force update to make sure drivers are working
|
|
bpy.context.view_layer.update()
|
|
bpy.context.evaluated_depsgraph_get().update()
|
|
|
|
# Initial state (should be gloves on)
|
|
print(f"Current state: Gloves = {pose_bone['Gloves']} (True=on, False=off)")
|
|
print(f" Work_gloves: hide_render={work_gloves_obj.hide_render}, hide_viewport={work_gloves_obj.hide_viewport}")
|
|
print(f" Main_Mask: show_render={main_mask_modifier.show_render}, show_viewport={main_mask_modifier.show_viewport}")
|
|
|
|
# Test toggle to False (gloves off)
|
|
print("\nToggling to False (gloves off - should hide gloves and Main_Mask, exposing hands)...")
|
|
pose_bone['Gloves'] = False
|
|
bpy.context.view_layer.update()
|
|
bpy.context.evaluated_depsgraph_get().update()
|
|
|
|
print(f"New state: Gloves = {pose_bone['Gloves']}")
|
|
print(f" Work_gloves: hide_render={work_gloves_obj.hide_render}, hide_viewport={work_gloves_obj.hide_viewport}")
|
|
print(f" Main_Mask: show_render={main_mask_modifier.show_render}, show_viewport={main_mask_modifier.show_viewport}")
|
|
|
|
# Test toggle back to True (gloves on)
|
|
print("\nToggling to True (gloves on - should show gloves and Main_Mask, hiding hands)...")
|
|
pose_bone['Gloves'] = True
|
|
bpy.context.view_layer.update()
|
|
bpy.context.evaluated_depsgraph_get().update()
|
|
|
|
print(f"Final state: Gloves = {pose_bone['Gloves']}")
|
|
print(f" Work_gloves: hide_render={work_gloves_obj.hide_render}, hide_viewport={work_gloves_obj.hide_viewport}")
|
|
print(f" Main_Mask: show_render={main_mask_modifier.show_render}, show_viewport={main_mask_modifier.show_viewport}")
|
|
|
|
print("\n✓ Mask Settings script completed successfully!")
|
|
print("The 'Gloves' property is now available on the Settings bone as a checkbox")
|
|
print("Use checkbox to toggle gloves and hand visibility")
|
|
print(" - Gloves ON (checked): Shows Work_gloves object + Main_Mask (hides hands from body)")
|
|
print(" - Gloves OFF (unchecked): Hides Work_gloves object + Main_Mask (shows hands via Hand_Mask)") |