|
| 1 | +from pydantic import BaseModel |
| 2 | +from fastmcp import Context |
| 3 | + |
| 4 | +from models import MCPResponse |
| 5 | +from registry import mcp_for_unity_resource |
| 6 | +from tools import get_unity_instance_from_context, async_send_with_unity_instance |
| 7 | +from unity_connection import async_send_command_with_retry |
| 8 | + |
| 9 | + |
| 10 | +class SelectionObjectInfo(BaseModel): |
| 11 | + """Information about a selected object.""" |
| 12 | + name: str | None = None |
| 13 | + type: str | None = None |
| 14 | + instanceID: int | None = None |
| 15 | + |
| 16 | + |
| 17 | +class SelectionGameObjectInfo(BaseModel): |
| 18 | + """Information about a selected GameObject.""" |
| 19 | + name: str | None = None |
| 20 | + instanceID: int | None = None |
| 21 | + |
| 22 | + |
| 23 | +class SelectionData(BaseModel): |
| 24 | + """Selection data fields.""" |
| 25 | + activeObject: str | None = None |
| 26 | + activeGameObject: str | None = None |
| 27 | + activeTransform: str | None = None |
| 28 | + activeInstanceID: int = 0 |
| 29 | + count: int = 0 |
| 30 | + objects: list[SelectionObjectInfo] = [] |
| 31 | + gameObjects: list[SelectionGameObjectInfo] = [] |
| 32 | + assetGUIDs: list[str] = [] |
| 33 | + |
| 34 | + |
| 35 | +class SelectionResponse(MCPResponse): |
| 36 | + """Detailed information about the current editor selection.""" |
| 37 | + data: SelectionData = SelectionData() |
| 38 | + |
| 39 | + |
| 40 | +@mcp_for_unity_resource( |
| 41 | + uri="unity://editor/selection", |
| 42 | + name="editor_selection", |
| 43 | + description="Detailed information about currently selected objects in the editor, including GameObjects, assets, and their properties." |
| 44 | +) |
| 45 | +async def get_selection(ctx: Context) -> SelectionResponse | MCPResponse: |
| 46 | + """Get detailed editor selection information.""" |
| 47 | + unity_instance = get_unity_instance_from_context(ctx) |
| 48 | + response = await async_send_with_unity_instance( |
| 49 | + async_send_command_with_retry, |
| 50 | + unity_instance, |
| 51 | + "get_selection", |
| 52 | + {} |
| 53 | + ) |
| 54 | + return SelectionResponse(**response) if isinstance(response, dict) else response |
0 commit comments