"""
View Control Tools
MCP tools for reliable viewport navigation.
"""
from mcp.server.fastmcp import Context
import logging
logger = logging.getLogger("BlenderMCPServer")
def set_view_axis(
ctx: Context,
blender_connection,
axis: str
) -> str:
"""
Set viewport to a standard axis view.
Parameters:
- axis: Standard view name - "FRONT", "BACK", "LEFT", "RIGHT", "TOP", "BOTTOM"
"""
try:
result = blender_connection.send_command("set_view_axis", {"axis": axis})
if "error" in result:
return f"Error: {result['error']}"
output = "View Axis Set\n"
output += "=" * 50 + "\n\n"
output += f"View aligned to: {result.get('axis', 'Unknown')}\n"
output += "\nViewport is now showing the specified axis view.\n"
return output
except Exception as e:
logger.error(f"Error setting view axis: {str(e)}")
return f"Error setting view axis: {str(e)}"
def orbit_view(
ctx: Context,
blender_connection,
yaw_delta: float = 0.0,
pitch_delta: float = 0.0,
distance_delta: float = 0.0,
target: str = None,
point: list = None
) -> str:
"""
Orbit the viewport by specified angles.
Parameters:
- yaw_delta: Horizontal rotation in degrees (positive = rotate right)
- pitch_delta: Vertical rotation in degrees (positive = rotate up)
- distance_delta: Change in view distance (positive = zoom out, negative = zoom in)
- target: Object name to orbit around (default: current view location)
- point: List of [x, y, z] coordinates to orbit around
"""
try:
params = {
"yaw_delta": yaw_delta,
"pitch_delta": pitch_delta,
"distance_delta": distance_delta
}
if target:
params["target"] = target
if point:
params["point"] = point
result = blender_connection.send_command("orbit_view", params)
if "error" in result:
return f"Error: {result['error']}"
output = "View Orbit Applied\n"
output += "=" * 50 + "\n\n"
output += f"Yaw change: {result.get('yaw_delta', 0):.1f} degrees\n"
output += f"Pitch change: {result.get('pitch_delta', 0):.1f} degrees\n"
output += f"Distance change: {result.get('distance_delta', 0):.2f}\n"
view_loc = result.get('view_location', {})
output += f"\nView center: ({view_loc.get('x', 0):.3f}, {view_loc.get('y', 0):.3f}, {view_loc.get('z', 0):.3f})\n"
output += f"View distance: {result.get('view_distance', 0):.2f}\n"
return output
except Exception as e:
logger.error(f"Error orbiting view: {str(e)}")
return f"Error orbiting view: {str(e)}"