"""
Shading Validation Handlers
Handlers for quick viewport shading setup to validate surface quality.
"""
import bpy
import traceback
def set_matcap_shading(matcap_name=None):
"""
Set matcap shading for surface validation.
Matcap shading reveals shading artifacts, harsh lines, and surface issues.
Parameters:
- matcap_name: Optional specific matcap name (default: use current or first available)
Returns success status.
"""
try:
# Find the 3D viewport
area = None
for a in bpy.context.screen.areas:
if a.type == 'VIEW_3D':
area = a
break
if not area:
return {"error": "No 3D viewport found"}
# Get the space data
space = None
for s in area.spaces:
if s.type == 'VIEW_3D':
space = s
break
if not space:
return {"error": "No 3D viewport space found"}
# Set to SOLID mode with matcap
space.shading.type = 'SOLID'
space.shading.light = 'MATCAP'
# Disable wireframe overlay for clean shading view
space.overlay.show_wireframes = False
# Set matcap if specified
applied_matcap = None
if matcap_name:
# Try to set the specified matcap
try:
space.shading.studio_light = matcap_name
applied_matcap = matcap_name
except:
# If matcap not found, use default
applied_matcap = space.shading.studio_light
else:
applied_matcap = space.shading.studio_light
return {
"success": True,
"shading_type": "SOLID",
"light_mode": "MATCAP",
"matcap": applied_matcap,
"wireframe_overlay": False
}
except Exception as e:
traceback.print_exc()
return {"error": f"Failed to set matcap shading: {str(e)}"}
def toggle_wireframe_overlay(enabled=None):
"""
Toggle or set wireframe overlay visibility.
Parameters:
- enabled: True to show, False to hide, None to toggle
Returns new state.
"""
try:
# Find the 3D viewport
area = None
for a in bpy.context.screen.areas:
if a.type == 'VIEW_3D':
area = a
break
if not area:
return {"error": "No 3D viewport found"}
# Get the space data
space = None
for s in area.spaces:
if s.type == 'VIEW_3D':
space = s
break
if not space:
return {"error": "No 3D viewport space found"}
# Toggle or set
if enabled is None:
space.overlay.show_wireframes = not space.overlay.show_wireframes
else:
space.overlay.show_wireframes = enabled
return {
"success": True,
"wireframe_enabled": space.overlay.show_wireframes
}
except Exception as e:
traceback.print_exc()
return {"error": f"Failed to toggle wireframe overlay: {str(e)}"}
def toggle_smooth_shading(object_name=None, smooth=True):
"""
Set smooth or flat shading on mesh object.
Parameters:
- object_name: Name of object (default: active object)
- smooth: True for smooth shading, False for flat
Returns success status.
"""
try:
# Get target object
if object_name:
obj = bpy.data.objects.get(object_name)
if not obj:
return {"error": f"Object '{object_name}' not found"}
else:
obj = bpy.context.active_object
if not obj or obj.type != 'MESH':
return {"error": "No valid mesh object selected"}
# Set shading
mesh = obj.data
if smooth:
for poly in mesh.polygons:
poly.use_smooth = True
else:
for poly in mesh.polygons:
poly.use_smooth = False
mesh.update()
return {
"success": True,
"object_name": obj.name,
"smooth_shading": smooth
}
except Exception as e:
traceback.print_exc()
return {"error": f"Failed to set shading: {str(e)}"}