"""
Advanced Viewport Handlers
Functions for controlling viewport display modes, overlays, and topology visualization.
"""
import bpy
import os
import tempfile
import traceback
def set_shading_mode(mode="SOLID", show_wireframe=False, show_xray=False, xray_alpha=0.5):
"""
Set the viewport shading mode and options.
Parameters:
- mode: Shading mode - "WIREFRAME", "SOLID", "MATERIAL", "RENDERED"
- show_wireframe: Show wireframe overlay on solid/material modes
- show_xray: Enable X-Ray mode for seeing through objects
- xray_alpha: X-Ray transparency (0.0-1.0)
Returns success status.
"""
try:
# Get 3D viewport
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
for space in area.spaces:
if space.type == 'VIEW_3D':
# Set shading mode
if mode in ['WIREFRAME', 'SOLID', 'MATERIAL', 'RENDERED']:
space.shading.type = mode
else:
return {"error": f"Unknown shading mode: {mode}"}
# Set wireframe overlay
space.overlay.show_wireframes = show_wireframe
# Set X-Ray
space.shading.show_xray = show_xray
space.shading.xray_alpha = xray_alpha
return {
"success": True,
"mode": mode,
"wireframe_overlay": show_wireframe,
"xray_enabled": show_xray,
"xray_alpha": xray_alpha
}
return {"error": "No 3D viewport found"}
except Exception as e:
traceback.print_exc()
return {"error": f"Failed to set shading mode: {str(e)}"}
def set_overlay_options(
show_wireframe=None,
show_face_orientation=None,
show_normals=None,
normals_length=0.1,
show_stats=None,
show_edge_seams=None,
show_edge_sharp=None
):
"""
Configure viewport overlay options for topology visualization.
Parameters:
- show_wireframe: Show wireframe overlay
- show_face_orientation: Show face orientation colors (blue=front, red=back)
- show_normals: Show vertex/face normals
- normals_length: Length of normal display lines
- show_stats: Show mesh statistics overlay
- show_edge_seams: Highlight UV seams
- show_edge_sharp: Highlight sharp edges
Returns success status with applied settings.
"""
try:
applied = {}
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
for space in area.spaces:
if space.type == 'VIEW_3D':
overlay = space.overlay
if show_wireframe is not None:
overlay.show_wireframes = show_wireframe
applied["wireframe"] = show_wireframe
if show_face_orientation is not None:
overlay.show_face_orientation = show_face_orientation
applied["face_orientation"] = show_face_orientation
if show_stats is not None:
overlay.show_stats = show_stats
applied["stats"] = show_stats
if show_edge_seams is not None:
overlay.show_edge_seams = show_edge_seams
applied["edge_seams"] = show_edge_seams
if show_edge_sharp is not None:
overlay.show_edge_sharp = show_edge_sharp
applied["edge_sharp"] = show_edge_sharp
# Normals require mesh edit mode context
if show_normals is not None:
overlay.show_vertex_normals = show_normals
overlay.show_split_normals = show_normals
overlay.show_face_normals = show_normals
overlay.normals_length = normals_length
applied["normals"] = show_normals
return {
"success": True,
"applied_settings": applied
}
return {"error": "No 3D viewport found"}
except Exception as e:
traceback.print_exc()
return {"error": f"Failed to set overlay options: {str(e)}"}
def get_topology_screenshot(
show_wireframe=True,
show_face_orientation=False,
shading_mode="SOLID",
max_size=1024
):
"""
Capture a screenshot with topology visualization enabled.
Temporarily enables wireframe/face orientation overlays,
captures the viewport, then restores original settings.
Parameters:
- show_wireframe: Show wireframe overlay in screenshot
- show_face_orientation: Show face orientation colors
- shading_mode: Shading mode for screenshot
- max_size: Maximum dimension of the screenshot
Returns path to the screenshot file.
"""
try:
# Store original settings
original_settings = {}
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
for space in area.spaces:
if space.type == 'VIEW_3D':
original_settings = {
"shading_type": space.shading.type,
"show_wireframes": space.overlay.show_wireframes,
"show_face_orientation": space.overlay.show_face_orientation
}
# Apply topology visualization settings
space.shading.type = shading_mode
space.overlay.show_wireframes = show_wireframe
space.overlay.show_face_orientation = show_face_orientation
break
break
# Create temporary file for screenshot
temp_dir = tempfile.gettempdir()
filepath = os.path.join(temp_dir, "topology_screenshot.png")
# Capture screenshot
bpy.ops.screen.screenshot(filepath=filepath)
# Restore original settings
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
for space in area.spaces:
if space.type == 'VIEW_3D':
if original_settings:
space.shading.type = original_settings["shading_type"]
space.overlay.show_wireframes = original_settings["show_wireframes"]
space.overlay.show_face_orientation = original_settings["show_face_orientation"]
break
break
# Read and encode the image
if os.path.exists(filepath):
import base64
with open(filepath, "rb") as f:
image_data = base64.b64encode(f.read()).decode('utf-8')
return {
"success": True,
"filepath": filepath,
"image_data": image_data,
"settings_used": {
"shading_mode": shading_mode,
"wireframe": show_wireframe,
"face_orientation": show_face_orientation
}
}
else:
return {"error": "Failed to save screenshot"}
except Exception as e:
traceback.print_exc()
return {"error": f"Failed to capture topology screenshot: {str(e)}"}