[
  {
    "path": ".gitignore",
    "content": "# Python-generated files\n__pycache__/\n*.py[oc]\nbuild/\ndist/\nwheels/\n*.egg-info\n\n# Virtual environments\n.venv\n\n# macOS\n.DS_Store\n\n# Local config secrets\nsrc/blender_mcp/config.py\n"
  },
  {
    "path": ".python-version",
    "content": "3.13.2\n"
  },
  {
    "path": "LICENSE",
    "content": "MIT License\n\nCopyright (c) 2025 Siddharth Ahuja\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE."
  },
  {
    "path": "README.md",
    "content": "\n\n# BlenderMCP - Blender Model Context Protocol Integration\n\nBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.\n\n**We have no official website. Any website you see online is unofficial and has no affiliation with this project. Use them at your own risk.**\n\n[Full tutorial](https://www.youtube.com/watch?v=lCyQ717DuzQ)\n\n### Join the Community\n\nGive feedback, get inspired, and build on top of the MCP: [Discord](https://discord.gg/z5apgR8TFU)\n\n### Supporters\n\n[CodeRabbit](https://www.coderabbit.ai/)\n\n**All supporters:**\n\n[Support this project](https://github.com/sponsors/ahujasid)\n\n## Current version(1.5.5)\n- Added Hunyuan3D support\n- View screenshots for Blender viewport to better understand the scene\n- Search and download Sketchfab models\n- Support for Poly Haven assets through their API\n- Support to generate 3D models using Hyper3D Rodin\n- Run Blender MCP on a remote host\n- Telemetry for tools executed (completely anonymous)\n\n### Installating a new version (existing users)\n- For newcomers, you can go straight to Installation. For existing users, see the points below\n- Download the latest addon.py file and replace the older one, then add it to Blender\n- Delete the MCP server from Claude and add it back again, and you should be good to go!\n\n\n## Features\n\n- **Two-way communication**: Connect Claude AI to Blender through a socket-based server\n- **Object manipulation**: Create, modify, and delete 3D objects in Blender\n- **Material control**: Apply and modify materials and colors\n- **Scene inspection**: Get detailed information about the current Blender scene\n- **Code execution**: Run arbitrary Python code in Blender from Claude\n\n## Components\n\nThe system consists of two main components:\n\n1. **Blender Addon (`addon.py`)**: A Blender addon that creates a socket server within Blender to receive and execute commands\n2. **MCP Server (`src/blender_mcp/server.py`)**: A Python server that implements the Model Context Protocol and connects to the Blender addon\n\n## Installation\n\n\n### Prerequisites\n\n- Blender 3.0 or newer\n- Python 3.10 or newer\n- uv package manager: \n\n**If you're on Mac, please install uv as**\n```bash\nbrew install uv\n```\n**On Windows**\n```powershell\npowershell -c \"irm https://astral.sh/uv/install.ps1 | iex\" \n```\nand then add uv to the user path in Windows (you may need to restart Claude Desktop after):\n```powershell\n$localBin = \"$env:USERPROFILE\\.local\\bin\"\n$userPath = [Environment]::GetEnvironmentVariable(\"Path\", \"User\")\n[Environment]::SetEnvironmentVariable(\"Path\", \"$userPath;$localBin\", \"User\")\n```\n\nOtherwise installation instructions are on their website: [Install uv](https://docs.astral.sh/uv/getting-started/installation/)\n\n**⚠️ Do not proceed before installing UV**\n\n### Environment Variables\n\nThe following environment variables can be used to configure the Blender connection:\n\n- `BLENDER_HOST`: Host address for Blender socket server (default: \"localhost\")\n- `BLENDER_PORT`: Port number for Blender socket server (default: 9876)\n\nExample:\n```bash\nexport BLENDER_HOST='host.docker.internal'\nexport BLENDER_PORT=9876\n```\n\n### Claude for Desktop Integration\n\n[Watch the setup instruction video](https://www.youtube.com/watch?v=neoK_WMq92g) (Assuming you have already installed uv)\n\nGo to Claude > Settings > Developer > Edit Config > claude_desktop_config.json to include the following:\n\n```json\n{\n    \"mcpServers\": {\n        \"blender\": {\n            \"command\": \"uvx\",\n            \"args\": [\n                \"blender-mcp\"\n            ]\n        }\n    }\n}\n```\n<details>\n<summary>Claude Code</summary>\n\nUse the Claude Code CLI to add the blender MCP server:\n\n```bash\nclaude mcp add blender uvx blender-mcp\n```\n</details>\n\n### Cursor integration\n\n[![Install MCP Server](https://cursor.com/deeplink/mcp-install-dark.svg)](https://cursor.com/link/mcp%2Finstall?name=blender&config=eyJjb21tYW5kIjoidXZ4IGJsZW5kZXItbWNwIn0%3D)\n\nFor Mac users, go to Settings > MCP and paste the following \n\n- To use as a global server, use \"add new global MCP server\" button and paste\n- To use as a project specific server, create `.cursor/mcp.json` in the root of the project and paste\n\n\n```json\n{\n    \"mcpServers\": {\n        \"blender\": {\n            \"command\": \"uvx\",\n            \"args\": [\n                \"blender-mcp\"\n            ]\n        }\n    }\n}\n```\n\nFor Windows users, go to Settings > MCP > Add Server, add a new server with the following settings:\n\n```json\n{\n    \"mcpServers\": {\n        \"blender\": {\n            \"command\": \"cmd\",\n            \"args\": [\n                \"/c\",\n                \"uvx\",\n                \"blender-mcp\"\n            ]\n        }\n    }\n}\n```\n\n[Cursor setup video](https://www.youtube.com/watch?v=wgWsJshecac)\n\n**⚠️ Only run one instance of the MCP server (either on Cursor or Claude Desktop), not both**\n\n### Visual Studio Code Integration\n\n_Prerequisites_: Make sure you have [Visual Studio Code](https://code.visualstudio.com/) installed before proceeding.\n\n[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install_blender--mcp_server-0098FF?style=flat-square&logo=visualstudiocode&logoColor=ffffff)](vscode:mcp/install?%7B%22name%22%3A%22blender-mcp%22%2C%22type%22%3A%22stdio%22%2C%22command%22%3A%22uvx%22%2C%22args%22%3A%5B%22blender-mcp%22%5D%7D)\n\n### Installing the Blender Addon\n\n1. Download the `addon.py` file from this repo\n1. Open Blender\n2. Go to Edit > Preferences > Add-ons\n3. Click \"Install...\" and select the `addon.py` file\n4. Enable the addon by checking the box next to \"Interface: Blender MCP\"\n\n\n## Usage\n\n### Starting the Connection\n![BlenderMCP in the sidebar](assets/addon-instructions.png)\n\n1. In Blender, go to the 3D View sidebar (press N if not visible)\n2. Find the \"BlenderMCP\" tab\n3. Turn on the Poly Haven checkbox if you want assets from their API (optional)\n4. Click \"Connect to Claude\"\n5. Make sure the MCP server is running in your terminal\n\n### Using with Claude\n\nOnce the config file has been set on Claude, and the addon is running on Blender, you will see a hammer icon with tools for the Blender MCP.\n\n![BlenderMCP in the sidebar](assets/hammer-icon.png)\n\n#### Capabilities\n\n- Get scene and object information \n- Create, delete and modify shapes\n- Apply or create materials for objects\n- Execute any Python code in Blender\n- Download the right models, assets and HDRIs through [Poly Haven](https://polyhaven.com/)\n- AI generated 3D models through [Hyper3D Rodin](https://hyper3d.ai/)\n\n\n### Example Commands\n\nHere are some examples of what you can ask Claude to do:\n\n- \"Create a low poly scene in a dungeon, with a dragon guarding a pot of gold\" [Demo](https://www.youtube.com/watch?v=DqgKuLYUv00)\n- \"Create a beach vibe using HDRIs, textures, and models like rocks and vegetation from Poly Haven\" [Demo](https://www.youtube.com/watch?v=I29rn92gkC4)\n- Give a reference image, and create a Blender scene out of it [Demo](https://www.youtube.com/watch?v=FDRb03XPiRo)\n- \"Generate a 3D model of a garden gnome through Hyper3D\"\n- \"Get information about the current scene, and make a threejs sketch from it\" [Demo](https://www.youtube.com/watch?v=jxbNI5L7AH8)\n- \"Make this car red and metallic\" \n- \"Create a sphere and place it above the cube\"\n- \"Make the lighting like a studio\"\n- \"Point the camera at the scene, and make it isometric\"\n\n## Hyper3D integration\n\nHyper3D's free trial key allows you to generate a limited number of models per day. If the daily limit is reached, you can wait for the next day's reset or obtain your own key from hyper3d.ai and fal.ai.\n\n## Troubleshooting\n\n- **Connection issues**: Make sure the Blender addon server is running, and the MCP server is configured on Claude, DO NOT run the uvx command in the terminal. Sometimes, the first command won't go through but after that it starts working.\n- **Timeout errors**: Try simplifying your requests or breaking them into smaller steps\n- **Poly Haven integration**: Claude is sometimes erratic with its behaviour\n- **Have you tried turning it off and on again?**: If you're still having connection errors, try restarting both Claude and the Blender server\n\n\n## Technical Details\n\n### Communication Protocol\n\nThe system uses a simple JSON-based protocol over TCP sockets:\n\n- **Commands** are sent as JSON objects with a `type` and optional `params`\n- **Responses** are JSON objects with a `status` and `result` or `message`\n\n## Limitations & Security Considerations\n\n- The `execute_blender_code` tool allows running arbitrary Python code in Blender, which can be powerful but potentially dangerous. Use with caution in production environments. ALWAYS save your work before using it.\n- Poly Haven requires downloading models, textures, and HDRI images. If you do not want to use it, please turn it off in the checkbox in Blender. \n- Complex operations might need to be broken down into smaller steps\n\n\n#### Telemetry Control\n\nBlenderMCP collects anonymous usage data to help improve the tool. You can control telemetry in two ways:\n\n1. **In Blender**: Go to Edit > Preferences > Add-ons > Blender MCP and uncheck the telemetry consent checkbox\n   - With consent (checked): Collects anonymized prompts, code snippets, and screenshots\n   - Without consent (unchecked): Only collects minimal anonymous usage data (tool names, success/failure, duration)\n\n2. **Environment Variable**: Completely disable all telemetry by running:\n```bash\nDISABLE_TELEMETRY=true uvx blender-mcp\n```\n\nOr add it to your MCP config:\n```json\n{\n    \"mcpServers\": {\n        \"blender\": {\n            \"command\": \"uvx\",\n            \"args\": [\"blender-mcp\"],\n            \"env\": {\n                \"DISABLE_TELEMETRY\": \"true\"\n            }\n        }\n    }\n}\n```\n\nAll telemetry data is fully anonymized and used solely to improve BlenderMCP.\n\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## Disclaimer\n\nThis is a third-party integration and not made by Blender. Made by [Siddharth](https://x.com/sidahuj)\n"
  },
  {
    "path": "TERMS_AND_CONDITIONS.md",
    "content": "# Blender MCP - Terms of Use and Privacy Policy\n\n**Last Updated: January 2025**\n\n---\n\n## 1. About This Project\n\nBlender MCP is a free, open-source project maintained by Siddharth Ahuja (\"I,\" \"me,\" \"my\"). This document describes how I collect and may use data when you use Blender MCP.\n\nBy using Blender MCP, you agree to these terms. If you do not agree, please do not use the software.\n\n---\n\n## 2. Data I Collect\n\nWhen you use Blender MCP, I may collect:\n\n- **Prompts and text inputs** you provide to the AI\n- **Generated code** produced in response to your prompts\n- **Scene metadata** such as object names, modifier settings, and configurations\n- **Basic usage data** including timestamps and feature usage\n\nI do **not** collect:\n\n- Screenshots or images of your viewport\n- Your Blender files or 3D models\n- Personal files unrelated to your Blender session\n- Passwords or financial information\n- Data from other applications on your system\n\n---\n\n## 3. How I May Use Your Data\n\nI am currently collecting data for potential future use. This data may be used to:\n\n- **Train AI models** for 3D creation and Blender automation\n- **Improve Blender MCP** based on real-world usage\n- **Conduct research** on AI-assisted creative workflows\n- **Share datasets** with the research community (in anonymized or aggregated form)\n\nYour data may be:\n\n- Stored indefinitely\n- Used to train machine learning models in the future\n- Released as part of an open dataset (anonymized)\n\n---\n\n## 4. Data Sharing\n\nI may share collected data with:\n\n- **The open-source/research community** as part of public datasets\n- **Collaborators** working on AI or Blender-related research\n- **Legal authorities** if required by law\n\nI do not sell your data.\n\n---\n\n## 5. Your Rights\n\nYou may:\n\n- **Request access** to the data I've collected from your usage\n- **Request deletion** of your data\n- **Opt out of telemetry** by unchecking the telemetry option in the Blender MCP addon preferences. When disabled, no data is collected, and you can continue using the software normally.\n\nTo exercise these rights, contact me at ahujasid@gmail.com.\n\n**Important:** If data has been used to train an AI model or included in a public dataset, it may not be possible to fully remove it.\n\n---\n\n## 6. Data Retention\n\n- Data may be retained indefinitely\n- I will make reasonable efforts to honor deletion requests for unprocessed data\n- Anonymized or aggregated data may be retained and shared permanently\n\n---\n\n## 7. Security\n\nI take reasonable steps to protect collected data, but this is a solo open-source project, not a company with enterprise security infrastructure. I cannot guarantee absolute security.\n\n---\n\n## 8. Children\n\nBlender MCP is not intended for users under 16. I do not knowingly collect data from children.\n\n---\n\n## 9. International Users\n\nYour data may be stored and processed in any country. By using Blender MCP, you consent to international data transfers.\n\n---\n\n## 10. Intellectual Property\n\n### Your Content\n\nYou retain ownership of your original creative work. By using Blender MCP with telemetry enabled, you grant me a **worldwide, royalty-free, perpetual license** to use:\n\n- Prompts you submit\n- Images/screenshots of your Blender viewport\n- Code generated in response to your prompts\n- Scene metadata captured during use\n\nThis license is for AI training, research, open datasets, and improving the project.\n\n**Note:** When telemetry is disabled, no license is granted as no data is collected.\n\n### AI-Generated Content\n\nYou may use AI-generated code however you like, but it's provided \"as is\" with no guarantees.\n\n### Blender MCP\n\nThe Blender MCP source code is open source under its stated license. These terms apply only to data collection.\n\n---\n\n## 11. No Warranty\n\nBLENDER MCP IS PROVIDED \"AS IS\" WITHOUT ANY WARRANTIES.\n\nI do not guarantee that:\n\n- The software will work correctly\n- AI-generated code will be safe or functional\n- Your data will be secure\n\n**You are responsible for reviewing any AI-generated code before using it.**\n\n---\n\n## 12. Limitation of Liability\n\nTO THE MAXIMUM EXTENT PERMITTED BY LAW, I AM NOT LIABLE FOR ANY DAMAGES ARISING FROM YOUR USE OF BLENDER MCP.\n\nThis is a free, open-source project maintained in my spare time. Use at your own risk.\n\n---\n\n## 13. Changes\n\nI may update these terms at any time. Continued use of Blender MCP after changes means you accept the new terms.\n\n---\n\n## 14. Contact\n\nQuestions or requests? Email me at ahujasid@gmail.com.\n\n---\n\n## 15. Consent\n\nBy using Blender MCP with telemetry enabled, you acknowledge that:\n\n1. You have read and understood these terms\n2. You consent to the collection of prompts, generated code, images/screenshots, and scene metadata\n3. You understand this data may be used to train AI models or released as part of open datasets\n4. You understand that once data is used for training or released publicly, it cannot be fully deleted\n5. You are at least 16 years old\n6. You can disable telemetry at any time in the addon preferences\n\n---\n\n*Blender MCP is an independent project and is not affiliated with the Blender Foundation.*\n\n"
  },
  {
    "path": "addon.py",
    "content": "# Code created by Siddharth Ahuja: www.github.com/ahujasid © 2025\n\nimport re\nimport bpy\nimport mathutils\nimport json\nimport threading\nimport socket\nimport time\nimport requests\nimport tempfile\nimport traceback\nimport os\nimport shutil\nimport zipfile\nfrom bpy.props import IntProperty, BoolProperty\nimport io\nfrom datetime import datetime\nimport hashlib, hmac, base64\nimport os.path as osp\nfrom contextlib import redirect_stdout, suppress\n\nbl_info = {\n    \"name\": \"Blender MCP\",\n    \"author\": \"BlenderMCP\",\n    \"version\": (1, 2),\n    \"blender\": (3, 0, 0),\n    \"location\": \"View3D > Sidebar > BlenderMCP\",\n    \"description\": \"Connect Blender to Claude via MCP\",\n    \"category\": \"Interface\",\n}\n\nRODIN_FREE_TRIAL_KEY = \"k9TcfFoEhNd9cCPP2guHAHHHkctZHIRhZDywZ1euGUXwihbYLpOjQhofby80NJez\"\n\n# Add User-Agent as required by Poly Haven API\nREQ_HEADERS = requests.utils.default_headers()\nREQ_HEADERS.update({\"User-Agent\": \"blender-mcp\"})\n\nclass BlenderMCPServer:\n    def __init__(self, host='localhost', port=9876):\n        self.host = host\n        self.port = port\n        self.running = False\n        self.socket = None\n        self.server_thread = None\n\n    def start(self):\n        if self.running:\n            print(\"Server is already running\")\n            return\n\n        self.running = True\n\n        try:\n            # Create socket\n            self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n            self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n            self.socket.bind((self.host, self.port))\n            self.socket.listen(1)\n\n            # Start server thread\n            self.server_thread = threading.Thread(target=self._server_loop)\n            self.server_thread.daemon = True\n            self.server_thread.start()\n\n            print(f\"BlenderMCP server started on {self.host}:{self.port}\")\n        except Exception as e:\n            print(f\"Failed to start server: {str(e)}\")\n            self.stop()\n\n    def stop(self):\n        self.running = False\n\n        # Close socket\n        if self.socket:\n            try:\n                self.socket.close()\n            except:\n                pass\n            self.socket = None\n\n        # Wait for thread to finish\n        if self.server_thread:\n            try:\n                if self.server_thread.is_alive():\n                    self.server_thread.join(timeout=1.0)\n            except:\n                pass\n            self.server_thread = None\n\n        print(\"BlenderMCP server stopped\")\n\n    def _server_loop(self):\n        \"\"\"Main server loop in a separate thread\"\"\"\n        print(\"Server thread started\")\n        self.socket.settimeout(1.0)  # Timeout to allow for stopping\n\n        while self.running:\n            try:\n                # Accept new connection\n                try:\n                    client, address = self.socket.accept()\n                    print(f\"Connected to client: {address}\")\n\n                    # Handle client in a separate thread\n                    client_thread = threading.Thread(\n                        target=self._handle_client,\n                        args=(client,)\n                    )\n                    client_thread.daemon = True\n                    client_thread.start()\n                except socket.timeout:\n                    # Just check running condition\n                    continue\n                except Exception as e:\n                    print(f\"Error accepting connection: {str(e)}\")\n                    time.sleep(0.5)\n            except Exception as e:\n                print(f\"Error in server loop: {str(e)}\")\n                if not self.running:\n                    break\n                time.sleep(0.5)\n\n        print(\"Server thread stopped\")\n\n    def _handle_client(self, client):\n        \"\"\"Handle connected client\"\"\"\n        print(\"Client handler started\")\n        client.settimeout(None)  # No timeout\n        buffer = b''\n\n        try:\n            while self.running:\n                # Receive data\n                try:\n                    data = client.recv(8192)\n                    if not data:\n                        print(\"Client disconnected\")\n                        break\n\n                    buffer += data\n                    try:\n                        # Try to parse command\n                        command = json.loads(buffer.decode('utf-8'))\n                        buffer = b''\n\n                        # Execute command in Blender's main thread\n                        def execute_wrapper():\n                            try:\n                                response = self.execute_command(command)\n                                response_json = json.dumps(response)\n                                try:\n                                    client.sendall(response_json.encode('utf-8'))\n                                except:\n                                    print(\"Failed to send response - client disconnected\")\n                            except Exception as e:\n                                print(f\"Error executing command: {str(e)}\")\n                                traceback.print_exc()\n                                try:\n                                    error_response = {\n                                        \"status\": \"error\",\n                                        \"message\": str(e)\n                                    }\n                                    client.sendall(json.dumps(error_response).encode('utf-8'))\n                                except:\n                                    pass\n                            return None\n\n                        # Schedule execution in main thread\n                        bpy.app.timers.register(execute_wrapper, first_interval=0.0)\n                    except json.JSONDecodeError:\n                        # Incomplete data, wait for more\n                        pass\n                except Exception as e:\n                    print(f\"Error receiving data: {str(e)}\")\n                    break\n        except Exception as e:\n            print(f\"Error in client handler: {str(e)}\")\n        finally:\n            try:\n                client.close()\n            except:\n                pass\n            print(\"Client handler stopped\")\n\n    def execute_command(self, command):\n        \"\"\"Execute a command in the main Blender thread\"\"\"\n        try:\n            return self._execute_command_internal(command)\n\n        except Exception as e:\n            print(f\"Error executing command: {str(e)}\")\n            traceback.print_exc()\n            return {\"status\": \"error\", \"message\": str(e)}\n\n    def _execute_command_internal(self, command):\n        \"\"\"Internal command execution with proper context\"\"\"\n        cmd_type = command.get(\"type\")\n        params = command.get(\"params\", {})\n\n        # Add a handler for checking PolyHaven status\n        if cmd_type == \"get_polyhaven_status\":\n            return {\"status\": \"success\", \"result\": self.get_polyhaven_status()}\n\n        # Base handlers that are always available\n        handlers = {\n            \"get_scene_info\": self.get_scene_info,\n            \"get_object_info\": self.get_object_info,\n            \"get_viewport_screenshot\": self.get_viewport_screenshot,\n            \"execute_code\": self.execute_code,\n            \"get_telemetry_consent\": self.get_telemetry_consent,\n            \"get_polyhaven_status\": self.get_polyhaven_status,\n            \"get_hyper3d_status\": self.get_hyper3d_status,\n            \"get_sketchfab_status\": self.get_sketchfab_status,\n            \"get_hunyuan3d_status\": self.get_hunyuan3d_status,\n        }\n\n        # Add Polyhaven handlers only if enabled\n        if bpy.context.scene.blendermcp_use_polyhaven:\n            polyhaven_handlers = {\n                \"get_polyhaven_categories\": self.get_polyhaven_categories,\n                \"search_polyhaven_assets\": self.search_polyhaven_assets,\n                \"download_polyhaven_asset\": self.download_polyhaven_asset,\n                \"set_texture\": self.set_texture,\n            }\n            handlers.update(polyhaven_handlers)\n\n        # Add Hyper3d handlers only if enabled\n        if bpy.context.scene.blendermcp_use_hyper3d:\n            polyhaven_handlers = {\n                \"create_rodin_job\": self.create_rodin_job,\n                \"poll_rodin_job_status\": self.poll_rodin_job_status,\n                \"import_generated_asset\": self.import_generated_asset,\n            }\n            handlers.update(polyhaven_handlers)\n\n        # Add Sketchfab handlers only if enabled\n        if bpy.context.scene.blendermcp_use_sketchfab:\n            sketchfab_handlers = {\n                \"search_sketchfab_models\": self.search_sketchfab_models,\n                \"get_sketchfab_model_preview\": self.get_sketchfab_model_preview,\n                \"download_sketchfab_model\": self.download_sketchfab_model,\n            }\n            handlers.update(sketchfab_handlers)\n        \n        # Add Hunyuan3d handlers only if enabled\n        if bpy.context.scene.blendermcp_use_hunyuan3d:\n            hunyuan_handlers = {\n                \"create_hunyuan_job\": self.create_hunyuan_job,\n                \"poll_hunyuan_job_status\": self.poll_hunyuan_job_status,\n                \"import_generated_asset_hunyuan\": self.import_generated_asset_hunyuan\n            }\n            handlers.update(hunyuan_handlers)\n\n        handler = handlers.get(cmd_type)\n        if handler:\n            try:\n                print(f\"Executing handler for {cmd_type}\")\n                result = handler(**params)\n                print(f\"Handler execution complete\")\n                return {\"status\": \"success\", \"result\": result}\n            except Exception as e:\n                print(f\"Error in handler: {str(e)}\")\n                traceback.print_exc()\n                return {\"status\": \"error\", \"message\": str(e)}\n        else:\n            return {\"status\": \"error\", \"message\": f\"Unknown command type: {cmd_type}\"}\n\n\n\n    def get_scene_info(self):\n        \"\"\"Get information about the current Blender scene\"\"\"\n        try:\n            print(\"Getting scene info...\")\n            # Simplify the scene info to reduce data size\n            scene_info = {\n                \"name\": bpy.context.scene.name,\n                \"object_count\": len(bpy.context.scene.objects),\n                \"objects\": [],\n                \"materials_count\": len(bpy.data.materials),\n            }\n\n            # Collect minimal object information (limit to first 10 objects)\n            for i, obj in enumerate(bpy.context.scene.objects):\n                if i >= 10:  # Reduced from 20 to 10\n                    break\n\n                obj_info = {\n                    \"name\": obj.name,\n                    \"type\": obj.type,\n                    # Only include basic location data\n                    \"location\": [round(float(obj.location.x), 2),\n                                round(float(obj.location.y), 2),\n                                round(float(obj.location.z), 2)],\n                }\n                scene_info[\"objects\"].append(obj_info)\n\n            print(f\"Scene info collected: {len(scene_info['objects'])} objects\")\n            return scene_info\n        except Exception as e:\n            print(f\"Error in get_scene_info: {str(e)}\")\n            traceback.print_exc()\n            return {\"error\": str(e)}\n\n    @staticmethod\n    def _get_aabb(obj):\n        \"\"\" Returns the world-space axis-aligned bounding box (AABB) of an object. \"\"\"\n        if obj.type != 'MESH':\n            raise TypeError(\"Object must be a mesh\")\n\n        # Get the bounding box corners in local space\n        local_bbox_corners = [mathutils.Vector(corner) for corner in obj.bound_box]\n\n        # Convert to world coordinates\n        world_bbox_corners = [obj.matrix_world @ corner for corner in local_bbox_corners]\n\n        # Compute axis-aligned min/max coordinates\n        min_corner = mathutils.Vector(map(min, zip(*world_bbox_corners)))\n        max_corner = mathutils.Vector(map(max, zip(*world_bbox_corners)))\n\n        return [\n            [*min_corner], [*max_corner]\n        ]\n\n\n\n    def get_object_info(self, name):\n        \"\"\"Get detailed information about a specific object\"\"\"\n        obj = bpy.data.objects.get(name)\n        if not obj:\n            raise ValueError(f\"Object not found: {name}\")\n\n        # Basic object info\n        obj_info = {\n            \"name\": obj.name,\n            \"type\": obj.type,\n            \"location\": [obj.location.x, obj.location.y, obj.location.z],\n            \"rotation\": [obj.rotation_euler.x, obj.rotation_euler.y, obj.rotation_euler.z],\n            \"scale\": [obj.scale.x, obj.scale.y, obj.scale.z],\n            \"visible\": obj.visible_get(),\n            \"materials\": [],\n        }\n\n        if obj.type == \"MESH\":\n            bounding_box = self._get_aabb(obj)\n            obj_info[\"world_bounding_box\"] = bounding_box\n\n        # Add material slots\n        for slot in obj.material_slots:\n            if slot.material:\n                obj_info[\"materials\"].append(slot.material.name)\n\n        # Add mesh data if applicable\n        if obj.type == 'MESH' and obj.data:\n            mesh = obj.data\n            obj_info[\"mesh\"] = {\n                \"vertices\": len(mesh.vertices),\n                \"edges\": len(mesh.edges),\n                \"polygons\": len(mesh.polygons),\n            }\n\n        return obj_info\n\n    def get_viewport_screenshot(self, max_size=800, filepath=None, format=\"png\"):\n        \"\"\"\n        Capture a screenshot of the current 3D viewport and save it to the specified path.\n\n        Parameters:\n        - max_size: Maximum size in pixels for the largest dimension of the image\n        - filepath: Path where to save the screenshot file\n        - format: Image format (png, jpg, etc.)\n\n        Returns success/error status\n        \"\"\"\n        try:\n            if not filepath:\n                return {\"error\": \"No filepath provided\"}\n\n            # Find the active 3D viewport\n            area = None\n            for a in bpy.context.screen.areas:\n                if a.type == 'VIEW_3D':\n                    area = a\n                    break\n\n            if not area:\n                return {\"error\": \"No 3D viewport found\"}\n\n            # Take screenshot with proper context override\n            with bpy.context.temp_override(area=area):\n                bpy.ops.screen.screenshot_area(filepath=filepath)\n\n            # Load and resize if needed\n            img = bpy.data.images.load(filepath)\n            width, height = img.size\n\n            if max(width, height) > max_size:\n                scale = max_size / max(width, height)\n                new_width = int(width * scale)\n                new_height = int(height * scale)\n                img.scale(new_width, new_height)\n\n                # Set format and save\n                img.file_format = format.upper()\n                img.save()\n                width, height = new_width, new_height\n\n            # Cleanup Blender image data\n            bpy.data.images.remove(img)\n\n            return {\n                \"success\": True,\n                \"width\": width,\n                \"height\": height,\n                \"filepath\": filepath\n            }\n\n        except Exception as e:\n            return {\"error\": str(e)}\n\n    def execute_code(self, code):\n        \"\"\"Execute arbitrary Blender Python code\"\"\"\n        # This is powerful but potentially dangerous - use with caution\n        try:\n            # Create a local namespace for execution\n            namespace = {\"bpy\": bpy}\n\n            # Capture stdout during execution, and return it as result\n            capture_buffer = io.StringIO()\n            with redirect_stdout(capture_buffer):\n                exec(code, namespace)\n\n            captured_output = capture_buffer.getvalue()\n            return {\"executed\": True, \"result\": captured_output}\n        except Exception as e:\n            raise Exception(f\"Code execution error: {str(e)}\")\n\n\n\n    def get_polyhaven_categories(self, asset_type):\n        \"\"\"Get categories for a specific asset type from Polyhaven\"\"\"\n        try:\n            if asset_type not in [\"hdris\", \"textures\", \"models\", \"all\"]:\n                return {\"error\": f\"Invalid asset type: {asset_type}. Must be one of: hdris, textures, models, all\"}\n\n            response = requests.get(f\"https://api.polyhaven.com/categories/{asset_type}\", headers=REQ_HEADERS)\n            if response.status_code == 200:\n                return {\"categories\": response.json()}\n            else:\n                return {\"error\": f\"API request failed with status code {response.status_code}\"}\n        except Exception as e:\n            return {\"error\": str(e)}\n\n    def search_polyhaven_assets(self, asset_type=None, categories=None):\n        \"\"\"Search for assets from Polyhaven with optional filtering\"\"\"\n        try:\n            url = \"https://api.polyhaven.com/assets\"\n            params = {}\n\n            if asset_type and asset_type != \"all\":\n                if asset_type not in [\"hdris\", \"textures\", \"models\"]:\n                    return {\"error\": f\"Invalid asset type: {asset_type}. Must be one of: hdris, textures, models, all\"}\n                params[\"type\"] = asset_type\n\n            if categories:\n                params[\"categories\"] = categories\n\n            response = requests.get(url, params=params, headers=REQ_HEADERS)\n            if response.status_code == 200:\n                # Limit the response size to avoid overwhelming Blender\n                assets = response.json()\n                # Return only the first 20 assets to keep response size manageable\n                limited_assets = {}\n                for i, (key, value) in enumerate(assets.items()):\n                    if i >= 20:  # Limit to 20 assets\n                        break\n                    limited_assets[key] = value\n\n                return {\"assets\": limited_assets, \"total_count\": len(assets), \"returned_count\": len(limited_assets)}\n            else:\n                return {\"error\": f\"API request failed with status code {response.status_code}\"}\n        except Exception as e:\n            return {\"error\": str(e)}\n\n    def download_polyhaven_asset(self, asset_id, asset_type, resolution=\"1k\", file_format=None):\n        try:\n            # First get the files information\n            files_response = requests.get(f\"https://api.polyhaven.com/files/{asset_id}\", headers=REQ_HEADERS)\n            if files_response.status_code != 200:\n                return {\"error\": f\"Failed to get asset files: {files_response.status_code}\"}\n\n            files_data = files_response.json()\n\n            # Handle different asset types\n            if asset_type == \"hdris\":\n                # For HDRIs, download the .hdr or .exr file\n                if not file_format:\n                    file_format = \"hdr\"  # Default format for HDRIs\n\n                if \"hdri\" in files_data and resolution in files_data[\"hdri\"] and file_format in files_data[\"hdri\"][resolution]:\n                    file_info = files_data[\"hdri\"][resolution][file_format]\n                    file_url = file_info[\"url\"]\n\n                    # For HDRIs, we need to save to a temporary file first\n                    # since Blender can't properly load HDR data directly from memory\n                    with tempfile.NamedTemporaryFile(suffix=f\".{file_format}\", delete=False) as tmp_file:\n                        # Download the file\n                        response = requests.get(file_url, headers=REQ_HEADERS)\n                        if response.status_code != 200:\n                            return {\"error\": f\"Failed to download HDRI: {response.status_code}\"}\n\n                        tmp_file.write(response.content)\n                        tmp_path = tmp_file.name\n\n                    try:\n                        # Create a new world if none exists\n                        if not bpy.data.worlds:\n                            bpy.data.worlds.new(\"World\")\n\n                        world = bpy.data.worlds[0]\n                        world.use_nodes = True\n                        node_tree = world.node_tree\n\n                        # Clear existing nodes\n                        for node in node_tree.nodes:\n                            node_tree.nodes.remove(node)\n\n                        # Create nodes\n                        tex_coord = node_tree.nodes.new(type='ShaderNodeTexCoord')\n                        tex_coord.location = (-800, 0)\n\n                        mapping = node_tree.nodes.new(type='ShaderNodeMapping')\n                        mapping.location = (-600, 0)\n\n                        # Load the image from the temporary file\n                        env_tex = node_tree.nodes.new(type='ShaderNodeTexEnvironment')\n                        env_tex.location = (-400, 0)\n                        env_tex.image = bpy.data.images.load(tmp_path)\n\n                        # Use a color space that exists in all Blender versions\n                        if file_format.lower() == 'exr':\n                            # Try to use Linear color space for EXR files\n                            try:\n                                env_tex.image.colorspace_settings.name = 'Linear'\n                            except:\n                                # Fallback to Non-Color if Linear isn't available\n                                env_tex.image.colorspace_settings.name = 'Non-Color'\n                        else:  # hdr\n                            # For HDR files, try these options in order\n                            for color_space in ['Linear', 'Linear Rec.709', 'Non-Color']:\n                                try:\n                                    env_tex.image.colorspace_settings.name = color_space\n                                    break  # Stop if we successfully set a color space\n                                except:\n                                    continue\n\n                        background = node_tree.nodes.new(type='ShaderNodeBackground')\n                        background.location = (-200, 0)\n\n                        output = node_tree.nodes.new(type='ShaderNodeOutputWorld')\n                        output.location = (0, 0)\n\n                        # Connect nodes\n                        node_tree.links.new(tex_coord.outputs['Generated'], mapping.inputs['Vector'])\n                        node_tree.links.new(mapping.outputs['Vector'], env_tex.inputs['Vector'])\n                        node_tree.links.new(env_tex.outputs['Color'], background.inputs['Color'])\n                        node_tree.links.new(background.outputs['Background'], output.inputs['Surface'])\n\n                        # Set as active world\n                        bpy.context.scene.world = world\n\n                        # Clean up temporary file\n                        try:\n                            tempfile._cleanup()  # This will clean up all temporary files\n                        except:\n                            pass\n\n                        return {\n                            \"success\": True,\n                            \"message\": f\"HDRI {asset_id} imported successfully\",\n                            \"image_name\": env_tex.image.name\n                        }\n                    except Exception as e:\n                        return {\"error\": f\"Failed to set up HDRI in Blender: {str(e)}\"}\n                else:\n                    return {\"error\": f\"Requested resolution or format not available for this HDRI\"}\n\n            elif asset_type == \"textures\":\n                if not file_format:\n                    file_format = \"jpg\"  # Default format for textures\n\n                downloaded_maps = {}\n\n                try:\n                    for map_type in files_data:\n                        if map_type not in [\"blend\", \"gltf\"]:  # Skip non-texture files\n                            if resolution in files_data[map_type] and file_format in files_data[map_type][resolution]:\n                                file_info = files_data[map_type][resolution][file_format]\n                                file_url = file_info[\"url\"]\n\n                                # Use NamedTemporaryFile like we do for HDRIs\n                                with tempfile.NamedTemporaryFile(suffix=f\".{file_format}\", delete=False) as tmp_file:\n                                    # Download the file\n                                    response = requests.get(file_url, headers=REQ_HEADERS)\n                                    if response.status_code == 200:\n                                        tmp_file.write(response.content)\n                                        tmp_path = tmp_file.name\n\n                                        # Load image from temporary file\n                                        image = bpy.data.images.load(tmp_path)\n                                        image.name = f\"{asset_id}_{map_type}.{file_format}\"\n\n                                        # Pack the image into .blend file\n                                        image.pack()\n\n                                        # Set color space based on map type\n                                        if map_type in ['color', 'diffuse', 'albedo']:\n                                            try:\n                                                image.colorspace_settings.name = 'sRGB'\n                                            except:\n                                                pass\n                                        else:\n                                            try:\n                                                image.colorspace_settings.name = 'Non-Color'\n                                            except:\n                                                pass\n\n                                        downloaded_maps[map_type] = image\n\n                                        # Clean up temporary file\n                                        try:\n                                            os.unlink(tmp_path)\n                                        except:\n                                            pass\n\n                    if not downloaded_maps:\n                        return {\"error\": f\"No texture maps found for the requested resolution and format\"}\n\n                    # Create a new material with the downloaded textures\n                    mat = bpy.data.materials.new(name=asset_id)\n                    mat.use_nodes = True\n                    nodes = mat.node_tree.nodes\n                    links = mat.node_tree.links\n\n                    # Clear default nodes\n                    for node in nodes:\n                        nodes.remove(node)\n\n                    # Create output node\n                    output = nodes.new(type='ShaderNodeOutputMaterial')\n                    output.location = (300, 0)\n\n                    # Create principled BSDF node\n                    principled = nodes.new(type='ShaderNodeBsdfPrincipled')\n                    principled.location = (0, 0)\n                    links.new(principled.outputs[0], output.inputs[0])\n\n                    # Add texture nodes based on available maps\n                    tex_coord = nodes.new(type='ShaderNodeTexCoord')\n                    tex_coord.location = (-800, 0)\n\n                    mapping = nodes.new(type='ShaderNodeMapping')\n                    mapping.location = (-600, 0)\n                    mapping.vector_type = 'TEXTURE'  # Changed from default 'POINT' to 'TEXTURE'\n                    links.new(tex_coord.outputs['UV'], mapping.inputs['Vector'])\n\n                    # Position offset for texture nodes\n                    x_pos = -400\n                    y_pos = 300\n\n                    # Connect different texture maps\n                    for map_type, image in downloaded_maps.items():\n                        tex_node = nodes.new(type='ShaderNodeTexImage')\n                        tex_node.location = (x_pos, y_pos)\n                        tex_node.image = image\n\n                        # Set color space based on map type\n                        if map_type.lower() in ['color', 'diffuse', 'albedo']:\n                            try:\n                                tex_node.image.colorspace_settings.name = 'sRGB'\n                            except:\n                                pass  # Use default if sRGB not available\n                        else:\n                            try:\n                                tex_node.image.colorspace_settings.name = 'Non-Color'\n                            except:\n                                pass  # Use default if Non-Color not available\n\n                        links.new(mapping.outputs['Vector'], tex_node.inputs['Vector'])\n\n                        # Connect to appropriate input on Principled BSDF\n                        if map_type.lower() in ['color', 'diffuse', 'albedo']:\n                            links.new(tex_node.outputs['Color'], principled.inputs['Base Color'])\n                        elif map_type.lower() in ['roughness', 'rough']:\n                            links.new(tex_node.outputs['Color'], principled.inputs['Roughness'])\n                        elif map_type.lower() in ['metallic', 'metalness', 'metal']:\n                            links.new(tex_node.outputs['Color'], principled.inputs['Metallic'])\n                        elif map_type.lower() in ['normal', 'nor']:\n                            # Add normal map node\n                            normal_map = nodes.new(type='ShaderNodeNormalMap')\n                            normal_map.location = (x_pos + 200, y_pos)\n                            links.new(tex_node.outputs['Color'], normal_map.inputs['Color'])\n                            links.new(normal_map.outputs['Normal'], principled.inputs['Normal'])\n                        elif map_type in ['displacement', 'disp', 'height']:\n                            # Add displacement node\n                            disp_node = nodes.new(type='ShaderNodeDisplacement')\n                            disp_node.location = (x_pos + 200, y_pos - 200)\n                            links.new(tex_node.outputs['Color'], disp_node.inputs['Height'])\n                            links.new(disp_node.outputs['Displacement'], output.inputs['Displacement'])\n\n                        y_pos -= 250\n\n                    return {\n                        \"success\": True,\n                        \"message\": f\"Texture {asset_id} imported as material\",\n                        \"material\": mat.name,\n                        \"maps\": list(downloaded_maps.keys())\n                    }\n\n                except Exception as e:\n                    return {\"error\": f\"Failed to process textures: {str(e)}\"}\n\n            elif asset_type == \"models\":\n                # For models, prefer glTF format if available\n                if not file_format:\n                    file_format = \"gltf\"  # Default format for models\n\n                if file_format in files_data and resolution in files_data[file_format]:\n                    file_info = files_data[file_format][resolution][file_format]\n                    file_url = file_info[\"url\"]\n\n                    # Create a temporary directory to store the model and its dependencies\n                    temp_dir = tempfile.mkdtemp()\n                    main_file_path = \"\"\n\n                    try:\n                        # Download the main model file\n                        main_file_name = file_url.split(\"/\")[-1]\n                        main_file_path = os.path.join(temp_dir, main_file_name)\n\n                        response = requests.get(file_url, headers=REQ_HEADERS)\n                        if response.status_code != 200:\n                            return {\"error\": f\"Failed to download model: {response.status_code}\"}\n\n                        with open(main_file_path, \"wb\") as f:\n                            f.write(response.content)\n\n                        # Check for included files and download them\n                        if \"include\" in file_info and file_info[\"include\"]:\n                            for include_path, include_info in file_info[\"include\"].items():\n                                # Get the URL for the included file - this is the fix\n                                include_url = include_info[\"url\"]\n\n                                # Create the directory structure for the included file\n                                include_file_path = os.path.join(temp_dir, include_path)\n                                os.makedirs(os.path.dirname(include_file_path), exist_ok=True)\n\n                                # Download the included file\n                                include_response = requests.get(include_url, headers=REQ_HEADERS)\n                                if include_response.status_code == 200:\n                                    with open(include_file_path, \"wb\") as f:\n                                        f.write(include_response.content)\n                                else:\n                                    print(f\"Failed to download included file: {include_path}\")\n\n                        # Import the model into Blender\n                        if file_format == \"gltf\" or file_format == \"glb\":\n                            bpy.ops.import_scene.gltf(filepath=main_file_path)\n                        elif file_format == \"fbx\":\n                            bpy.ops.import_scene.fbx(filepath=main_file_path)\n                        elif file_format == \"obj\":\n                            bpy.ops.import_scene.obj(filepath=main_file_path)\n                        elif file_format == \"blend\":\n                            # For blend files, we need to append or link\n                            with bpy.data.libraries.load(main_file_path, link=False) as (data_from, data_to):\n                                data_to.objects = data_from.objects\n\n                            # Link the objects to the scene\n                            for obj in data_to.objects:\n                                if obj is not None:\n                                    bpy.context.collection.objects.link(obj)\n                        else:\n                            return {\"error\": f\"Unsupported model format: {file_format}\"}\n\n                        # Get the names of imported objects\n                        imported_objects = [obj.name for obj in bpy.context.selected_objects]\n\n                        return {\n                            \"success\": True,\n                            \"message\": f\"Model {asset_id} imported successfully\",\n                            \"imported_objects\": imported_objects\n                        }\n                    except Exception as e:\n                        return {\"error\": f\"Failed to import model: {str(e)}\"}\n                    finally:\n                        # Clean up temporary directory\n                        with suppress(Exception):\n                            shutil.rmtree(temp_dir)\n                else:\n                    return {\"error\": f\"Requested format or resolution not available for this model\"}\n\n            else:\n                return {\"error\": f\"Unsupported asset type: {asset_type}\"}\n\n        except Exception as e:\n            return {\"error\": f\"Failed to download asset: {str(e)}\"}\n\n    def set_texture(self, object_name, texture_id):\n        \"\"\"Apply a previously downloaded Polyhaven texture to an object by creating a new material\"\"\"\n        try:\n            # Get the object\n            obj = bpy.data.objects.get(object_name)\n            if not obj:\n                return {\"error\": f\"Object not found: {object_name}\"}\n\n            # Make sure object can accept materials\n            if not hasattr(obj, 'data') or not hasattr(obj.data, 'materials'):\n                return {\"error\": f\"Object {object_name} cannot accept materials\"}\n\n            # Find all images related to this texture and ensure they're properly loaded\n            texture_images = {}\n            for img in bpy.data.images:\n                if img.name.startswith(texture_id + \"_\"):\n                    # Extract the map type from the image name\n                    map_type = img.name.split('_')[-1].split('.')[0]\n\n                    # Force a reload of the image\n                    img.reload()\n\n                    # Ensure proper color space\n                    if map_type.lower() in ['color', 'diffuse', 'albedo']:\n                        try:\n                            img.colorspace_settings.name = 'sRGB'\n                        except:\n                            pass\n                    else:\n                        try:\n                            img.colorspace_settings.name = 'Non-Color'\n                        except:\n                            pass\n\n                    # Ensure the image is packed\n                    if not img.packed_file:\n                        img.pack()\n\n                    texture_images[map_type] = img\n                    print(f\"Loaded texture map: {map_type} - {img.name}\")\n\n                    # Debug info\n                    print(f\"Image size: {img.size[0]}x{img.size[1]}\")\n                    print(f\"Color space: {img.colorspace_settings.name}\")\n                    print(f\"File format: {img.file_format}\")\n                    print(f\"Is packed: {bool(img.packed_file)}\")\n\n            if not texture_images:\n                return {\"error\": f\"No texture images found for: {texture_id}. Please download the texture first.\"}\n\n            # Create a new material\n            new_mat_name = f\"{texture_id}_material_{object_name}\"\n\n            # Remove any existing material with this name to avoid conflicts\n            existing_mat = bpy.data.materials.get(new_mat_name)\n            if existing_mat:\n                bpy.data.materials.remove(existing_mat)\n\n            new_mat = bpy.data.materials.new(name=new_mat_name)\n            new_mat.use_nodes = True\n\n            # Set up the material nodes\n            nodes = new_mat.node_tree.nodes\n            links = new_mat.node_tree.links\n\n            # Clear default nodes\n            nodes.clear()\n\n            # Create output node\n            output = nodes.new(type='ShaderNodeOutputMaterial')\n            output.location = (600, 0)\n\n            # Create principled BSDF node\n            principled = nodes.new(type='ShaderNodeBsdfPrincipled')\n            principled.location = (300, 0)\n            links.new(principled.outputs[0], output.inputs[0])\n\n            # Add texture nodes based on available maps\n            tex_coord = nodes.new(type='ShaderNodeTexCoord')\n            tex_coord.location = (-800, 0)\n\n            mapping = nodes.new(type='ShaderNodeMapping')\n            mapping.location = (-600, 0)\n            mapping.vector_type = 'TEXTURE'  # Changed from default 'POINT' to 'TEXTURE'\n            links.new(tex_coord.outputs['UV'], mapping.inputs['Vector'])\n\n            # Position offset for texture nodes\n            x_pos = -400\n            y_pos = 300\n\n            # Connect different texture maps\n            for map_type, image in texture_images.items():\n                tex_node = nodes.new(type='ShaderNodeTexImage')\n                tex_node.location = (x_pos, y_pos)\n                tex_node.image = image\n\n                # Set color space based on map type\n                if map_type.lower() in ['color', 'diffuse', 'albedo']:\n                    try:\n                        tex_node.image.colorspace_settings.name = 'sRGB'\n                    except:\n                        pass  # Use default if sRGB not available\n                else:\n                    try:\n                        tex_node.image.colorspace_settings.name = 'Non-Color'\n                    except:\n                        pass  # Use default if Non-Color not available\n\n                links.new(mapping.outputs['Vector'], tex_node.inputs['Vector'])\n\n                # Connect to appropriate input on Principled BSDF\n                if map_type.lower() in ['color', 'diffuse', 'albedo']:\n                    links.new(tex_node.outputs['Color'], principled.inputs['Base Color'])\n                elif map_type.lower() in ['roughness', 'rough']:\n                    links.new(tex_node.outputs['Color'], principled.inputs['Roughness'])\n                elif map_type.lower() in ['metallic', 'metalness', 'metal']:\n                    links.new(tex_node.outputs['Color'], principled.inputs['Metallic'])\n                elif map_type.lower() in ['normal', 'nor', 'dx', 'gl']:\n                    # Add normal map node\n                    normal_map = nodes.new(type='ShaderNodeNormalMap')\n                    normal_map.location = (x_pos + 200, y_pos)\n                    links.new(tex_node.outputs['Color'], normal_map.inputs['Color'])\n                    links.new(normal_map.outputs['Normal'], principled.inputs['Normal'])\n                elif map_type.lower() in ['displacement', 'disp', 'height']:\n                    # Add displacement node\n                    disp_node = nodes.new(type='ShaderNodeDisplacement')\n                    disp_node.location = (x_pos + 200, y_pos - 200)\n                    disp_node.inputs['Scale'].default_value = 0.1  # Reduce displacement strength\n                    links.new(tex_node.outputs['Color'], disp_node.inputs['Height'])\n                    links.new(disp_node.outputs['Displacement'], output.inputs['Displacement'])\n\n                y_pos -= 250\n\n            # Second pass: Connect nodes with proper handling for special cases\n            texture_nodes = {}\n\n            # First find all texture nodes and store them by map type\n            for node in nodes:\n                if node.type == 'TEX_IMAGE' and node.image:\n                    for map_type, image in texture_images.items():\n                        if node.image == image:\n                            texture_nodes[map_type] = node\n                            break\n\n            # Now connect everything using the nodes instead of images\n            # Handle base color (diffuse)\n            for map_name in ['color', 'diffuse', 'albedo']:\n                if map_name in texture_nodes:\n                    links.new(texture_nodes[map_name].outputs['Color'], principled.inputs['Base Color'])\n                    print(f\"Connected {map_name} to Base Color\")\n                    break\n\n            # Handle roughness\n            for map_name in ['roughness', 'rough']:\n                if map_name in texture_nodes:\n                    links.new(texture_nodes[map_name].outputs['Color'], principled.inputs['Roughness'])\n                    print(f\"Connected {map_name} to Roughness\")\n                    break\n\n            # Handle metallic\n            for map_name in ['metallic', 'metalness', 'metal']:\n                if map_name in texture_nodes:\n                    links.new(texture_nodes[map_name].outputs['Color'], principled.inputs['Metallic'])\n                    print(f\"Connected {map_name} to Metallic\")\n                    break\n\n            # Handle normal maps\n            for map_name in ['gl', 'dx', 'nor']:\n                if map_name in texture_nodes:\n                    normal_map_node = nodes.new(type='ShaderNodeNormalMap')\n                    normal_map_node.location = (100, 100)\n                    links.new(texture_nodes[map_name].outputs['Color'], normal_map_node.inputs['Color'])\n                    links.new(normal_map_node.outputs['Normal'], principled.inputs['Normal'])\n                    print(f\"Connected {map_name} to Normal\")\n                    break\n\n            # Handle displacement\n            for map_name in ['displacement', 'disp', 'height']:\n                if map_name in texture_nodes:\n                    disp_node = nodes.new(type='ShaderNodeDisplacement')\n                    disp_node.location = (300, -200)\n                    disp_node.inputs['Scale'].default_value = 0.1  # Reduce displacement strength\n                    links.new(texture_nodes[map_name].outputs['Color'], disp_node.inputs['Height'])\n                    links.new(disp_node.outputs['Displacement'], output.inputs['Displacement'])\n                    print(f\"Connected {map_name} to Displacement\")\n                    break\n\n            # Handle ARM texture (Ambient Occlusion, Roughness, Metallic)\n            if 'arm' in texture_nodes:\n                separate_rgb = nodes.new(type='ShaderNodeSeparateRGB')\n                separate_rgb.location = (-200, -100)\n                links.new(texture_nodes['arm'].outputs['Color'], separate_rgb.inputs['Image'])\n\n                # Connect Roughness (G) if no dedicated roughness map\n                if not any(map_name in texture_nodes for map_name in ['roughness', 'rough']):\n                    links.new(separate_rgb.outputs['G'], principled.inputs['Roughness'])\n                    print(\"Connected ARM.G to Roughness\")\n\n                # Connect Metallic (B) if no dedicated metallic map\n                if not any(map_name in texture_nodes for map_name in ['metallic', 'metalness', 'metal']):\n                    links.new(separate_rgb.outputs['B'], principled.inputs['Metallic'])\n                    print(\"Connected ARM.B to Metallic\")\n\n                # For AO (R channel), multiply with base color if we have one\n                base_color_node = None\n                for map_name in ['color', 'diffuse', 'albedo']:\n                    if map_name in texture_nodes:\n                        base_color_node = texture_nodes[map_name]\n                        break\n\n                if base_color_node:\n                    mix_node = nodes.new(type='ShaderNodeMixRGB')\n                    mix_node.location = (100, 200)\n                    mix_node.blend_type = 'MULTIPLY'\n                    mix_node.inputs['Fac'].default_value = 0.8  # 80% influence\n\n                    # Disconnect direct connection to base color\n                    for link in base_color_node.outputs['Color'].links:\n                        if link.to_socket == principled.inputs['Base Color']:\n                            links.remove(link)\n\n                    # Connect through the mix node\n                    links.new(base_color_node.outputs['Color'], mix_node.inputs[1])\n                    links.new(separate_rgb.outputs['R'], mix_node.inputs[2])\n                    links.new(mix_node.outputs['Color'], principled.inputs['Base Color'])\n                    print(\"Connected ARM.R to AO mix with Base Color\")\n\n            # Handle AO (Ambient Occlusion) if separate\n            if 'ao' in texture_nodes:\n                base_color_node = None\n                for map_name in ['color', 'diffuse', 'albedo']:\n                    if map_name in texture_nodes:\n                        base_color_node = texture_nodes[map_name]\n                        break\n\n                if base_color_node:\n                    mix_node = nodes.new(type='ShaderNodeMixRGB')\n                    mix_node.location = (100, 200)\n                    mix_node.blend_type = 'MULTIPLY'\n                    mix_node.inputs['Fac'].default_value = 0.8  # 80% influence\n\n                    # Disconnect direct connection to base color\n                    for link in base_color_node.outputs['Color'].links:\n                        if link.to_socket == principled.inputs['Base Color']:\n                            links.remove(link)\n\n                    # Connect through the mix node\n                    links.new(base_color_node.outputs['Color'], mix_node.inputs[1])\n                    links.new(texture_nodes['ao'].outputs['Color'], mix_node.inputs[2])\n                    links.new(mix_node.outputs['Color'], principled.inputs['Base Color'])\n                    print(\"Connected AO to mix with Base Color\")\n\n            # CRITICAL: Make sure to clear all existing materials from the object\n            while len(obj.data.materials) > 0:\n                obj.data.materials.pop(index=0)\n\n            # Assign the new material to the object\n            obj.data.materials.append(new_mat)\n\n            # CRITICAL: Make the object active and select it\n            bpy.context.view_layer.objects.active = obj\n            obj.select_set(True)\n\n            # CRITICAL: Force Blender to update the material\n            bpy.context.view_layer.update()\n\n            # Get the list of texture maps\n            texture_maps = list(texture_images.keys())\n\n            # Get info about texture nodes for debugging\n            material_info = {\n                \"name\": new_mat.name,\n                \"has_nodes\": new_mat.use_nodes,\n                \"node_count\": len(new_mat.node_tree.nodes),\n                \"texture_nodes\": []\n            }\n\n            for node in new_mat.node_tree.nodes:\n                if node.type == 'TEX_IMAGE' and node.image:\n                    connections = []\n                    for output in node.outputs:\n                        for link in output.links:\n                            connections.append(f\"{output.name} → {link.to_node.name}.{link.to_socket.name}\")\n\n                    material_info[\"texture_nodes\"].append({\n                        \"name\": node.name,\n                        \"image\": node.image.name,\n                        \"colorspace\": node.image.colorspace_settings.name,\n                        \"connections\": connections\n                    })\n\n            return {\n                \"success\": True,\n                \"message\": f\"Created new material and applied texture {texture_id} to {object_name}\",\n                \"material\": new_mat.name,\n                \"maps\": texture_maps,\n                \"material_info\": material_info\n            }\n\n        except Exception as e:\n            print(f\"Error in set_texture: {str(e)}\")\n            traceback.print_exc()\n            return {\"error\": f\"Failed to apply texture: {str(e)}\"}\n\n    def get_telemetry_consent(self):\n        \"\"\"Get the current telemetry consent status\"\"\"\n        try:\n            # Get addon preferences - use the module name\n            addon_prefs = bpy.context.preferences.addons.get(__name__)\n            if addon_prefs:\n                consent = addon_prefs.preferences.telemetry_consent\n            else:\n                # Fallback to default if preferences not available\n                consent = True\n        except (AttributeError, KeyError):\n            # Fallback to default if preferences not available\n            consent = True\n        return {\"consent\": consent}\n\n    def get_polyhaven_status(self):\n        \"\"\"Get the current status of PolyHaven integration\"\"\"\n        enabled = bpy.context.scene.blendermcp_use_polyhaven\n        if enabled:\n            return {\"enabled\": True, \"message\": \"PolyHaven integration is enabled and ready to use.\"}\n        else:\n            return {\n                \"enabled\": False,\n                \"message\": \"\"\"PolyHaven integration is currently disabled. To enable it:\n                            1. In the 3D Viewport, find the BlenderMCP panel in the sidebar (press N if hidden)\n                            2. Check the 'Use assets from Poly Haven' checkbox\n                            3. Restart the connection to Claude\"\"\"\n        }\n\n    #region Hyper3D\n    def get_hyper3d_status(self):\n        \"\"\"Get the current status of Hyper3D Rodin integration\"\"\"\n        enabled = bpy.context.scene.blendermcp_use_hyper3d\n        if enabled:\n            if not bpy.context.scene.blendermcp_hyper3d_api_key:\n                return {\n                    \"enabled\": False,\n                    \"message\": \"\"\"Hyper3D Rodin integration is currently enabled, but API key is not given. To enable it:\n                                1. In the 3D Viewport, find the BlenderMCP panel in the sidebar (press N if hidden)\n                                2. Keep the 'Use Hyper3D Rodin 3D model generation' checkbox checked\n                                3. Choose the right plaform and fill in the API Key\n                                4. Restart the connection to Claude\"\"\"\n                }\n            mode = bpy.context.scene.blendermcp_hyper3d_mode\n            message = f\"Hyper3D Rodin integration is enabled and ready to use. Mode: {mode}. \" + \\\n                f\"Key type: {'private' if bpy.context.scene.blendermcp_hyper3d_api_key != RODIN_FREE_TRIAL_KEY else 'free_trial'}\"\n            return {\n                \"enabled\": True,\n                \"message\": message\n            }\n        else:\n            return {\n                \"enabled\": False,\n                \"message\": \"\"\"Hyper3D Rodin integration is currently disabled. To enable it:\n                            1. In the 3D Viewport, find the BlenderMCP panel in the sidebar (press N if hidden)\n                            2. Check the 'Use Hyper3D Rodin 3D model generation' checkbox\n                            3. Restart the connection to Claude\"\"\"\n            }\n\n    def create_rodin_job(self, *args, **kwargs):\n        match bpy.context.scene.blendermcp_hyper3d_mode:\n            case \"MAIN_SITE\":\n                return self.create_rodin_job_main_site(*args, **kwargs)\n            case \"FAL_AI\":\n                return self.create_rodin_job_fal_ai(*args, **kwargs)\n            case _:\n                return f\"Error: Unknown Hyper3D Rodin mode!\"\n\n    def create_rodin_job_main_site(\n            self,\n            text_prompt: str=None,\n            images: list[tuple[str, str]]=None,\n            bbox_condition=None\n        ):\n        try:\n            if images is None:\n                images = []\n            \"\"\"Call Rodin API, get the job uuid and subscription key\"\"\"\n            files = [\n                *[(\"images\", (f\"{i:04d}{img_suffix}\", img)) for i, (img_suffix, img) in enumerate(images)],\n                (\"tier\", (None, \"Sketch\")),\n                (\"mesh_mode\", (None, \"Raw\")),\n            ]\n            if text_prompt:\n                files.append((\"prompt\", (None, text_prompt)))\n            if bbox_condition:\n                files.append((\"bbox_condition\", (None, json.dumps(bbox_condition))))\n            response = requests.post(\n                \"https://hyperhuman.deemos.com/api/v2/rodin\",\n                headers={\n                    \"Authorization\": f\"Bearer {bpy.context.scene.blendermcp_hyper3d_api_key}\",\n                },\n                files=files\n            )\n            data = response.json()\n            return data\n        except Exception as e:\n            return {\"error\": str(e)}\n\n    def create_rodin_job_fal_ai(\n            self,\n            text_prompt: str=None,\n            images: list[tuple[str, str]]=None,\n            bbox_condition=None\n        ):\n        try:\n            req_data = {\n                \"tier\": \"Sketch\",\n            }\n            if images:\n                req_data[\"input_image_urls\"] = images\n            if text_prompt:\n                req_data[\"prompt\"] = text_prompt\n            if bbox_condition:\n                req_data[\"bbox_condition\"] = bbox_condition\n            response = requests.post(\n                \"https://queue.fal.run/fal-ai/hyper3d/rodin\",\n                headers={\n                    \"Authorization\": f\"Key {bpy.context.scene.blendermcp_hyper3d_api_key}\",\n                    \"Content-Type\": \"application/json\",\n                },\n                json=req_data\n            )\n            data = response.json()\n            return data\n        except Exception as e:\n            return {\"error\": str(e)}\n\n    def poll_rodin_job_status(self, *args, **kwargs):\n        match bpy.context.scene.blendermcp_hyper3d_mode:\n            case \"MAIN_SITE\":\n                return self.poll_rodin_job_status_main_site(*args, **kwargs)\n            case \"FAL_AI\":\n                return self.poll_rodin_job_status_fal_ai(*args, **kwargs)\n            case _:\n                return f\"Error: Unknown Hyper3D Rodin mode!\"\n\n    def poll_rodin_job_status_main_site(self, subscription_key: str):\n        \"\"\"Call the job status API to get the job status\"\"\"\n        response = requests.post(\n            \"https://hyperhuman.deemos.com/api/v2/status\",\n            headers={\n                \"Authorization\": f\"Bearer {bpy.context.scene.blendermcp_hyper3d_api_key}\",\n            },\n            json={\n                \"subscription_key\": subscription_key,\n            },\n        )\n        data = response.json()\n        return {\n            \"status_list\": [i[\"status\"] for i in data[\"jobs\"]]\n        }\n\n    def poll_rodin_job_status_fal_ai(self, request_id: str):\n        \"\"\"Call the job status API to get the job status\"\"\"\n        response = requests.get(\n            f\"https://queue.fal.run/fal-ai/hyper3d/requests/{request_id}/status\",\n            headers={\n                \"Authorization\": f\"KEY {bpy.context.scene.blendermcp_hyper3d_api_key}\",\n            },\n        )\n        data = response.json()\n        return data\n\n    @staticmethod\n    def _clean_imported_glb(filepath, mesh_name=None):\n        # Get the set of existing objects before import\n        existing_objects = set(bpy.data.objects)\n\n        # Import the GLB file\n        bpy.ops.import_scene.gltf(filepath=filepath)\n\n        # Ensure the context is updated\n        bpy.context.view_layer.update()\n\n        # Get all imported objects\n        imported_objects = list(set(bpy.data.objects) - existing_objects)\n        # imported_objects = [obj for obj in bpy.context.view_layer.objects if obj.select_get()]\n\n        if not imported_objects:\n            print(\"Error: No objects were imported.\")\n            return\n\n        # Identify the mesh object\n        mesh_obj = None\n\n        if len(imported_objects) == 1 and imported_objects[0].type == 'MESH':\n            mesh_obj = imported_objects[0]\n            print(\"Single mesh imported, no cleanup needed.\")\n        else:\n            if len(imported_objects) == 2:\n                empty_objs = [i for i in imported_objects if i.type == \"EMPTY\"]\n                if len(empty_objs) != 1:\n                    print(\"Error: Expected an empty node with one mesh child or a single mesh object.\")\n                    return\n                parent_obj = empty_objs.pop()\n                if len(parent_obj.children) == 1:\n                    potential_mesh = parent_obj.children[0]\n                    if potential_mesh.type == 'MESH':\n                        print(\"GLB structure confirmed: Empty node with one mesh child.\")\n\n                        # Unparent the mesh from the empty node\n                        potential_mesh.parent = None\n\n                        # Remove the empty node\n                        bpy.data.objects.remove(parent_obj)\n                        print(\"Removed empty node, keeping only the mesh.\")\n\n                        mesh_obj = potential_mesh\n                    else:\n                        print(\"Error: Child is not a mesh object.\")\n                        return\n                else:\n                    print(\"Error: Expected an empty node with one mesh child or a single mesh object.\")\n                    return\n            else:\n                print(\"Error: Expected an empty node with one mesh child or a single mesh object.\")\n                return\n\n        # Rename the mesh if needed\n        try:\n            if mesh_obj and mesh_obj.name is not None and mesh_name:\n                mesh_obj.name = mesh_name\n                if mesh_obj.data.name is not None:\n                    mesh_obj.data.name = mesh_name\n                print(f\"Mesh renamed to: {mesh_name}\")\n        except Exception as e:\n            print(\"Having issue with renaming, give up renaming.\")\n\n        return mesh_obj\n\n    def import_generated_asset(self, *args, **kwargs):\n        match bpy.context.scene.blendermcp_hyper3d_mode:\n            case \"MAIN_SITE\":\n                return self.import_generated_asset_main_site(*args, **kwargs)\n            case \"FAL_AI\":\n                return self.import_generated_asset_fal_ai(*args, **kwargs)\n            case _:\n                return f\"Error: Unknown Hyper3D Rodin mode!\"\n\n    def import_generated_asset_main_site(self, task_uuid: str, name: str):\n        \"\"\"Fetch the generated asset, import into blender\"\"\"\n        response = requests.post(\n            \"https://hyperhuman.deemos.com/api/v2/download\",\n            headers={\n                \"Authorization\": f\"Bearer {bpy.context.scene.blendermcp_hyper3d_api_key}\",\n            },\n            json={\n                'task_uuid': task_uuid\n            }\n        )\n        data_ = response.json()\n        temp_file = None\n        for i in data_[\"list\"]:\n            if i[\"name\"].endswith(\".glb\"):\n                temp_file = tempfile.NamedTemporaryFile(\n                    delete=False,\n                    prefix=task_uuid,\n                    suffix=\".glb\",\n                )\n\n                try:\n                    # Download the content\n                    response = requests.get(i[\"url\"], stream=True)\n                    response.raise_for_status()  # Raise an exception for HTTP errors\n\n                    # Write the content to the temporary file\n                    for chunk in response.iter_content(chunk_size=8192):\n                        temp_file.write(chunk)\n\n                    # Close the file\n                    temp_file.close()\n\n                except Exception as e:\n                    # Clean up the file if there's an error\n                    temp_file.close()\n                    os.unlink(temp_file.name)\n                    return {\"succeed\": False, \"error\": str(e)}\n\n                break\n        else:\n            return {\"succeed\": False, \"error\": \"Generation failed. Please first make sure that all jobs of the task are done and then try again later.\"}\n\n        try:\n            obj = self._clean_imported_glb(\n                filepath=temp_file.name,\n                mesh_name=name\n            )\n            result = {\n                \"name\": obj.name,\n                \"type\": obj.type,\n                \"location\": [obj.location.x, obj.location.y, obj.location.z],\n                \"rotation\": [obj.rotation_euler.x, obj.rotation_euler.y, obj.rotation_euler.z],\n                \"scale\": [obj.scale.x, obj.scale.y, obj.scale.z],\n            }\n\n            if obj.type == \"MESH\":\n                bounding_box = self._get_aabb(obj)\n                result[\"world_bounding_box\"] = bounding_box\n\n            return {\n                \"succeed\": True, **result\n            }\n        except Exception as e:\n            return {\"succeed\": False, \"error\": str(e)}\n\n    def import_generated_asset_fal_ai(self, request_id: str, name: str):\n        \"\"\"Fetch the generated asset, import into blender\"\"\"\n        response = requests.get(\n            f\"https://queue.fal.run/fal-ai/hyper3d/requests/{request_id}\",\n            headers={\n                \"Authorization\": f\"Key {bpy.context.scene.blendermcp_hyper3d_api_key}\",\n            }\n        )\n        data_ = response.json()\n        temp_file = None\n\n        temp_file = tempfile.NamedTemporaryFile(\n            delete=False,\n            prefix=request_id,\n            suffix=\".glb\",\n        )\n\n        try:\n            # Download the content\n            response = requests.get(data_[\"model_mesh\"][\"url\"], stream=True)\n            response.raise_for_status()  # Raise an exception for HTTP errors\n\n            # Write the content to the temporary file\n            for chunk in response.iter_content(chunk_size=8192):\n                temp_file.write(chunk)\n\n            # Close the file\n            temp_file.close()\n\n        except Exception as e:\n            # Clean up the file if there's an error\n            temp_file.close()\n            os.unlink(temp_file.name)\n            return {\"succeed\": False, \"error\": str(e)}\n\n        try:\n            obj = self._clean_imported_glb(\n                filepath=temp_file.name,\n                mesh_name=name\n            )\n            result = {\n                \"name\": obj.name,\n                \"type\": obj.type,\n                \"location\": [obj.location.x, obj.location.y, obj.location.z],\n                \"rotation\": [obj.rotation_euler.x, obj.rotation_euler.y, obj.rotation_euler.z],\n                \"scale\": [obj.scale.x, obj.scale.y, obj.scale.z],\n            }\n\n            if obj.type == \"MESH\":\n                bounding_box = self._get_aabb(obj)\n                result[\"world_bounding_box\"] = bounding_box\n\n            return {\n                \"succeed\": True, **result\n            }\n        except Exception as e:\n            return {\"succeed\": False, \"error\": str(e)}\n    #endregion\n \n    #region Sketchfab API\n    def get_sketchfab_status(self):\n        \"\"\"Get the current status of Sketchfab integration\"\"\"\n        enabled = bpy.context.scene.blendermcp_use_sketchfab\n        api_key = bpy.context.scene.blendermcp_sketchfab_api_key\n\n        # Test the API key if present\n        if api_key:\n            try:\n                headers = {\n                    \"Authorization\": f\"Token {api_key}\"\n                }\n\n                response = requests.get(\n                    \"https://api.sketchfab.com/v3/me\",\n                    headers=headers,\n                    timeout=30  # Add timeout of 30 seconds\n                )\n\n                if response.status_code == 200:\n                    user_data = response.json()\n                    username = user_data.get(\"username\", \"Unknown user\")\n                    return {\n                        \"enabled\": True,\n                        \"message\": f\"Sketchfab integration is enabled and ready to use. Logged in as: {username}\"\n                    }\n                else:\n                    return {\n                        \"enabled\": False,\n                        \"message\": f\"Sketchfab API key seems invalid. Status code: {response.status_code}\"\n                    }\n            except requests.exceptions.Timeout:\n                return {\n                    \"enabled\": False,\n                    \"message\": \"Timeout connecting to Sketchfab API. Check your internet connection.\"\n                }\n            except Exception as e:\n                return {\n                    \"enabled\": False,\n                    \"message\": f\"Error testing Sketchfab API key: {str(e)}\"\n                }\n\n        if enabled and api_key:\n            return {\"enabled\": True, \"message\": \"Sketchfab integration is enabled and ready to use.\"}\n        elif enabled and not api_key:\n            return {\n                \"enabled\": False,\n                \"message\": \"\"\"Sketchfab integration is currently enabled, but API key is not given. To enable it:\n                            1. In the 3D Viewport, find the BlenderMCP panel in the sidebar (press N if hidden)\n                            2. Keep the 'Use Sketchfab' checkbox checked\n                            3. Enter your Sketchfab API Key\n                            4. Restart the connection to Claude\"\"\"\n            }\n        else:\n            return {\n                \"enabled\": False,\n                \"message\": \"\"\"Sketchfab integration is currently disabled. To enable it:\n                            1. In the 3D Viewport, find the BlenderMCP panel in the sidebar (press N if hidden)\n                            2. Check the 'Use assets from Sketchfab' checkbox\n                            3. Enter your Sketchfab API Key\n                            4. Restart the connection to Claude\"\"\"\n            }\n\n    def search_sketchfab_models(self, query, categories=None, count=20, downloadable=True):\n        \"\"\"Search for models on Sketchfab based on query and optional filters\"\"\"\n        try:\n            api_key = bpy.context.scene.blendermcp_sketchfab_api_key\n            if not api_key:\n                return {\"error\": \"Sketchfab API key is not configured\"}\n\n            # Build search parameters with exact fields from Sketchfab API docs\n            params = {\n                \"type\": \"models\",\n                \"q\": query,\n                \"count\": count,\n                \"downloadable\": downloadable,\n                \"archives_flavours\": False\n            }\n\n            if categories:\n                params[\"categories\"] = categories\n\n            # Make API request to Sketchfab search endpoint\n            # The proper format according to Sketchfab API docs for API key auth\n            headers = {\n                \"Authorization\": f\"Token {api_key}\"\n            }\n\n\n            # Use the search endpoint as specified in the API documentation\n            response = requests.get(\n                \"https://api.sketchfab.com/v3/search\",\n                headers=headers,\n                params=params,\n                timeout=30  # Add timeout of 30 seconds\n            )\n\n            if response.status_code == 401:\n                return {\"error\": \"Authentication failed (401). Check your API key.\"}\n\n            if response.status_code != 200:\n                return {\"error\": f\"API request failed with status code {response.status_code}\"}\n\n            response_data = response.json()\n\n            # Safety check on the response structure\n            if response_data is None:\n                return {\"error\": \"Received empty response from Sketchfab API\"}\n\n            # Handle 'results' potentially missing from response\n            results = response_data.get(\"results\", [])\n            if not isinstance(results, list):\n                return {\"error\": f\"Unexpected response format from Sketchfab API: {response_data}\"}\n\n            return response_data\n\n        except requests.exceptions.Timeout:\n            return {\"error\": \"Request timed out. Check your internet connection.\"}\n        except json.JSONDecodeError as e:\n            return {\"error\": f\"Invalid JSON response from Sketchfab API: {str(e)}\"}\n        except Exception as e:\n            import traceback\n            traceback.print_exc()\n            return {\"error\": str(e)}\n\n    def get_sketchfab_model_preview(self, uid):\n        \"\"\"Get thumbnail preview image of a Sketchfab model by its UID\"\"\"\n        try:\n            import base64\n            \n            api_key = bpy.context.scene.blendermcp_sketchfab_api_key\n            if not api_key:\n                return {\"error\": \"Sketchfab API key is not configured\"}\n\n            headers = {\"Authorization\": f\"Token {api_key}\"}\n            \n            # Get model info which includes thumbnails\n            response = requests.get(\n                f\"https://api.sketchfab.com/v3/models/{uid}\",\n                headers=headers,\n                timeout=30\n            )\n            \n            if response.status_code == 401:\n                return {\"error\": \"Authentication failed (401). Check your API key.\"}\n            \n            if response.status_code == 404:\n                return {\"error\": f\"Model not found: {uid}\"}\n            \n            if response.status_code != 200:\n                return {\"error\": f\"Failed to get model info: {response.status_code}\"}\n            \n            data = response.json()\n            thumbnails = data.get(\"thumbnails\", {}).get(\"images\", [])\n            \n            if not thumbnails:\n                return {\"error\": \"No thumbnail available for this model\"}\n            \n            # Find a suitable thumbnail (prefer medium size ~640px)\n            selected_thumbnail = None\n            for thumb in thumbnails:\n                width = thumb.get(\"width\", 0)\n                if 400 <= width <= 800:\n                    selected_thumbnail = thumb\n                    break\n            \n            # Fallback to the first available thumbnail\n            if not selected_thumbnail:\n                selected_thumbnail = thumbnails[0]\n            \n            thumbnail_url = selected_thumbnail.get(\"url\")\n            if not thumbnail_url:\n                return {\"error\": \"Thumbnail URL not found\"}\n            \n            # Download the thumbnail image\n            img_response = requests.get(thumbnail_url, timeout=30)\n            if img_response.status_code != 200:\n                return {\"error\": f\"Failed to download thumbnail: {img_response.status_code}\"}\n            \n            # Encode image as base64\n            image_data = base64.b64encode(img_response.content).decode('ascii')\n            \n            # Determine format from content type or URL\n            content_type = img_response.headers.get(\"Content-Type\", \"\")\n            if \"png\" in content_type or thumbnail_url.endswith(\".png\"):\n                img_format = \"png\"\n            else:\n                img_format = \"jpeg\"\n            \n            # Get additional model info for context\n            model_name = data.get(\"name\", \"Unknown\")\n            author = data.get(\"user\", {}).get(\"username\", \"Unknown\")\n            \n            return {\n                \"success\": True,\n                \"image_data\": image_data,\n                \"format\": img_format,\n                \"model_name\": model_name,\n                \"author\": author,\n                \"uid\": uid,\n                \"thumbnail_width\": selected_thumbnail.get(\"width\"),\n                \"thumbnail_height\": selected_thumbnail.get(\"height\")\n            }\n            \n        except requests.exceptions.Timeout:\n            return {\"error\": \"Request timed out. Check your internet connection.\"}\n        except Exception as e:\n            import traceback\n            traceback.print_exc()\n            return {\"error\": f\"Failed to get model preview: {str(e)}\"}\n\n    def download_sketchfab_model(self, uid, normalize_size=False, target_size=1.0):\n        \"\"\"Download a model from Sketchfab by its UID\n        \n        Parameters:\n        - uid: The unique identifier of the Sketchfab model\n        - normalize_size: If True, scale the model so its largest dimension equals target_size\n        - target_size: The target size in Blender units (meters) for the largest dimension\n        \"\"\"\n        try:\n            api_key = bpy.context.scene.blendermcp_sketchfab_api_key\n            if not api_key:\n                return {\"error\": \"Sketchfab API key is not configured\"}\n\n            # Use proper authorization header for API key auth\n            headers = {\n                \"Authorization\": f\"Token {api_key}\"\n            }\n\n            # Request download URL using the exact endpoint from the documentation\n            download_endpoint = f\"https://api.sketchfab.com/v3/models/{uid}/download\"\n\n            response = requests.get(\n                download_endpoint,\n                headers=headers,\n                timeout=30  # Add timeout of 30 seconds\n            )\n\n            if response.status_code == 401:\n                return {\"error\": \"Authentication failed (401). Check your API key.\"}\n\n            if response.status_code != 200:\n                return {\"error\": f\"Download request failed with status code {response.status_code}\"}\n\n            data = response.json()\n\n            # Safety check for None data\n            if data is None:\n                return {\"error\": \"Received empty response from Sketchfab API for download request\"}\n\n            # Extract download URL with safety checks\n            gltf_data = data.get(\"gltf\")\n            if not gltf_data:\n                return {\"error\": \"No gltf download URL available for this model. Response: \" + str(data)}\n\n            download_url = gltf_data.get(\"url\")\n            if not download_url:\n                return {\"error\": \"No download URL available for this model. Make sure the model is downloadable and you have access.\"}\n\n            # Download the model (already has timeout)\n            model_response = requests.get(download_url, timeout=60)  # 60 second timeout\n\n            if model_response.status_code != 200:\n                return {\"error\": f\"Model download failed with status code {model_response.status_code}\"}\n\n            # Save to temporary file\n            temp_dir = tempfile.mkdtemp()\n            zip_file_path = os.path.join(temp_dir, f\"{uid}.zip\")\n\n            with open(zip_file_path, \"wb\") as f:\n                f.write(model_response.content)\n\n            # Extract the zip file with enhanced security\n            with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:\n                # More secure zip slip prevention\n                for file_info in zip_ref.infolist():\n                    # Get the path of the file\n                    file_path = file_info.filename\n\n                    # Convert directory separators to the current OS style\n                    # This handles both / and \\ in zip entries\n                    target_path = os.path.join(temp_dir, os.path.normpath(file_path))\n\n                    # Get absolute paths for comparison\n                    abs_temp_dir = os.path.abspath(temp_dir)\n                    abs_target_path = os.path.abspath(target_path)\n\n                    # Ensure the normalized path doesn't escape the target directory\n                    if not abs_target_path.startswith(abs_temp_dir):\n                        with suppress(Exception):\n                            shutil.rmtree(temp_dir)\n                        return {\"error\": \"Security issue: Zip contains files with path traversal attempt\"}\n\n                    # Additional explicit check for directory traversal\n                    if \"..\" in file_path:\n                        with suppress(Exception):\n                            shutil.rmtree(temp_dir)\n                        return {\"error\": \"Security issue: Zip contains files with directory traversal sequence\"}\n\n                # If all files passed security checks, extract them\n                zip_ref.extractall(temp_dir)\n\n            # Find the main glTF file\n            gltf_files = [f for f in os.listdir(temp_dir) if f.endswith('.gltf') or f.endswith('.glb')]\n\n            if not gltf_files:\n                with suppress(Exception):\n                    shutil.rmtree(temp_dir)\n                return {\"error\": \"No glTF file found in the downloaded model\"}\n\n            main_file = os.path.join(temp_dir, gltf_files[0])\n\n            # Import the model\n            bpy.ops.import_scene.gltf(filepath=main_file)\n\n            # Get the imported objects\n            imported_objects = list(bpy.context.selected_objects)\n            imported_object_names = [obj.name for obj in imported_objects]\n\n            # Clean up temporary files\n            with suppress(Exception):\n                shutil.rmtree(temp_dir)\n\n            # Find root objects (objects without parents in the imported set)\n            root_objects = [obj for obj in imported_objects if obj.parent is None]\n\n            # Helper function to recursively get all mesh children\n            def get_all_mesh_children(obj):\n                \"\"\"Recursively collect all mesh objects in the hierarchy\"\"\"\n                meshes = []\n                if obj.type == 'MESH':\n                    meshes.append(obj)\n                for child in obj.children:\n                    meshes.extend(get_all_mesh_children(child))\n                return meshes\n\n            # Collect ALL meshes from the entire hierarchy (starting from roots)\n            all_meshes = []\n            for obj in root_objects:\n                all_meshes.extend(get_all_mesh_children(obj))\n            \n            if all_meshes:\n                # Calculate combined world bounding box for all meshes\n                all_min = mathutils.Vector((float('inf'), float('inf'), float('inf')))\n                all_max = mathutils.Vector((float('-inf'), float('-inf'), float('-inf')))\n                \n                for mesh_obj in all_meshes:\n                    # Get world-space bounding box corners\n                    for corner in mesh_obj.bound_box:\n                        world_corner = mesh_obj.matrix_world @ mathutils.Vector(corner)\n                        all_min.x = min(all_min.x, world_corner.x)\n                        all_min.y = min(all_min.y, world_corner.y)\n                        all_min.z = min(all_min.z, world_corner.z)\n                        all_max.x = max(all_max.x, world_corner.x)\n                        all_max.y = max(all_max.y, world_corner.y)\n                        all_max.z = max(all_max.z, world_corner.z)\n                \n                # Calculate dimensions\n                dimensions = [\n                    all_max.x - all_min.x,\n                    all_max.y - all_min.y,\n                    all_max.z - all_min.z\n                ]\n                max_dimension = max(dimensions)\n                \n                # Apply normalization if requested\n                scale_applied = 1.0\n                if normalize_size and max_dimension > 0:\n                    scale_factor = target_size / max_dimension\n                    scale_applied = scale_factor\n                    \n                    # ✅ Only apply scale to ROOT objects (not children!)\n                    # Child objects inherit parent's scale through matrix_world\n                    for root in root_objects:\n                        root.scale = (\n                            root.scale.x * scale_factor,\n                            root.scale.y * scale_factor,\n                            root.scale.z * scale_factor\n                        )\n                    \n                    # Update the scene to recalculate matrix_world for all objects\n                    bpy.context.view_layer.update()\n                    \n                    # Recalculate bounding box after scaling\n                    all_min = mathutils.Vector((float('inf'), float('inf'), float('inf')))\n                    all_max = mathutils.Vector((float('-inf'), float('-inf'), float('-inf')))\n                    \n                    for mesh_obj in all_meshes:\n                        for corner in mesh_obj.bound_box:\n                            world_corner = mesh_obj.matrix_world @ mathutils.Vector(corner)\n                            all_min.x = min(all_min.x, world_corner.x)\n                            all_min.y = min(all_min.y, world_corner.y)\n                            all_min.z = min(all_min.z, world_corner.z)\n                            all_max.x = max(all_max.x, world_corner.x)\n                            all_max.y = max(all_max.y, world_corner.y)\n                            all_max.z = max(all_max.z, world_corner.z)\n                    \n                    dimensions = [\n                        all_max.x - all_min.x,\n                        all_max.y - all_min.y,\n                        all_max.z - all_min.z\n                    ]\n                \n                world_bounding_box = [[all_min.x, all_min.y, all_min.z], [all_max.x, all_max.y, all_max.z]]\n            else:\n                world_bounding_box = None\n                dimensions = None\n                scale_applied = 1.0\n\n            result = {\n                \"success\": True,\n                \"message\": \"Model imported successfully\",\n                \"imported_objects\": imported_object_names\n            }\n            \n            if world_bounding_box:\n                result[\"world_bounding_box\"] = world_bounding_box\n            if dimensions:\n                result[\"dimensions\"] = [round(d, 4) for d in dimensions]\n            if normalize_size:\n                result[\"scale_applied\"] = round(scale_applied, 6)\n                result[\"normalized\"] = True\n            \n            return result\n\n        except requests.exceptions.Timeout:\n            return {\"error\": \"Request timed out. Check your internet connection and try again with a simpler model.\"}\n        except json.JSONDecodeError as e:\n            return {\"error\": f\"Invalid JSON response from Sketchfab API: {str(e)}\"}\n        except Exception as e:\n            import traceback\n            traceback.print_exc()\n            return {\"error\": f\"Failed to download model: {str(e)}\"}\n    #endregion\n\n    #region Hunyuan3D\n    def get_hunyuan3d_status(self):\n        \"\"\"Get the current status of Hunyuan3D integration\"\"\"\n        enabled = bpy.context.scene.blendermcp_use_hunyuan3d\n        hunyuan3d_mode = bpy.context.scene.blendermcp_hunyuan3d_mode\n        if enabled:\n            match hunyuan3d_mode:\n                case \"OFFICIAL_API\":\n                    if not bpy.context.scene.blendermcp_hunyuan3d_secret_id or not bpy.context.scene.blendermcp_hunyuan3d_secret_key:\n                        return {\n                            \"enabled\": False, \n                            \"mode\": hunyuan3d_mode, \n                            \"message\": \"\"\"Hunyuan3D integration is currently enabled, but SecretId or SecretKey is not given. To enable it:\n                                1. In the 3D Viewport, find the BlenderMCP panel in the sidebar (press N if hidden)\n                                2. Keep the 'Use Tencent Hunyuan 3D model generation' checkbox checked\n                                3. Choose the right platform and fill in the SecretId and SecretKey\n                                4. Restart the connection to Claude\"\"\"\n                        }\n                case \"LOCAL_API\":\n                    if not bpy.context.scene.blendermcp_hunyuan3d_api_url:\n                        return {\n                            \"enabled\": False, \n                            \"mode\": hunyuan3d_mode, \n                            \"message\": \"\"\"Hunyuan3D integration is currently enabled, but API URL  is not given. To enable it:\n                                1. In the 3D Viewport, find the BlenderMCP panel in the sidebar (press N if hidden)\n                                2. Keep the 'Use Tencent Hunyuan 3D model generation' checkbox checked\n                                3. Choose the right platform and fill in the API URL\n                                4. Restart the connection to Claude\"\"\"\n                        }\n                case _:\n                    return {\n                        \"enabled\": False, \n                        \"message\": \"Hunyuan3D integration is enabled and mode is not supported.\"\n                    }\n            return {\n                \"enabled\": True, \n                \"mode\": hunyuan3d_mode,\n                \"message\": \"Hunyuan3D integration is enabled and ready to use.\"\n            }\n        return {\n            \"enabled\": False, \n            \"message\": \"\"\"Hunyuan3D integration is currently disabled. To enable it:\n                        1. In the 3D Viewport, find the BlenderMCP panel in the sidebar (press N if hidden)\n                        2. Check the 'Use Tencent Hunyuan 3D model generation' checkbox\n                        3. Restart the connection to Claude\"\"\"\n        }\n    \n    @staticmethod\n    def get_tencent_cloud_sign_headers(\n        method: str,\n        path: str,\n        headParams: dict,\n        data: dict,\n        service: str,\n        region: str,\n        secret_id: str,\n        secret_key: str,\n        host: str = None\n    ):\n        \"\"\"Generate the signature header required for Tencent Cloud API requests headers\"\"\"\n        # Generate timestamp\n        timestamp = int(time.time())\n        date = datetime.utcfromtimestamp(timestamp).strftime(\"%Y-%m-%d\")\n        \n        # If host is not provided, it is generated based on service and region.\n        if not host:\n            host = f\"{service}.tencentcloudapi.com\"\n        \n        endpoint = f\"https://{host}\"\n        \n        # Constructing the request body\n        payload_str = json.dumps(data)\n        \n        # ************* Step 1: Concatenate the canonical request string *************\n        canonical_uri = path\n        canonical_querystring = \"\"\n        ct = \"application/json; charset=utf-8\"\n        canonical_headers = f\"content-type:{ct}\\nhost:{host}\\nx-tc-action:{headParams.get('Action', '').lower()}\\n\"\n        signed_headers = \"content-type;host;x-tc-action\"\n        hashed_request_payload = hashlib.sha256(payload_str.encode(\"utf-8\")).hexdigest()\n        \n        canonical_request = (method + \"\\n\" +\n                            canonical_uri + \"\\n\" +\n                            canonical_querystring + \"\\n\" +\n                            canonical_headers + \"\\n\" +\n                            signed_headers + \"\\n\" +\n                            hashed_request_payload)\n\n        # ************* Step 2: Construct the reception signature string *************\n        credential_scope = f\"{date}/{service}/tc3_request\"\n        hashed_canonical_request = hashlib.sha256(canonical_request.encode(\"utf-8\")).hexdigest()\n        string_to_sign = (\"TC3-HMAC-SHA256\" + \"\\n\" +\n                        str(timestamp) + \"\\n\" +\n                        credential_scope + \"\\n\" +\n                        hashed_canonical_request)\n\n        # ************* Step 3: Calculate the signature *************\n        def sign(key, msg):\n            return hmac.new(key, msg.encode(\"utf-8\"), hashlib.sha256).digest()\n\n        secret_date = sign((\"TC3\" + secret_key).encode(\"utf-8\"), date)\n        secret_service = sign(secret_date, service)\n        secret_signing = sign(secret_service, \"tc3_request\")\n        signature = hmac.new(\n            secret_signing, \n            string_to_sign.encode(\"utf-8\"), \n            hashlib.sha256\n        ).hexdigest()\n\n        # ************* Step 4: Connect Authorization *************\n        authorization = (\"TC3-HMAC-SHA256\" + \" \" +\n                        \"Credential=\" + secret_id + \"/\" + credential_scope + \", \" +\n                        \"SignedHeaders=\" + signed_headers + \", \" +\n                        \"Signature=\" + signature)\n\n        # Constructing request headers\n        headers = {\n            \"Authorization\": authorization,\n            \"Content-Type\": \"application/json; charset=utf-8\",\n            \"Host\": host,\n            \"X-TC-Action\": headParams.get(\"Action\", \"\"),\n            \"X-TC-Timestamp\": str(timestamp),\n            \"X-TC-Version\": headParams.get(\"Version\", \"\"),\n            \"X-TC-Region\": region\n        }\n\n        return headers, endpoint\n\n    def create_hunyuan_job(self, *args, **kwargs):\n        match bpy.context.scene.blendermcp_hunyuan3d_mode:\n            case \"OFFICIAL_API\":\n                return self.create_hunyuan_job_main_site(*args, **kwargs)\n            case \"LOCAL_API\":\n                return self.create_hunyuan_job_local_site(*args, **kwargs)\n            case _:\n                return f\"Error: Unknown Hunyuan3D mode!\"\n\n    def create_hunyuan_job_main_site(\n        self,\n        text_prompt: str = None,\n        image: str = None\n    ):\n        try:\n            secret_id = bpy.context.scene.blendermcp_hunyuan3d_secret_id\n            secret_key = bpy.context.scene.blendermcp_hunyuan3d_secret_key\n\n            if not secret_id or not secret_key:\n                return {\"error\": \"SecretId or SecretKey is not given\"}\n\n            # Parameter verification\n            if not text_prompt and not image:\n                return {\"error\": \"Prompt or Image is required\"}\n            if text_prompt and image:\n                return {\"error\": \"Prompt and Image cannot be provided simultaneously\"}\n            # Fixed parameter configuration\n            service = \"hunyuan\"\n            action = \"SubmitHunyuanTo3DJob\"\n            version = \"2023-09-01\"\n            region = \"ap-guangzhou\"\n\n            headParams={\n                \"Action\": action,\n                \"Version\": version,\n                \"Region\": region,\n            }\n\n            # Constructing request parameters\n            data = {\n                \"Num\": 1  # The current API limit is only 1\n            }\n\n            # Handling text prompts\n            if text_prompt:\n                if len(text_prompt) > 200:\n                    return {\"error\": \"Prompt exceeds 200 characters limit\"}\n                data[\"Prompt\"] = text_prompt\n\n            # Handling image\n            if image:\n                if re.match(r'^https?://', image, re.IGNORECASE) is not None:\n                    data[\"ImageUrl\"] = image\n                else:\n                    try:\n                        # Convert to Base64 format\n                        with open(image, \"rb\") as f:\n                            image_base64 = base64.b64encode(f.read()).decode(\"ascii\")\n                        data[\"ImageBase64\"] = image_base64\n                    except Exception as e:\n                        return {\"error\": f\"Image encoding failed: {str(e)}\"}\n            \n            # Get signed headers\n            headers, endpoint = self.get_tencent_cloud_sign_headers(\"POST\", \"/\", headParams, data, service, region, secret_id, secret_key)\n\n            response = requests.post(\n                endpoint,\n                headers = headers,\n                data = json.dumps(data)\n            )\n\n            if response.status_code == 200:\n                return response.json()\n            return {\n                \"error\": f\"API request failed with status {response.status_code}: {response}\"\n            }\n        except Exception as e:\n            return {\"error\": str(e)}\n\n    def create_hunyuan_job_local_site(\n        self,\n        text_prompt: str = None,\n        image: str = None):\n        try:\n            base_url = bpy.context.scene.blendermcp_hunyuan3d_api_url.rstrip('/')\n            octree_resolution = bpy.context.scene.blendermcp_hunyuan3d_octree_resolution\n            num_inference_steps = bpy.context.scene.blendermcp_hunyuan3d_num_inference_steps\n            guidance_scale = bpy.context.scene.blendermcp_hunyuan3d_guidance_scale\n            texture = bpy.context.scene.blendermcp_hunyuan3d_texture\n\n            if not base_url:\n                return {\"error\": \"API URL is not given\"}\n            # Parameter verification\n            if not text_prompt and not image:\n                return {\"error\": \"Prompt or Image is required\"}\n\n            # Constructing request parameters\n            data = {\n                \"octree_resolution\": octree_resolution,\n                \"num_inference_steps\": num_inference_steps,\n                \"guidance_scale\": guidance_scale,\n                \"texture\": texture,\n            }\n\n            # Handling text prompts\n            if text_prompt:\n                data[\"text\"] = text_prompt\n\n            # Handling image\n            if image:\n                if re.match(r'^https?://', image, re.IGNORECASE) is not None:\n                    try:\n                        resImg = requests.get(image)\n                        resImg.raise_for_status()\n                        image_base64 = base64.b64encode(resImg.content).decode(\"ascii\")\n                        data[\"image\"] = image_base64\n                    except Exception as e:\n                        return {\"error\": f\"Failed to download or encode image: {str(e)}\"} \n                else:\n                    try:\n                        # Convert to Base64 format\n                        with open(image, \"rb\") as f:\n                            image_base64 = base64.b64encode(f.read()).decode(\"ascii\")\n                        data[\"image\"] = image_base64\n                    except Exception as e:\n                        return {\"error\": f\"Image encoding failed: {str(e)}\"}\n\n            response = requests.post(\n                f\"{base_url}/generate\",\n                json = data,\n            )\n\n            if response.status_code != 200:\n                return {\n                    \"error\": f\"Generation failed: {response.text}\"\n                }\n        \n            # Decode base64 and save to temporary file\n            with tempfile.NamedTemporaryFile(delete=False, suffix=\".glb\") as temp_file:\n                temp_file.write(response.content)\n                temp_file_name = temp_file.name\n\n            # Import the GLB file in the main thread\n            def import_handler():\n                bpy.ops.import_scene.gltf(filepath=temp_file_name)\n                os.unlink(temp_file.name)\n                return None\n            \n            bpy.app.timers.register(import_handler)\n\n            return {\n                \"status\": \"DONE\",\n                \"message\": \"Generation and Import glb succeeded\"\n            }\n        except Exception as e:\n            print(f\"An error occurred: {e}\")\n            return {\"error\": str(e)}\n        \n    \n    def poll_hunyuan_job_status(self, *args, **kwargs):\n        return self.poll_hunyuan_job_status_ai(*args, **kwargs)\n    \n    def poll_hunyuan_job_status_ai(self, job_id: str):\n        \"\"\"Call the job status API to get the job status\"\"\"\n        print(job_id)\n        try:\n            secret_id = bpy.context.scene.blendermcp_hunyuan3d_secret_id\n            secret_key = bpy.context.scene.blendermcp_hunyuan3d_secret_key\n\n            if not secret_id or not secret_key:\n                return {\"error\": \"SecretId or SecretKey is not given\"}\n            if not job_id:\n                return {\"error\": \"JobId is required\"}\n            \n            service = \"hunyuan\"\n            action = \"QueryHunyuanTo3DJob\"\n            version = \"2023-09-01\"\n            region = \"ap-guangzhou\"\n\n            headParams={\n                \"Action\": action,\n                \"Version\": version,\n                \"Region\": region,\n            }\n\n            clean_job_id = job_id.removeprefix(\"job_\")\n            data = {\n                \"JobId\": clean_job_id\n            }\n\n            headers, endpoint = self.get_tencent_cloud_sign_headers(\"POST\", \"/\", headParams, data, service, region, secret_id, secret_key)\n\n            response = requests.post(\n                endpoint,\n                headers=headers,\n                data=json.dumps(data)\n            )\n\n            if response.status_code == 200:\n                return response.json()\n            return {\n                \"error\": f\"API request failed with status {response.status_code}: {response}\"\n            }\n        except Exception as e:\n            return {\"error\": str(e)}\n\n    def import_generated_asset_hunyuan(self, *args, **kwargs):\n        return self.import_generated_asset_hunyuan_ai(*args, **kwargs)\n            \n    def import_generated_asset_hunyuan_ai(self, name: str , zip_file_url: str):\n        if not zip_file_url:\n            return {\"error\": \"Zip file not found\"}\n        \n        # Validate URL\n        if not re.match(r'^https?://', zip_file_url, re.IGNORECASE):\n            return {\"error\": \"Invalid URL format. Must start with http:// or https://\"}\n        \n        # Create a temporary directory\n        temp_dir = tempfile.mkdtemp(prefix=\"tencent_obj_\")\n        zip_file_path = osp.join(temp_dir, \"model.zip\")\n        obj_file_path = osp.join(temp_dir, \"model.obj\")\n        mtl_file_path = osp.join(temp_dir, \"model.mtl\")\n\n        try:\n            # Download ZIP file\n            zip_response = requests.get(zip_file_url, stream=True)\n            zip_response.raise_for_status()\n            with open(zip_file_path, \"wb\") as f:\n                for chunk in zip_response.iter_content(chunk_size=8192):\n                    f.write(chunk)\n\n            # Unzip the ZIP\n            with zipfile.ZipFile(zip_file_path, \"r\") as zip_ref:\n                zip_ref.extractall(temp_dir)\n\n            # Find the .obj file (there may be multiple, assuming the main file is model.obj)\n            for file in os.listdir(temp_dir):\n                if file.endswith(\".obj\"):\n                    obj_file_path = osp.join(temp_dir, file)\n\n            if not osp.exists(obj_file_path):\n                return {\"succeed\": False, \"error\": \"OBJ file not found after extraction\"}\n\n            # Import obj file\n            if bpy.app.version>=(4, 0, 0):\n                bpy.ops.wm.obj_import(filepath=obj_file_path)\n            else:\n                bpy.ops.import_scene.obj(filepath=obj_file_path)\n\n            imported_objs = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']\n            if not imported_objs:\n                return {\"succeed\": False, \"error\": \"No mesh objects imported\"}\n\n            obj = imported_objs[0]\n            if name:\n                obj.name = name\n\n            result = {\n                \"name\": obj.name,\n                \"type\": obj.type,\n                \"location\": [obj.location.x, obj.location.y, obj.location.z],\n                \"rotation\": [obj.rotation_euler.x, obj.rotation_euler.y, obj.rotation_euler.z],\n                \"scale\": [obj.scale.x, obj.scale.y, obj.scale.z],\n            }\n\n            if obj.type == \"MESH\":\n                bounding_box = self._get_aabb(obj)\n                result[\"world_bounding_box\"] = bounding_box\n\n            return {\"succeed\": True, **result}\n        except Exception as e:\n            return {\"succeed\": False, \"error\": str(e)}\n        finally:\n            #  Clean up temporary zip and obj, save texture and mtl\n            try:\n                if os.path.exists(zip_file_path):\n                    os.remove(zip_file_path) \n                if os.path.exists(obj_file_path):\n                    os.remove(obj_file_path)\n            except Exception as e:\n                print(f\"Failed to clean up temporary directory {temp_dir}: {e}\")\n    #endregion\n\n# Blender Addon Preferences\nclass BLENDERMCP_AddonPreferences(bpy.types.AddonPreferences):\n    bl_idname = __name__\n    \n    telemetry_consent: BoolProperty(\n        name=\"Allow Telemetry\",\n        description=\"Allow collection of prompts, code snippets, and screenshots to help improve Blender MCP\",\n        default=True\n    )\n\n    def draw(self, context):\n        layout = self.layout\n        \n        # Telemetry section\n        layout.label(text=\"Telemetry & Privacy:\", icon='PREFERENCES')\n        \n        box = layout.box()\n        row = box.row()\n        row.prop(self, \"telemetry_consent\", text=\"Allow Telemetry\")\n        \n        # Info text\n        box.separator()\n        if self.telemetry_consent:\n            box.label(text=\"With consent: We collect anonymized prompts, code, and screenshots.\", icon='INFO')\n        else:\n            box.label(text=\"Without consent: We only collect minimal anonymous usage data\", icon='INFO')\n            box.label(text=\"(tool names, success/failure, duration - no prompts or code).\", icon='BLANK1')\n        box.separator()\n        box.label(text=\"All data is fully anonymized. You can change this anytime.\", icon='CHECKMARK')\n        \n        # Terms and Conditions link\n        box.separator()\n        row = box.row()\n        row.operator(\"blendermcp.open_terms\", text=\"View Terms and Conditions\", icon='TEXT')\n\n# Blender UI Panel\nclass BLENDERMCP_PT_Panel(bpy.types.Panel):\n    bl_label = \"Blender MCP\"\n    bl_idname = \"BLENDERMCP_PT_Panel\"\n    bl_space_type = 'VIEW_3D'\n    bl_region_type = 'UI'\n    bl_category = 'BlenderMCP'\n\n    def draw(self, context):\n        layout = self.layout\n        scene = context.scene\n\n        layout.prop(scene, \"blendermcp_port\")\n        layout.prop(scene, \"blendermcp_use_polyhaven\", text=\"Use assets from Poly Haven\")\n\n        layout.prop(scene, \"blendermcp_use_hyper3d\", text=\"Use Hyper3D Rodin 3D model generation\")\n        if scene.blendermcp_use_hyper3d:\n            layout.prop(scene, \"blendermcp_hyper3d_mode\", text=\"Rodin Mode\")\n            layout.prop(scene, \"blendermcp_hyper3d_api_key\", text=\"API Key\")\n            layout.operator(\"blendermcp.set_hyper3d_free_trial_api_key\", text=\"Set Free Trial API Key\")\n\n        layout.prop(scene, \"blendermcp_use_sketchfab\", text=\"Use assets from Sketchfab\")\n        if scene.blendermcp_use_sketchfab:\n            layout.prop(scene, \"blendermcp_sketchfab_api_key\", text=\"API Key\")\n\n        layout.prop(scene, \"blendermcp_use_hunyuan3d\", text=\"Use Tencent Hunyuan 3D model generation\")\n        if scene.blendermcp_use_hunyuan3d:\n            layout.prop(scene, \"blendermcp_hunyuan3d_mode\", text=\"Hunyuan3D Mode\")\n            if scene.blendermcp_hunyuan3d_mode == 'OFFICIAL_API':\n                layout.prop(scene, \"blendermcp_hunyuan3d_secret_id\", text=\"SecretId\")\n                layout.prop(scene, \"blendermcp_hunyuan3d_secret_key\", text=\"SecretKey\")\n            if scene.blendermcp_hunyuan3d_mode == 'LOCAL_API':\n                layout.prop(scene, \"blendermcp_hunyuan3d_api_url\", text=\"API URL\")\n                layout.prop(scene, \"blendermcp_hunyuan3d_octree_resolution\", text=\"Octree Resolution\")\n                layout.prop(scene, \"blendermcp_hunyuan3d_num_inference_steps\", text=\"Number of Inference Steps\")\n                layout.prop(scene, \"blendermcp_hunyuan3d_guidance_scale\", text=\"Guidance Scale\")\n                layout.prop(scene, \"blendermcp_hunyuan3d_texture\", text=\"Generate Texture\")\n        \n        if not scene.blendermcp_server_running:\n            layout.operator(\"blendermcp.start_server\", text=\"Connect to MCP server\")\n        else:\n            layout.operator(\"blendermcp.stop_server\", text=\"Disconnect from MCP server\")\n            layout.label(text=f\"Running on port {scene.blendermcp_port}\")\n\n# Operator to set Hyper3D API Key\nclass BLENDERMCP_OT_SetFreeTrialHyper3DAPIKey(bpy.types.Operator):\n    bl_idname = \"blendermcp.set_hyper3d_free_trial_api_key\"\n    bl_label = \"Set Free Trial API Key\"\n\n    def execute(self, context):\n        context.scene.blendermcp_hyper3d_api_key = RODIN_FREE_TRIAL_KEY\n        context.scene.blendermcp_hyper3d_mode = 'MAIN_SITE'\n        self.report({'INFO'}, \"API Key set successfully!\")\n        return {'FINISHED'}\n\n# Operator to start the server\nclass BLENDERMCP_OT_StartServer(bpy.types.Operator):\n    bl_idname = \"blendermcp.start_server\"\n    bl_label = \"Connect to Claude\"\n    bl_description = \"Start the BlenderMCP server to connect with Claude\"\n\n    def execute(self, context):\n        scene = context.scene\n\n        # Create a new server instance\n        if not hasattr(bpy.types, \"blendermcp_server\") or not bpy.types.blendermcp_server:\n            bpy.types.blendermcp_server = BlenderMCPServer(port=scene.blendermcp_port)\n\n        # Start the server\n        bpy.types.blendermcp_server.start()\n        scene.blendermcp_server_running = True\n\n        return {'FINISHED'}\n\n# Operator to stop the server\nclass BLENDERMCP_OT_StopServer(bpy.types.Operator):\n    bl_idname = \"blendermcp.stop_server\"\n    bl_label = \"Stop the connection to Claude\"\n    bl_description = \"Stop the connection to Claude\"\n\n    def execute(self, context):\n        scene = context.scene\n\n        # Stop the server if it exists\n        if hasattr(bpy.types, \"blendermcp_server\") and bpy.types.blendermcp_server:\n            bpy.types.blendermcp_server.stop()\n            del bpy.types.blendermcp_server\n\n        scene.blendermcp_server_running = False\n\n        return {'FINISHED'}\n\n# Operator to open Terms and Conditions\nclass BLENDERMCP_OT_OpenTerms(bpy.types.Operator):\n    bl_idname = \"blendermcp.open_terms\"\n    bl_label = \"View Terms and Conditions\"\n    bl_description = \"Open the Terms and Conditions document\"\n\n    def execute(self, context):\n        # Open the Terms and Conditions on GitHub\n        terms_url = \"https://github.com/ahujasid/blender-mcp/blob/main/TERMS_AND_CONDITIONS.md\"\n        try:\n            import webbrowser\n            webbrowser.open(terms_url)\n            self.report({'INFO'}, \"Terms and Conditions opened in browser\")\n        except Exception as e:\n            self.report({'ERROR'}, f\"Could not open Terms and Conditions: {str(e)}\")\n        \n        return {'FINISHED'}\n\n# Registration functions\ndef register():\n    bpy.types.Scene.blendermcp_port = IntProperty(\n        name=\"Port\",\n        description=\"Port for the BlenderMCP server\",\n        default=9876,\n        min=1024,\n        max=65535\n    )\n\n    bpy.types.Scene.blendermcp_server_running = bpy.props.BoolProperty(\n        name=\"Server Running\",\n        default=False\n    )\n\n    bpy.types.Scene.blendermcp_use_polyhaven = bpy.props.BoolProperty(\n        name=\"Use Poly Haven\",\n        description=\"Enable Poly Haven asset integration\",\n        default=False\n    )\n\n    bpy.types.Scene.blendermcp_use_hyper3d = bpy.props.BoolProperty(\n        name=\"Use Hyper3D Rodin\",\n        description=\"Enable Hyper3D Rodin generatino integration\",\n        default=False\n    )\n\n    bpy.types.Scene.blendermcp_hyper3d_mode = bpy.props.EnumProperty(\n        name=\"Rodin Mode\",\n        description=\"Choose the platform used to call Rodin APIs\",\n        items=[\n            (\"MAIN_SITE\", \"hyper3d.ai\", \"hyper3d.ai\"),\n            (\"FAL_AI\", \"fal.ai\", \"fal.ai\"),\n        ],\n        default=\"MAIN_SITE\"\n    )\n\n    bpy.types.Scene.blendermcp_hyper3d_api_key = bpy.props.StringProperty(\n        name=\"Hyper3D API Key\",\n        subtype=\"PASSWORD\",\n        description=\"API Key provided by Hyper3D\",\n        default=\"\"\n    )\n\n    bpy.types.Scene.blendermcp_use_hunyuan3d = bpy.props.BoolProperty(\n        name=\"Use Hunyuan 3D\",\n        description=\"Enable Hunyuan asset integration\",\n        default=False\n    )\n\n    bpy.types.Scene.blendermcp_hunyuan3d_mode = bpy.props.EnumProperty(\n        name=\"Hunyuan3D Mode\",\n        description=\"Choose a local or official APIs\",\n        items=[\n            (\"LOCAL_API\", \"local api\", \"local api\"),\n            (\"OFFICIAL_API\", \"official api\", \"official api\"),\n        ],\n        default=\"LOCAL_API\"\n    )\n\n    bpy.types.Scene.blendermcp_hunyuan3d_secret_id = bpy.props.StringProperty(\n        name=\"Hunyuan 3D SecretId\",\n        description=\"SecretId provided by Hunyuan 3D\",\n        default=\"\"\n    )\n\n    bpy.types.Scene.blendermcp_hunyuan3d_secret_key = bpy.props.StringProperty(\n        name=\"Hunyuan 3D SecretKey\",\n        subtype=\"PASSWORD\",\n        description=\"SecretKey provided by Hunyuan 3D\",\n        default=\"\"\n    )\n\n    bpy.types.Scene.blendermcp_hunyuan3d_api_url = bpy.props.StringProperty(\n        name=\"API URL\",\n        description=\"URL of the Hunyuan 3D API service\",\n        default=\"http://localhost:8081\"\n    )\n\n    bpy.types.Scene.blendermcp_hunyuan3d_octree_resolution = bpy.props.IntProperty(\n        name=\"Octree Resolution\",\n        description=\"Octree resolution for the 3D generation\",\n        default=256,\n        min=128,\n        max=512,\n    )\n\n    bpy.types.Scene.blendermcp_hunyuan3d_num_inference_steps = bpy.props.IntProperty(\n        name=\"Number of Inference Steps\",\n        description=\"Number of inference steps for the 3D generation\",\n        default=20,\n        min=20,\n        max=50,\n    )\n\n    bpy.types.Scene.blendermcp_hunyuan3d_guidance_scale = bpy.props.FloatProperty(\n        name=\"Guidance Scale\",\n        description=\"Guidance scale for the 3D generation\",\n        default=5.5,\n        min=1.0,\n        max=10.0,\n    )\n\n    bpy.types.Scene.blendermcp_hunyuan3d_texture = bpy.props.BoolProperty(\n        name=\"Generate Texture\",\n        description=\"Whether to generate texture for the 3D model\",\n        default=False,\n    )\n    \n    bpy.types.Scene.blendermcp_use_sketchfab = bpy.props.BoolProperty(\n        name=\"Use Sketchfab\",\n        description=\"Enable Sketchfab asset integration\",\n        default=False\n    )\n\n    bpy.types.Scene.blendermcp_sketchfab_api_key = bpy.props.StringProperty(\n        name=\"Sketchfab API Key\",\n        subtype=\"PASSWORD\",\n        description=\"API Key provided by Sketchfab\",\n        default=\"\"\n    )\n\n    # Register preferences class\n    bpy.utils.register_class(BLENDERMCP_AddonPreferences)\n\n    bpy.utils.register_class(BLENDERMCP_PT_Panel)\n    bpy.utils.register_class(BLENDERMCP_OT_SetFreeTrialHyper3DAPIKey)\n    bpy.utils.register_class(BLENDERMCP_OT_StartServer)\n    bpy.utils.register_class(BLENDERMCP_OT_StopServer)\n    bpy.utils.register_class(BLENDERMCP_OT_OpenTerms)\n\n    print(\"BlenderMCP addon registered\")\n\ndef unregister():\n    # Stop the server if it's running\n    if hasattr(bpy.types, \"blendermcp_server\") and bpy.types.blendermcp_server:\n        bpy.types.blendermcp_server.stop()\n        del bpy.types.blendermcp_server\n\n    bpy.utils.unregister_class(BLENDERMCP_PT_Panel)\n    bpy.utils.unregister_class(BLENDERMCP_OT_SetFreeTrialHyper3DAPIKey)\n    bpy.utils.unregister_class(BLENDERMCP_OT_StartServer)\n    bpy.utils.unregister_class(BLENDERMCP_OT_StopServer)\n    bpy.utils.unregister_class(BLENDERMCP_OT_OpenTerms)\n    bpy.utils.unregister_class(BLENDERMCP_AddonPreferences)\n\n    del bpy.types.Scene.blendermcp_port\n    del bpy.types.Scene.blendermcp_server_running\n    del bpy.types.Scene.blendermcp_use_polyhaven\n    del bpy.types.Scene.blendermcp_use_hyper3d\n    del bpy.types.Scene.blendermcp_hyper3d_mode\n    del bpy.types.Scene.blendermcp_hyper3d_api_key\n    del bpy.types.Scene.blendermcp_use_sketchfab\n    del bpy.types.Scene.blendermcp_sketchfab_api_key\n    del bpy.types.Scene.blendermcp_use_hunyuan3d\n    del bpy.types.Scene.blendermcp_hunyuan3d_mode\n    del bpy.types.Scene.blendermcp_hunyuan3d_secret_id\n    del bpy.types.Scene.blendermcp_hunyuan3d_secret_key\n    del bpy.types.Scene.blendermcp_hunyuan3d_api_url\n    del bpy.types.Scene.blendermcp_hunyuan3d_octree_resolution\n    del bpy.types.Scene.blendermcp_hunyuan3d_num_inference_steps\n    del bpy.types.Scene.blendermcp_hunyuan3d_guidance_scale\n    del bpy.types.Scene.blendermcp_hunyuan3d_texture\n\n    print(\"BlenderMCP addon unregistered\")\n\nif __name__ == \"__main__\":\n    register()\n"
  },
  {
    "path": "main.py",
    "content": "from blender_mcp.server import main as server_main\n\ndef main():\n    \"\"\"Entry point for the blender-mcp package\"\"\"\n    server_main()\n\nif __name__ == \"__main__\":\n    main()\n"
  },
  {
    "path": "pyproject.toml",
    "content": "[project]\nname = \"blender-mcp\"\nversion = \"1.5.5\"\ndescription = \"Blender integration through the Model Context Protocol\"\nreadme = \"README.md\"\nrequires-python = \">=3.10\"\nauthors = [\n    {name = \"Your Name\", email = \"your.email@example.com\"}\n]\nlicense = {text = \"MIT\"}\nclassifiers = [\n    \"Programming Language :: Python :: 3\",\n    \"License :: OSI Approved :: MIT License\",\n    \"Operating System :: OS Independent\",\n]\ndependencies = [\n    \"mcp[cli]>=1.3.0\",\n    \"supabase>=2.0.0\",\n    \"tomli>=2.0.0\",\n]\n\n[project.scripts]\nblender-mcp = \"blender_mcp.server:main\"\n\n[build-system]\nrequires = [\"setuptools>=61.0\", \"wheel\"]\nbuild-backend = \"setuptools.build_meta\"\n\n[tool.setuptools]\npackage-dir = {\"\" = \"src\"}\n\n[project.urls]\n\"Homepage\" = \"https://github.com/yourusername/blender-mcp\"\n\"Bug Tracker\" = \"https://github.com/yourusername/blender-mcp/issues\"\n"
  },
  {
    "path": "src/blender_mcp/__init__.py",
    "content": "\"\"\"Blender integration through the Model Context Protocol.\"\"\"\n\n__version__ = \"0.1.0\"\n\n# Expose key classes and functions for easier imports\nfrom .server import BlenderConnection, get_blender_connection\n"
  },
  {
    "path": "src/blender_mcp/server.py",
    "content": "# blender_mcp_server.py\nfrom mcp.server.fastmcp import FastMCP, Context, Image\nimport socket\nimport json\nimport asyncio\nimport logging\nimport tempfile\nfrom dataclasses import dataclass\nfrom contextlib import asynccontextmanager\nfrom typing import AsyncIterator, Dict, Any, List\nimport os\nfrom pathlib import Path\nimport base64\nfrom urllib.parse import urlparse\n\n# Import telemetry\nfrom .telemetry import record_startup, get_telemetry\nfrom .telemetry_decorator import telemetry_tool\n\n# Configure logging\nlogging.basicConfig(level=logging.INFO,\n                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')\nlogger = logging.getLogger(\"BlenderMCPServer\")\n\n# Default configuration\nDEFAULT_HOST = \"localhost\"\nDEFAULT_PORT = 9876\n\n@dataclass\nclass BlenderConnection:\n    host: str\n    port: int\n    sock: socket.socket = None  # Changed from 'socket' to 'sock' to avoid naming conflict\n    \n    def connect(self) -> bool:\n        \"\"\"Connect to the Blender addon socket server\"\"\"\n        if self.sock:\n            return True\n            \n        try:\n            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n            self.sock.connect((self.host, self.port))\n            logger.info(f\"Connected to Blender at {self.host}:{self.port}\")\n            return True\n        except Exception as e:\n            logger.error(f\"Failed to connect to Blender: {str(e)}\")\n            self.sock = None\n            return False\n    \n    def disconnect(self):\n        \"\"\"Disconnect from the Blender addon\"\"\"\n        if self.sock:\n            try:\n                self.sock.close()\n            except Exception as e:\n                logger.error(f\"Error disconnecting from Blender: {str(e)}\")\n            finally:\n                self.sock = None\n\n    def receive_full_response(self, sock, buffer_size=8192):\n        \"\"\"Receive the complete response, potentially in multiple chunks\"\"\"\n        chunks = []\n        # Use a consistent timeout value that matches the addon's timeout\n        sock.settimeout(180.0)  # Match the addon's timeout\n        \n        try:\n            while True:\n                try:\n                    chunk = sock.recv(buffer_size)\n                    if not chunk:\n                        # If we get an empty chunk, the connection might be closed\n                        if not chunks:  # If we haven't received anything yet, this is an error\n                            raise Exception(\"Connection closed before receiving any data\")\n                        break\n                    \n                    chunks.append(chunk)\n                    \n                    # Check if we've received a complete JSON object\n                    try:\n                        data = b''.join(chunks)\n                        json.loads(data.decode('utf-8'))\n                        # If we get here, it parsed successfully\n                        logger.info(f\"Received complete response ({len(data)} bytes)\")\n                        return data\n                    except json.JSONDecodeError:\n                        # Incomplete JSON, continue receiving\n                        continue\n                except socket.timeout:\n                    # If we hit a timeout during receiving, break the loop and try to use what we have\n                    logger.warning(\"Socket timeout during chunked receive\")\n                    break\n                except (ConnectionError, BrokenPipeError, ConnectionResetError) as e:\n                    logger.error(f\"Socket connection error during receive: {str(e)}\")\n                    raise  # Re-raise to be handled by the caller\n        except socket.timeout:\n            logger.warning(\"Socket timeout during chunked receive\")\n        except Exception as e:\n            logger.error(f\"Error during receive: {str(e)}\")\n            raise\n            \n        # If we get here, we either timed out or broke out of the loop\n        # Try to use what we have\n        if chunks:\n            data = b''.join(chunks)\n            logger.info(f\"Returning data after receive completion ({len(data)} bytes)\")\n            try:\n                # Try to parse what we have\n                json.loads(data.decode('utf-8'))\n                return data\n            except json.JSONDecodeError:\n                # If we can't parse it, it's incomplete\n                raise Exception(\"Incomplete JSON response received\")\n        else:\n            raise Exception(\"No data received\")\n\n    def send_command(self, command_type: str, params: Dict[str, Any] = None) -> Dict[str, Any]:\n        \"\"\"Send a command to Blender and return the response\"\"\"\n        if not self.sock and not self.connect():\n            raise ConnectionError(\"Not connected to Blender\")\n        \n        command = {\n            \"type\": command_type,\n            \"params\": params or {}\n        }\n        \n        try:\n            # Log the command being sent\n            logger.info(f\"Sending command: {command_type} with params: {params}\")\n            \n            # Send the command\n            self.sock.sendall(json.dumps(command).encode('utf-8'))\n            logger.info(f\"Command sent, waiting for response...\")\n            \n            # Set a timeout for receiving - use the same timeout as in receive_full_response\n            self.sock.settimeout(180.0)  # Match the addon's timeout\n            \n            # Receive the response using the improved receive_full_response method\n            response_data = self.receive_full_response(self.sock)\n            logger.info(f\"Received {len(response_data)} bytes of data\")\n            \n            response = json.loads(response_data.decode('utf-8'))\n            logger.info(f\"Response parsed, status: {response.get('status', 'unknown')}\")\n            \n            if response.get(\"status\") == \"error\":\n                logger.error(f\"Blender error: {response.get('message')}\")\n                raise Exception(response.get(\"message\", \"Unknown error from Blender\"))\n            \n            return response.get(\"result\", {})\n        except socket.timeout:\n            logger.error(\"Socket timeout while waiting for response from Blender\")\n            # Don't try to reconnect here - let the get_blender_connection handle reconnection\n            # Just invalidate the current socket so it will be recreated next time\n            self.sock = None\n            raise Exception(\"Timeout waiting for Blender response - try simplifying your request\")\n        except (ConnectionError, BrokenPipeError, ConnectionResetError) as e:\n            logger.error(f\"Socket connection error: {str(e)}\")\n            self.sock = None\n            raise Exception(f\"Connection to Blender lost: {str(e)}\")\n        except json.JSONDecodeError as e:\n            logger.error(f\"Invalid JSON response from Blender: {str(e)}\")\n            # Try to log what was received\n            if 'response_data' in locals() and response_data:\n                logger.error(f\"Raw response (first 200 bytes): {response_data[:200]}\")\n            raise Exception(f\"Invalid response from Blender: {str(e)}\")\n        except Exception as e:\n            logger.error(f\"Error communicating with Blender: {str(e)}\")\n            # Don't try to reconnect here - let the get_blender_connection handle reconnection\n            self.sock = None\n            raise Exception(f\"Communication error with Blender: {str(e)}\")\n\n@asynccontextmanager\nasync def server_lifespan(server: FastMCP) -> AsyncIterator[Dict[str, Any]]:\n    \"\"\"Manage server startup and shutdown lifecycle\"\"\"\n    # We don't need to create a connection here since we're using the global connection\n    # for resources and tools\n\n    try:\n        # Just log that we're starting up\n        logger.info(\"BlenderMCP server starting up\")\n\n        # Record startup event for telemetry\n        try:\n            record_startup()\n        except Exception as e:\n            logger.debug(f\"Failed to record startup telemetry: {e}\")\n\n        # Try to connect to Blender on startup to verify it's available\n        try:\n            # This will initialize the global connection if needed\n            blender = get_blender_connection()\n            logger.info(\"Successfully connected to Blender on startup\")\n        except Exception as e:\n            logger.warning(f\"Could not connect to Blender on startup: {str(e)}\")\n            logger.warning(\"Make sure the Blender addon is running before using Blender resources or tools\")\n\n        # Return an empty context - we're using the global connection\n        yield {}\n    finally:\n        # Clean up the global connection on shutdown\n        global _blender_connection\n        if _blender_connection:\n            logger.info(\"Disconnecting from Blender on shutdown\")\n            _blender_connection.disconnect()\n            _blender_connection = None\n        logger.info(\"BlenderMCP server shut down\")\n\n# Create the MCP server with lifespan support\nmcp = FastMCP(\n    \"BlenderMCP\",\n    lifespan=server_lifespan\n)\n\n# Resource endpoints\n\n# Global connection for resources (since resources can't access context)\n_blender_connection = None\n_polyhaven_enabled = False  # Add this global variable\n\ndef get_blender_connection():\n    \"\"\"Get or create a persistent Blender connection\"\"\"\n    global _blender_connection, _polyhaven_enabled  # Add _polyhaven_enabled to globals\n    \n    # If we have an existing connection, check if it's still valid\n    if _blender_connection is not None:\n        try:\n            # First check if PolyHaven is enabled by sending a ping command\n            result = _blender_connection.send_command(\"get_polyhaven_status\")\n            # Store the PolyHaven status globally\n            _polyhaven_enabled = result.get(\"enabled\", False)\n            return _blender_connection\n        except Exception as e:\n            # Connection is dead, close it and create a new one\n            logger.warning(f\"Existing connection is no longer valid: {str(e)}\")\n            try:\n                _blender_connection.disconnect()\n            except:\n                pass\n            _blender_connection = None\n    \n    # Create a new connection if needed\n    if _blender_connection is None:\n        host = os.getenv(\"BLENDER_HOST\", DEFAULT_HOST)\n        port = int(os.getenv(\"BLENDER_PORT\", DEFAULT_PORT))\n        _blender_connection = BlenderConnection(host=host, port=port)\n        if not _blender_connection.connect():\n            logger.error(\"Failed to connect to Blender\")\n            _blender_connection = None\n            raise Exception(\"Could not connect to Blender. Make sure the Blender addon is running.\")\n        logger.info(\"Created new persistent connection to Blender\")\n    \n    return _blender_connection\n\n\n@telemetry_tool(\"get_scene_info\")\n@mcp.tool()\ndef get_scene_info(ctx: Context) -> str:\n    \"\"\"Get detailed information about the current Blender scene\"\"\"\n    try:\n        blender = get_blender_connection()\n        result = blender.send_command(\"get_scene_info\")\n\n        # Just return the JSON representation of what Blender sent us\n        return json.dumps(result, indent=2)\n    except Exception as e:\n        logger.error(f\"Error getting scene info from Blender: {str(e)}\")\n        return f\"Error getting scene info: {str(e)}\"\n\n@telemetry_tool(\"get_object_info\")\n@mcp.tool()\ndef get_object_info(ctx: Context, object_name: str) -> str:\n    \"\"\"\n    Get detailed information about a specific object in the Blender scene.\n    \n    Parameters:\n    - object_name: The name of the object to get information about\n    \"\"\"\n    try:\n        blender = get_blender_connection()\n        result = blender.send_command(\"get_object_info\", {\"name\": object_name})\n        \n        # Just return the JSON representation of what Blender sent us\n        return json.dumps(result, indent=2)\n    except Exception as e:\n        logger.error(f\"Error getting object info from Blender: {str(e)}\")\n        return f\"Error getting object info: {str(e)}\"\n\n@telemetry_tool(\"get_viewport_screenshot\")\n@mcp.tool()\ndef get_viewport_screenshot(ctx: Context, max_size: int = 800) -> Image:\n    \"\"\"\n    Capture a screenshot of the current Blender 3D viewport.\n    \n    Parameters:\n    - max_size: Maximum size in pixels for the largest dimension (default: 800)\n    \n    Returns the screenshot as an Image.\n    \"\"\"\n    try:\n        blender = get_blender_connection()\n        \n        # Create temp file path\n        temp_dir = tempfile.gettempdir()\n        temp_path = os.path.join(temp_dir, f\"blender_screenshot_{os.getpid()}.png\")\n        \n        result = blender.send_command(\"get_viewport_screenshot\", {\n            \"max_size\": max_size,\n            \"filepath\": temp_path,\n            \"format\": \"png\"\n        })\n        \n        if \"error\" in result:\n            raise Exception(result[\"error\"])\n        \n        if not os.path.exists(temp_path):\n            raise Exception(\"Screenshot file was not created\")\n        \n        # Read the file\n        with open(temp_path, 'rb') as f:\n            image_bytes = f.read()\n        \n        # Delete the temp file\n        os.remove(temp_path)\n        \n        return Image(data=image_bytes, format=\"png\")\n        \n    except Exception as e:\n        logger.error(f\"Error capturing screenshot: {str(e)}\")\n        raise Exception(f\"Screenshot failed: {str(e)}\")\n\n\n@telemetry_tool(\"execute_blender_code\")\n@mcp.tool()\ndef execute_blender_code(ctx: Context, code: str) -> str:\n    \"\"\"\n    Execute arbitrary Python code in Blender. Make sure to do it step-by-step by breaking it into smaller chunks.\n\n    Parameters:\n    - code: The Python code to execute\n    \"\"\"\n    try:\n        # Get the global connection\n        blender = get_blender_connection()\n        result = blender.send_command(\"execute_code\", {\"code\": code})\n        return f\"Code executed successfully: {result.get('result', '')}\"\n    except Exception as e:\n        logger.error(f\"Error executing code: {str(e)}\")\n        return f\"Error executing code: {str(e)}\"\n\n@telemetry_tool(\"get_polyhaven_categories\")\n@mcp.tool()\ndef get_polyhaven_categories(ctx: Context, asset_type: str = \"hdris\") -> str:\n    \"\"\"\n    Get a list of categories for a specific asset type on Polyhaven.\n    \n    Parameters:\n    - asset_type: The type of asset to get categories for (hdris, textures, models, all)\n    \"\"\"\n    try:\n        blender = get_blender_connection()\n        if not _polyhaven_enabled:\n            return \"PolyHaven integration is disabled. Select it in the sidebar in BlenderMCP, then run it again.\"\n        result = blender.send_command(\"get_polyhaven_categories\", {\"asset_type\": asset_type})\n        \n        if \"error\" in result:\n            return f\"Error: {result['error']}\"\n        \n        # Format the categories in a more readable way\n        categories = result[\"categories\"]\n        formatted_output = f\"Categories for {asset_type}:\\n\\n\"\n        \n        # Sort categories by count (descending)\n        sorted_categories = sorted(categories.items(), key=lambda x: x[1], reverse=True)\n        \n        for category, count in sorted_categories:\n            formatted_output += f\"- {category}: {count} assets\\n\"\n        \n        return formatted_output\n    except Exception as e:\n        logger.error(f\"Error getting Polyhaven categories: {str(e)}\")\n        return f\"Error getting Polyhaven categories: {str(e)}\"\n\n@telemetry_tool(\"search_polyhaven_assets\")\n@mcp.tool()\ndef search_polyhaven_assets(\n    ctx: Context,\n    asset_type: str = \"all\",\n    categories: str = None\n) -> str:\n    \"\"\"\n    Search for assets on Polyhaven with optional filtering.\n    \n    Parameters:\n    - asset_type: Type of assets to search for (hdris, textures, models, all)\n    - categories: Optional comma-separated list of categories to filter by\n    \n    Returns a list of matching assets with basic information.\n    \"\"\"\n    try:\n        blender = get_blender_connection()\n        result = blender.send_command(\"search_polyhaven_assets\", {\n            \"asset_type\": asset_type,\n            \"categories\": categories\n        })\n        \n        if \"error\" in result:\n            return f\"Error: {result['error']}\"\n        \n        # Format the assets in a more readable way\n        assets = result[\"assets\"]\n        total_count = result[\"total_count\"]\n        returned_count = result[\"returned_count\"]\n        \n        formatted_output = f\"Found {total_count} assets\"\n        if categories:\n            formatted_output += f\" in categories: {categories}\"\n        formatted_output += f\"\\nShowing {returned_count} assets:\\n\\n\"\n        \n        # Sort assets by download count (popularity)\n        sorted_assets = sorted(assets.items(), key=lambda x: x[1].get(\"download_count\", 0), reverse=True)\n        \n        for asset_id, asset_data in sorted_assets:\n            formatted_output += f\"- {asset_data.get('name', asset_id)} (ID: {asset_id})\\n\"\n            formatted_output += f\"  Type: {['HDRI', 'Texture', 'Model'][asset_data.get('type', 0)]}\\n\"\n            formatted_output += f\"  Categories: {', '.join(asset_data.get('categories', []))}\\n\"\n            formatted_output += f\"  Downloads: {asset_data.get('download_count', 'Unknown')}\\n\\n\"\n        \n        return formatted_output\n    except Exception as e:\n        logger.error(f\"Error searching Polyhaven assets: {str(e)}\")\n        return f\"Error searching Polyhaven assets: {str(e)}\"\n\n@telemetry_tool(\"download_polyhaven_asset\")\n@mcp.tool()\ndef download_polyhaven_asset(\n    ctx: Context,\n    asset_id: str,\n    asset_type: str,\n    resolution: str = \"1k\",\n    file_format: str = None\n) -> str:\n    \"\"\"\n    Download and import a Polyhaven asset into Blender.\n    \n    Parameters:\n    - asset_id: The ID of the asset to download\n    - asset_type: The type of asset (hdris, textures, models)\n    - resolution: The resolution to download (e.g., 1k, 2k, 4k)\n    - file_format: Optional file format (e.g., hdr, exr for HDRIs; jpg, png for textures; gltf, fbx for models)\n    \n    Returns a message indicating success or failure.\n    \"\"\"\n    try:\n        blender = get_blender_connection()\n        result = blender.send_command(\"download_polyhaven_asset\", {\n            \"asset_id\": asset_id,\n            \"asset_type\": asset_type,\n            \"resolution\": resolution,\n            \"file_format\": file_format\n        })\n        \n        if \"error\" in result:\n            return f\"Error: {result['error']}\"\n        \n        if result.get(\"success\"):\n            message = result.get(\"message\", \"Asset downloaded and imported successfully\")\n            \n            # Add additional information based on asset type\n            if asset_type == \"hdris\":\n                return f\"{message}. The HDRI has been set as the world environment.\"\n            elif asset_type == \"textures\":\n                material_name = result.get(\"material\", \"\")\n                maps = \", \".join(result.get(\"maps\", []))\n                return f\"{message}. Created material '{material_name}' with maps: {maps}.\"\n            elif asset_type == \"models\":\n                return f\"{message}. The model has been imported into the current scene.\"\n            else:\n                return message\n        else:\n            return f\"Failed to download asset: {result.get('message', 'Unknown error')}\"\n    except Exception as e:\n        logger.error(f\"Error downloading Polyhaven asset: {str(e)}\")\n        return f\"Error downloading Polyhaven asset: {str(e)}\"\n\n@telemetry_tool(\"set_texture\")\n@mcp.tool()\ndef set_texture(\n    ctx: Context,\n    object_name: str,\n    texture_id: str\n) -> str:\n    \"\"\"\n    Apply a previously downloaded Polyhaven texture to an object.\n    \n    Parameters:\n    - object_name: Name of the object to apply the texture to\n    - texture_id: ID of the Polyhaven texture to apply (must be downloaded first)\n    \n    Returns a message indicating success or failure.\n    \"\"\"\n    try:\n        # Get the global connection\n        blender = get_blender_connection()\n        result = blender.send_command(\"set_texture\", {\n            \"object_name\": object_name,\n            \"texture_id\": texture_id\n        })\n        \n        if \"error\" in result:\n            return f\"Error: {result['error']}\"\n        \n        if result.get(\"success\"):\n            material_name = result.get(\"material\", \"\")\n            maps = \", \".join(result.get(\"maps\", []))\n            \n            # Add detailed material info\n            material_info = result.get(\"material_info\", {})\n            node_count = material_info.get(\"node_count\", 0)\n            has_nodes = material_info.get(\"has_nodes\", False)\n            texture_nodes = material_info.get(\"texture_nodes\", [])\n            \n            output = f\"Successfully applied texture '{texture_id}' to {object_name}.\\n\"\n            output += f\"Using material '{material_name}' with maps: {maps}.\\n\\n\"\n            output += f\"Material has nodes: {has_nodes}\\n\"\n            output += f\"Total node count: {node_count}\\n\\n\"\n            \n            if texture_nodes:\n                output += \"Texture nodes:\\n\"\n                for node in texture_nodes:\n                    output += f\"- {node['name']} using image: {node['image']}\\n\"\n                    if node['connections']:\n                        output += \"  Connections:\\n\"\n                        for conn in node['connections']:\n                            output += f\"    {conn}\\n\"\n            else:\n                output += \"No texture nodes found in the material.\\n\"\n            \n            return output\n        else:\n            return f\"Failed to apply texture: {result.get('message', 'Unknown error')}\"\n    except Exception as e:\n        logger.error(f\"Error applying texture: {str(e)}\")\n        return f\"Error applying texture: {str(e)}\"\n\n@telemetry_tool(\"get_polyhaven_status\")\n@mcp.tool()\ndef get_polyhaven_status(ctx: Context) -> str:\n    \"\"\"\n    Check if PolyHaven integration is enabled in Blender.\n    Returns a message indicating whether PolyHaven features are available.\n    \"\"\"\n    try:\n        blender = get_blender_connection()\n        result = blender.send_command(\"get_polyhaven_status\")\n        enabled = result.get(\"enabled\", False)\n        message = result.get(\"message\", \"\")\n        if enabled:\n            message += \"PolyHaven is good at Textures, and has a wider variety of textures than Sketchfab.\"\n        return message\n    except Exception as e:\n        logger.error(f\"Error checking PolyHaven status: {str(e)}\")\n        return f\"Error checking PolyHaven status: {str(e)}\"\n\n@telemetry_tool(\"get_hyper3d_status\")\n@mcp.tool()\ndef get_hyper3d_status(ctx: Context) -> str:\n    \"\"\"\n    Check if Hyper3D Rodin integration is enabled in Blender.\n    Returns a message indicating whether Hyper3D Rodin features are available.\n\n    Don't emphasize the key type in the returned message, but sliently remember it. \n    \"\"\"\n    try:\n        blender = get_blender_connection()\n        result = blender.send_command(\"get_hyper3d_status\")\n        enabled = result.get(\"enabled\", False)\n        message = result.get(\"message\", \"\")\n        if enabled:\n            message += \"\"\n        return message\n    except Exception as e:\n        logger.error(f\"Error checking Hyper3D status: {str(e)}\")\n        return f\"Error checking Hyper3D status: {str(e)}\"\n\n@telemetry_tool(\"get_sketchfab_status\")\n@mcp.tool()\ndef get_sketchfab_status(ctx: Context) -> str:\n    \"\"\"\n    Check if Sketchfab integration is enabled in Blender.\n    Returns a message indicating whether Sketchfab features are available.\n    \"\"\"\n    try:\n        blender = get_blender_connection()\n        result = blender.send_command(\"get_sketchfab_status\")\n        enabled = result.get(\"enabled\", False)\n        message = result.get(\"message\", \"\")\n        if enabled:\n            message += \"Sketchfab is good at Realistic models, and has a wider variety of models than PolyHaven.\"        \n        return message\n    except Exception as e:\n        logger.error(f\"Error checking Sketchfab status: {str(e)}\")\n        return f\"Error checking Sketchfab status: {str(e)}\"\n\n@telemetry_tool(\"search_sketchfab_models\")\n@mcp.tool()\ndef search_sketchfab_models(\n    ctx: Context,\n    query: str,\n    categories: str = None,\n    count: int = 20,\n    downloadable: bool = True\n) -> str:\n    \"\"\"\n    Search for models on Sketchfab with optional filtering.\n\n    Parameters:\n    - query: Text to search for\n    - categories: Optional comma-separated list of categories\n    - count: Maximum number of results to return (default 20)\n    - downloadable: Whether to include only downloadable models (default True)\n\n    Returns a formatted list of matching models.\n    \"\"\"\n    try:\n        blender = get_blender_connection()\n        logger.info(f\"Searching Sketchfab models with query: {query}, categories: {categories}, count: {count}, downloadable: {downloadable}\")\n        result = blender.send_command(\"search_sketchfab_models\", {\n            \"query\": query,\n            \"categories\": categories,\n            \"count\": count,\n            \"downloadable\": downloadable\n        })\n        \n        if \"error\" in result:\n            logger.error(f\"Error from Sketchfab search: {result['error']}\")\n            return f\"Error: {result['error']}\"\n        \n        # Safely get results with fallbacks for None\n        if result is None:\n            logger.error(\"Received None result from Sketchfab search\")\n            return \"Error: Received no response from Sketchfab search\"\n            \n        # Format the results\n        models = result.get(\"results\", []) or []\n        if not models:\n            return f\"No models found matching '{query}'\"\n            \n        formatted_output = f\"Found {len(models)} models matching '{query}':\\n\\n\"\n        \n        for model in models:\n            if model is None:\n                continue\n                \n            model_name = model.get(\"name\", \"Unnamed model\")\n            model_uid = model.get(\"uid\", \"Unknown ID\")\n            formatted_output += f\"- {model_name} (UID: {model_uid})\\n\"\n            \n            # Get user info with safety checks\n            user = model.get(\"user\") or {}\n            username = user.get(\"username\", \"Unknown author\") if isinstance(user, dict) else \"Unknown author\"\n            formatted_output += f\"  Author: {username}\\n\"\n            \n            # Get license info with safety checks\n            license_data = model.get(\"license\") or {}\n            license_label = license_data.get(\"label\", \"Unknown\") if isinstance(license_data, dict) else \"Unknown\"\n            formatted_output += f\"  License: {license_label}\\n\"\n            \n            # Add face count and downloadable status\n            face_count = model.get(\"faceCount\", \"Unknown\")\n            is_downloadable = \"Yes\" if model.get(\"isDownloadable\") else \"No\"\n            formatted_output += f\"  Face count: {face_count}\\n\"\n            formatted_output += f\"  Downloadable: {is_downloadable}\\n\\n\"\n        \n        return formatted_output\n    except Exception as e:\n        logger.error(f\"Error searching Sketchfab models: {str(e)}\")\n        import traceback\n        logger.error(traceback.format_exc())\n        return f\"Error searching Sketchfab models: {str(e)}\"\n\n@telemetry_tool(\"download_sketchfab_model\")\n@mcp.tool()\ndef get_sketchfab_model_preview(\n    ctx: Context,\n    uid: str\n) -> Image:\n    \"\"\"\n    Get a preview thumbnail of a Sketchfab model by its UID.\n    Use this to visually confirm a model before downloading.\n    \n    Parameters:\n    - uid: The unique identifier of the Sketchfab model (obtained from search_sketchfab_models)\n    \n    Returns the model's thumbnail as an Image for visual confirmation.\n    \"\"\"\n    try:\n        blender = get_blender_connection()\n        logger.info(f\"Getting Sketchfab model preview for UID: {uid}\")\n        \n        result = blender.send_command(\"get_sketchfab_model_preview\", {\"uid\": uid})\n        \n        if result is None:\n            raise Exception(\"Received no response from Blender\")\n        \n        if \"error\" in result:\n            raise Exception(result[\"error\"])\n        \n        # Decode base64 image data\n        image_data = base64.b64decode(result[\"image_data\"])\n        img_format = result.get(\"format\", \"jpeg\")\n        \n        # Log model info\n        model_name = result.get(\"model_name\", \"Unknown\")\n        author = result.get(\"author\", \"Unknown\")\n        logger.info(f\"Preview retrieved for '{model_name}' by {author}\")\n        \n        return Image(data=image_data, format=img_format)\n        \n    except Exception as e:\n        logger.error(f\"Error getting Sketchfab preview: {str(e)}\")\n        raise Exception(f\"Failed to get preview: {str(e)}\")\n\n\n@mcp.tool()\ndef download_sketchfab_model(\n    ctx: Context,\n    uid: str,\n    target_size: float\n) -> str:\n    \"\"\"\n    Download and import a Sketchfab model by its UID.\n    The model will be scaled so its largest dimension equals target_size.\n    \n    Parameters:\n    - uid: The unique identifier of the Sketchfab model\n    - target_size: REQUIRED. The target size in Blender units/meters for the largest dimension.\n                  You must specify the desired size for the model.\n                  Examples:\n                  - Chair: target_size=1.0 (1 meter tall)\n                  - Table: target_size=0.75 (75cm tall)\n                  - Car: target_size=4.5 (4.5 meters long)\n                  - Person: target_size=1.7 (1.7 meters tall)\n                  - Small object (cup, phone): target_size=0.1 to 0.3\n    \n    Returns a message with import details including object names, dimensions, and bounding box.\n    The model must be downloadable and you must have proper access rights.\n    \"\"\"\n    try:\n        blender = get_blender_connection()\n        logger.info(f\"Downloading Sketchfab model: {uid}, target_size={target_size}\")\n        \n        result = blender.send_command(\"download_sketchfab_model\", {\n            \"uid\": uid,\n            \"normalize_size\": True,  # Always normalize\n            \"target_size\": target_size\n        })\n        \n        if result is None:\n            logger.error(\"Received None result from Sketchfab download\")\n            return \"Error: Received no response from Sketchfab download request\"\n            \n        if \"error\" in result:\n            logger.error(f\"Error from Sketchfab download: {result['error']}\")\n            return f\"Error: {result['error']}\"\n        \n        if result.get(\"success\"):\n            imported_objects = result.get(\"imported_objects\", [])\n            object_names = \", \".join(imported_objects) if imported_objects else \"none\"\n            \n            output = f\"Successfully imported model.\\n\"\n            output += f\"Created objects: {object_names}\\n\"\n            \n            # Add dimension info if available\n            if result.get(\"dimensions\"):\n                dims = result[\"dimensions\"]\n                output += f\"Dimensions (X, Y, Z): {dims[0]:.3f} x {dims[1]:.3f} x {dims[2]:.3f} meters\\n\"\n            \n            # Add bounding box info if available\n            if result.get(\"world_bounding_box\"):\n                bbox = result[\"world_bounding_box\"]\n                output += f\"Bounding box: min={bbox[0]}, max={bbox[1]}\\n\"\n            \n            # Add normalization info if applied\n            if result.get(\"normalized\"):\n                scale = result.get(\"scale_applied\", 1.0)\n                output += f\"Size normalized: scale factor {scale:.6f} applied (target size: {target_size}m)\\n\"\n            \n            return output\n        else:\n            return f\"Failed to download model: {result.get('message', 'Unknown error')}\"\n    except Exception as e:\n        logger.error(f\"Error downloading Sketchfab model: {str(e)}\")\n        import traceback\n        logger.error(traceback.format_exc())\n        return f\"Error downloading Sketchfab model: {str(e)}\"\n\ndef _process_bbox(original_bbox: list[float] | list[int] | None) -> list[int] | None:\n    if original_bbox is None:\n        return None\n    if all(isinstance(i, int) for i in original_bbox):\n        return original_bbox\n    if any(i<=0 for i in original_bbox):\n        raise ValueError(\"Incorrect number range: bbox must be bigger than zero!\")\n    return [int(float(i) / max(original_bbox) * 100) for i in original_bbox] if original_bbox else None\n\n@telemetry_tool(\"generate_hyper3d_model_via_text\")\n@mcp.tool()\ndef generate_hyper3d_model_via_text(\n    ctx: Context,\n    text_prompt: str,\n    bbox_condition: list[float]=None\n) -> str:\n    \"\"\"\n    Generate 3D asset using Hyper3D by giving description of the desired asset, and import the asset into Blender.\n    The 3D asset has built-in materials.\n    The generated model has a normalized size, so re-scaling after generation can be useful.\n\n    Parameters:\n    - text_prompt: A short description of the desired model in **English**.\n    - bbox_condition: Optional. If given, it has to be a list of floats of length 3. Controls the ratio between [Length, Width, Height] of the model.\n\n    Returns a message indicating success or failure.\n    \"\"\"\n    try:\n        blender = get_blender_connection()\n        result = blender.send_command(\"create_rodin_job\", {\n            \"text_prompt\": text_prompt,\n            \"images\": None,\n            \"bbox_condition\": _process_bbox(bbox_condition),\n        })\n        succeed = result.get(\"submit_time\", False)\n        if succeed:\n            return json.dumps({\n                \"task_uuid\": result[\"uuid\"],\n                \"subscription_key\": result[\"jobs\"][\"subscription_key\"],\n            })\n        else:\n            return json.dumps(result)\n    except Exception as e:\n        logger.error(f\"Error generating Hyper3D task: {str(e)}\")\n        return f\"Error generating Hyper3D task: {str(e)}\"\n\n@telemetry_tool(\"generate_hyper3d_model_via_images\")\n@mcp.tool()\ndef generate_hyper3d_model_via_images(\n    ctx: Context,\n    input_image_paths: list[str]=None,\n    input_image_urls: list[str]=None,\n    bbox_condition: list[float]=None\n) -> str:\n    \"\"\"\n    Generate 3D asset using Hyper3D by giving images of the wanted asset, and import the generated asset into Blender.\n    The 3D asset has built-in materials.\n    The generated model has a normalized size, so re-scaling after generation can be useful.\n    \n    Parameters:\n    - input_image_paths: The **absolute** paths of input images. Even if only one image is provided, wrap it into a list. Required if Hyper3D Rodin in MAIN_SITE mode.\n    - input_image_urls: The URLs of input images. Even if only one image is provided, wrap it into a list. Required if Hyper3D Rodin in FAL_AI mode.\n    - bbox_condition: Optional. If given, it has to be a list of ints of length 3. Controls the ratio between [Length, Width, Height] of the model.\n\n    Only one of {input_image_paths, input_image_urls} should be given at a time, depending on the Hyper3D Rodin's current mode.\n    Returns a message indicating success or failure.\n    \"\"\"\n    if input_image_paths is not None and input_image_urls is not None:\n        return f\"Error: Conflict parameters given!\"\n    if input_image_paths is None and input_image_urls is None:\n        return f\"Error: No image given!\"\n    if input_image_paths is not None:\n        if not all(os.path.exists(i) for i in input_image_paths):\n            return \"Error: not all image paths are valid!\"\n        images = []\n        for path in input_image_paths:\n            with open(path, \"rb\") as f:\n                images.append(\n                    (Path(path).suffix, base64.b64encode(f.read()).decode(\"ascii\"))\n                )\n    elif input_image_urls is not None:\n        if not all(urlparse(i) for i in input_image_paths):\n            return \"Error: not all image URLs are valid!\"\n        images = input_image_urls.copy()\n    try:\n        blender = get_blender_connection()\n        result = blender.send_command(\"create_rodin_job\", {\n            \"text_prompt\": None,\n            \"images\": images,\n            \"bbox_condition\": _process_bbox(bbox_condition),\n        })\n        succeed = result.get(\"submit_time\", False)\n        if succeed:\n            return json.dumps({\n                \"task_uuid\": result[\"uuid\"],\n                \"subscription_key\": result[\"jobs\"][\"subscription_key\"],\n            })\n        else:\n            return json.dumps(result)\n    except Exception as e:\n        logger.error(f\"Error generating Hyper3D task: {str(e)}\")\n        return f\"Error generating Hyper3D task: {str(e)}\"\n\n@telemetry_tool(\"poll_rodin_job_status\")\n@mcp.tool()\ndef poll_rodin_job_status(\n    ctx: Context,\n    subscription_key: str=None,\n    request_id: str=None,\n):\n    \"\"\"\n    Check if the Hyper3D Rodin generation task is completed.\n\n    For Hyper3D Rodin mode MAIN_SITE:\n        Parameters:\n        - subscription_key: The subscription_key given in the generate model step.\n\n        Returns a list of status. The task is done if all status are \"Done\".\n        If \"Failed\" showed up, the generating process failed.\n        This is a polling API, so only proceed if the status are finally determined (\"Done\" or \"Canceled\").\n\n    For Hyper3D Rodin mode FAL_AI:\n        Parameters:\n        - request_id: The request_id given in the generate model step.\n\n        Returns the generation task status. The task is done if status is \"COMPLETED\".\n        The task is in progress if status is \"IN_PROGRESS\".\n        If status other than \"COMPLETED\", \"IN_PROGRESS\", \"IN_QUEUE\" showed up, the generating process might be failed.\n        This is a polling API, so only proceed if the status are finally determined (\"COMPLETED\" or some failed state).\n    \"\"\"\n    try:\n        blender = get_blender_connection()\n        kwargs = {}\n        if subscription_key:\n            kwargs = {\n                \"subscription_key\": subscription_key,\n            }\n        elif request_id:\n            kwargs = {\n                \"request_id\": request_id,\n            }\n        result = blender.send_command(\"poll_rodin_job_status\", kwargs)\n        return result\n    except Exception as e:\n        logger.error(f\"Error generating Hyper3D task: {str(e)}\")\n        return f\"Error generating Hyper3D task: {str(e)}\"\n\n@telemetry_tool(\"import_generated_asset\")\n@mcp.tool()\ndef import_generated_asset(\n    ctx: Context,\n    name: str,\n    task_uuid: str=None,\n    request_id: str=None,\n):\n    \"\"\"\n    Import the asset generated by Hyper3D Rodin after the generation task is completed.\n\n    Parameters:\n    - name: The name of the object in scene\n    - task_uuid: For Hyper3D Rodin mode MAIN_SITE: The task_uuid given in the generate model step.\n    - request_id: For Hyper3D Rodin mode FAL_AI: The request_id given in the generate model step.\n\n    Only give one of {task_uuid, request_id} based on the Hyper3D Rodin Mode!\n    Return if the asset has been imported successfully.\n    \"\"\"\n    try:\n        blender = get_blender_connection()\n        kwargs = {\n            \"name\": name\n        }\n        if task_uuid:\n            kwargs[\"task_uuid\"] = task_uuid\n        elif request_id:\n            kwargs[\"request_id\"] = request_id\n        result = blender.send_command(\"import_generated_asset\", kwargs)\n        return result\n    except Exception as e:\n        logger.error(f\"Error generating Hyper3D task: {str(e)}\")\n        return f\"Error generating Hyper3D task: {str(e)}\"\n\n@mcp.tool()\ndef get_hunyuan3d_status(ctx: Context) -> str:\n    \"\"\"\n    Check if Hunyuan3D integration is enabled in Blender.\n    Returns a message indicating whether Hunyuan3D features are available.\n\n    Don't emphasize the key type in the returned message, but silently remember it. \n    \"\"\"\n    try:\n        blender = get_blender_connection()\n        result = blender.send_command(\"get_hunyuan3d_status\")\n        message = result.get(\"message\", \"\")\n        return message\n    except Exception as e:\n        logger.error(f\"Error checking Hunyuan3D status: {str(e)}\")\n        return f\"Error checking Hunyuan3D status: {str(e)}\"\n    \n@mcp.tool()\ndef generate_hunyuan3d_model(\n    ctx: Context,\n    text_prompt: str = None,\n    input_image_url: str = None\n) -> str:\n    \"\"\"\n    Generate 3D asset using Hunyuan3D by providing either text description, image reference, \n    or both for the desired asset, and import the asset into Blender.\n    The 3D asset has built-in materials.\n    \n    Parameters:\n    - text_prompt: (Optional) A short description of the desired model in English/Chinese.\n    - input_image_url: (Optional) The local or remote url of the input image. Accepts None if only using text prompt.\n\n    Returns: \n    - When successful, returns a JSON with job_id (format: \"job_xxx\") indicating the task is in progress\n    - When the job completes, the status will change to \"DONE\" indicating the model has been imported\n    - Returns error message if the operation fails\n    \"\"\"\n    try:\n        blender = get_blender_connection()\n        result = blender.send_command(\"create_hunyuan_job\", {\n            \"text_prompt\": text_prompt,\n            \"image\": input_image_url,\n        })\n        if \"JobId\" in result.get(\"Response\", {}):\n            job_id = result[\"Response\"][\"JobId\"]\n            formatted_job_id = f\"job_{job_id}\"\n            return json.dumps({\n                \"job_id\": formatted_job_id,\n            })\n        return json.dumps(result)\n    except Exception as e:\n        logger.error(f\"Error generating Hunyuan3D task: {str(e)}\")\n        return f\"Error generating Hunyuan3D task: {str(e)}\"\n    \n@mcp.tool()\ndef poll_hunyuan_job_status(\n    ctx: Context,\n    job_id: str=None,\n):\n    \"\"\"\n    Check if the Hunyuan3D generation task is completed.\n\n    For Hunyuan3D:\n        Parameters:\n        - job_id: The job_id given in the generate model step.\n\n        Returns the generation task status. The task is done if status is \"DONE\".\n        The task is in progress if status is \"RUN\".\n        If status is \"DONE\", returns ResultFile3Ds, which is the generated ZIP model path\n        When the status is \"DONE\", the response includes a field named ResultFile3Ds that contains the generated ZIP file path of the 3D model in OBJ format.\n        This is a polling API, so only proceed if the status are finally determined (\"DONE\" or some failed state).\n    \"\"\"\n    try:\n        blender = get_blender_connection()\n        kwargs = {\n            \"job_id\": job_id,\n        }\n        result = blender.send_command(\"poll_hunyuan_job_status\", kwargs)\n        return result\n    except Exception as e:\n        logger.error(f\"Error generating Hunyuan3D task: {str(e)}\")\n        return f\"Error generating Hunyuan3D task: {str(e)}\"\n\n@mcp.tool()\ndef import_generated_asset_hunyuan(\n    ctx: Context,\n    name: str,\n    zip_file_url: str,\n):\n    \"\"\"\n    Import the asset generated by Hunyuan3D after the generation task is completed.\n\n    Parameters:\n    - name: The name of the object in scene\n    - zip_file_url: The zip_file_url given in the generate model step.\n\n    Return if the asset has been imported successfully.\n    \"\"\"\n    try:\n        blender = get_blender_connection()\n        kwargs = {\n            \"name\": name\n        }\n        if zip_file_url:\n            kwargs[\"zip_file_url\"] = zip_file_url\n        result = blender.send_command(\"import_generated_asset_hunyuan\", kwargs)\n        return result\n    except Exception as e:\n        logger.error(f\"Error generating Hunyuan3D task: {str(e)}\")\n        return f\"Error generating Hunyuan3D task: {str(e)}\"\n\n\n@mcp.prompt()\ndef asset_creation_strategy() -> str:\n    \"\"\"Defines the preferred strategy for creating assets in Blender\"\"\"\n    return \"\"\"When creating 3D content in Blender, always start by checking if integrations are available:\n\n    0. Before anything, always check the scene from get_scene_info()\n    1. First use the following tools to verify if the following integrations are enabled:\n        1. PolyHaven\n            Use get_polyhaven_status() to verify its status\n            If PolyHaven is enabled:\n            - For objects/models: Use download_polyhaven_asset() with asset_type=\"models\"\n            - For materials/textures: Use download_polyhaven_asset() with asset_type=\"textures\"\n            - For environment lighting: Use download_polyhaven_asset() with asset_type=\"hdris\"\n        2. Sketchfab\n            Sketchfab is good at Realistic models, and has a wider variety of models than PolyHaven.\n            Use get_sketchfab_status() to verify its status\n            If Sketchfab is enabled:\n            - For objects/models: First search using search_sketchfab_models() with your query\n            - Then download specific models using download_sketchfab_model() with the UID\n            - Note that only downloadable models can be accessed, and API key must be properly configured\n            - Sketchfab has a wider variety of models than PolyHaven, especially for specific subjects\n        3. Hyper3D(Rodin)\n            Hyper3D Rodin is good at generating 3D models for single item.\n            So don't try to:\n            1. Generate the whole scene with one shot\n            2. Generate ground using Hyper3D\n            3. Generate parts of the items separately and put them together afterwards\n\n            Use get_hyper3d_status() to verify its status\n            If Hyper3D is enabled:\n            - For objects/models, do the following steps:\n                1. Create the model generation task\n                    - Use generate_hyper3d_model_via_images() if image(s) is/are given\n                    - Use generate_hyper3d_model_via_text() if generating 3D asset using text prompt\n                    If key type is free_trial and insufficient balance error returned, tell the user that the free trial key can only generated limited models everyday, they can choose to:\n                    - Wait for another day and try again\n                    - Go to hyper3d.ai to find out how to get their own API key\n                    - Go to fal.ai to get their own private API key\n                2. Poll the status\n                    - Use poll_rodin_job_status() to check if the generation task has completed or failed\n                3. Import the asset\n                    - Use import_generated_asset() to import the generated GLB model the asset\n                4. After importing the asset, ALWAYS check the world_bounding_box of the imported mesh, and adjust the mesh's location and size\n                    Adjust the imported mesh's location, scale, rotation, so that the mesh is on the right spot.\n\n                You can reuse assets previous generated by running python code to duplicate the object, without creating another generation task.\n        4. Hunyuan3D\n            Hunyuan3D is good at generating 3D models for single item.\n            So don't try to:\n            1. Generate the whole scene with one shot\n            2. Generate ground using Hunyuan3D\n            3. Generate parts of the items separately and put them together afterwards\n\n            Use get_hunyuan3d_status() to verify its status\n            If Hunyuan3D is enabled:\n                if Hunyuan3D mode is \"OFFICIAL_API\":\n                    - For objects/models, do the following steps:\n                        1. Create the model generation task\n                            - Use generate_hunyuan3d_model by providing either a **text description** OR an **image(local or urls) reference**.\n                            - Go to cloud.tencent.com out how to get their own SecretId and SecretKey\n                        2. Poll the status\n                            - Use poll_hunyuan_job_status() to check if the generation task has completed or failed\n                        3. Import the asset\n                            - Use import_generated_asset_hunyuan() to import the generated OBJ model the asset\n                    if Hunyuan3D mode is \"LOCAL_API\":\n                        - For objects/models, do the following steps:\n                        1. Create the model generation task\n                            - Use generate_hunyuan3d_model if image (local or urls)  or text prompt is given and import the asset\n\n                You can reuse assets previous generated by running python code to duplicate the object, without creating another generation task.\n\n    3. Always check the world_bounding_box for each item so that:\n        - Ensure that all objects that should not be clipping are not clipping.\n        - Items have right spatial relationship.\n    \n    4. Recommended asset source priority:\n        - For specific existing objects: First try Sketchfab, then PolyHaven\n        - For generic objects/furniture: First try PolyHaven, then Sketchfab\n        - For custom or unique items not available in libraries: Use Hyper3D Rodin or Hunyuan3D\n        - For environment lighting: Use PolyHaven HDRIs\n        - For materials/textures: Use PolyHaven textures\n\n    Only fall back to scripting when:\n    - PolyHaven, Sketchfab, Hyper3D, and Hunyuan3D are all disabled\n    - A simple primitive is explicitly requested\n    - No suitable asset exists in any of the libraries\n    - Hyper3D Rodin or Hunyuan3D failed to generate the desired asset\n    - The task specifically requires a basic material/color\n    \"\"\"\n\n# Main execution\n\ndef main():\n    \"\"\"Run the MCP server\"\"\"\n    mcp.run()\n\nif __name__ == \"__main__\":\n    main()"
  },
  {
    "path": "src/blender_mcp/telemetry.py",
    "content": "\"\"\"\nPrivacy-focused, anonymous telemetry for Blender MCP\nTracks tool usage, DAU/MAU, and performance metrics\n\"\"\"\n\nimport contextlib\nimport json\nimport logging\nimport os\nimport platform\nimport queue\nimport sys\nimport threading\nimport time\nimport uuid\nfrom dataclasses import dataclass\nfrom enum import Enum\nfrom pathlib import Path\nfrom typing import Any\n\ntry:\n    from supabase import create_client, Client\n    HAS_SUPABASE = True\nexcept ImportError:\n    HAS_SUPABASE = False\n\ntry:\n    import tomli\nexcept ImportError:\n    try:\n        import tomllib as tomli\n    except ImportError:\n        tomli = None\n\nlogger = logging.getLogger(\"blender-mcp-telemetry\")\n\n\ndef get_package_version() -> str:\n    \"\"\"Get version from pyproject.toml\"\"\"\n    try:\n        pyproject_path = Path(__file__).parent.parent.parent.parent / \"pyproject.toml\"\n        if pyproject_path.exists():\n            if tomli:\n                with open(pyproject_path, \"rb\") as f:\n                    data = tomli.load(f)\n                    return data[\"project\"][\"version\"]\n    except Exception:\n        pass\n    return \"unknown\"\n\n\nMCP_VERSION = get_package_version()\n\n\nclass EventType(str, Enum):\n    \"\"\"Types of telemetry events\"\"\"\n    STARTUP = \"startup\"\n    TOOL_EXECUTION = \"tool_execution\"\n    PROMPT_SENT = \"prompt_sent\"\n    CONNECTION = \"connection\"\n    ERROR = \"error\"\n\n\n@dataclass\nclass TelemetryEvent:\n    \"\"\"Structure for telemetry events\"\"\"\n    event_type: EventType\n    customer_uuid: str\n    session_id: str\n    timestamp: float\n    version: str\n    platform: str\n\n    # Optional fields\n    tool_name: str | None = None\n    prompt_text: str | None = None\n    success: bool = True\n    duration_ms: float | None = None\n    error_message: str | None = None\n    blender_version: str | None = None\n    metadata: dict[str, Any] | None = None\n\n\nclass TelemetryCollector:\n    \"\"\"Main telemetry collection class\"\"\"\n\n    def __init__(self):\n        \"\"\"Initialize telemetry collector\"\"\"\n        # Import config here to avoid circular imports\n        from .config import telemetry_config\n        self.config = telemetry_config\n\n        # Check if disabled via environment variables\n        if self._is_disabled():\n            self.config.enabled = False\n            logger.warning(\"Telemetry disabled via environment variable\")\n\n        # Generate or load customer UUID\n        self._customer_uuid: str = self._get_or_create_uuid()\n        self._session_id: str = str(uuid.uuid4())\n\n        # Rate limiting tracking\n        self._event_timestamps: list[float] = []\n        self._rate_limit_lock = threading.Lock()\n\n        # Background queue and worker\n        self._queue: \"queue.Queue[TelemetryEvent]\" = queue.Queue(maxsize=1000)\n        self._worker: threading.Thread = threading.Thread(\n            target=self._worker_loop, daemon=True\n        )\n        self._worker.start()\n\n        logger.warning(f\"Telemetry initialized (enabled={self.config.enabled}, has_supabase={HAS_SUPABASE}, customer_uuid={self._customer_uuid})\")\n\n    def _is_disabled(self) -> bool:\n        \"\"\"Check if telemetry is disabled via environment variables\"\"\"\n        disable_vars = [\n            \"DISABLE_TELEMETRY\",\n            \"BLENDER_MCP_DISABLE_TELEMETRY\",\n            \"MCP_DISABLE_TELEMETRY\"\n        ]\n\n        for var in disable_vars:\n            if os.environ.get(var, \"\").lower() in (\"true\", \"1\", \"yes\", \"on\"):\n                return True\n        return False\n\n    def _get_data_directory(self) -> Path:\n        \"\"\"Get directory for storing telemetry data\"\"\"\n        if sys.platform == \"win32\":\n            base_dir = Path(os.environ.get('APPDATA', Path.home() / 'AppData' / 'Roaming'))\n        elif sys.platform == \"darwin\":\n            base_dir = Path.home() / 'Library' / 'Application Support'\n        else:  # Linux\n            base_dir = Path(os.environ.get('XDG_DATA_HOME', Path.home() / '.local' / 'share'))\n\n        data_dir = base_dir / 'BlenderMCP'\n        data_dir.mkdir(parents=True, exist_ok=True)\n        return data_dir\n\n    def _get_or_create_uuid(self) -> str:\n        \"\"\"Get or create anonymous customer UUID\"\"\"\n        try:\n            data_dir = self._get_data_directory()\n            uuid_file = data_dir / \"customer_uuid.txt\"\n\n            if uuid_file.exists():\n                customer_uuid = uuid_file.read_text(encoding=\"utf-8\").strip()\n                if customer_uuid:\n                    return customer_uuid\n\n            # Create new UUID\n            customer_uuid = str(uuid.uuid4())\n            uuid_file.write_text(customer_uuid, encoding=\"utf-8\")\n\n            # Set restrictive permissions on Unix\n            if sys.platform != \"win32\":\n                os.chmod(uuid_file, 0o600)\n\n            return customer_uuid\n        except Exception as e:\n            logger.debug(f\"Failed to persist UUID: {e}\")\n            return str(uuid.uuid4())\n\n    def _check_user_consent(self) -> bool:\n        \"\"\"Check if user has consented to prompt collection via Blender addon\"\"\"\n        try:\n            # Import here to avoid circular dependency\n            from .server import get_blender_connection\n            blender = get_blender_connection()\n            result = blender.send_command(\"get_telemetry_consent\", {})\n            consent = result.get(\"consent\", False)\n            return consent\n        except Exception as e:\n            # Default to False if we can't check (user hasn't given consent or Blender not connected)\n            return False\n\n    def record_event(\n        self,\n        event_type: EventType,\n        tool_name: str | None = None,\n        prompt_text: str | None = None,\n        success: bool = True,\n        duration_ms: float | None = None,\n        error_message: str | None = None,\n        blender_version: str | None = None,\n        metadata: dict[str, Any] | None = None\n    ):\n        \"\"\"Record a telemetry event (non-blocking)\"\"\"\n        if not self.config.enabled:\n            logger.warning(f\"Telemetry disabled, skipping event: {event_type}\")\n            return\n        if not HAS_SUPABASE:\n            logger.warning(f\"Supabase not available, skipping event: {event_type}\")\n            return\n\n        logger.warning(f\"Recording telemetry event: {event_type}, tool={tool_name}\")\n\n        # Check user consent for private data collection\n        user_consent = self._check_user_consent()\n        \n        if not user_consent:\n            # Without consent, only collect minimal anonymous usage data:\n            # - Session startup events\n            # - Tool execution events (tool name, success, duration)\n            # - Basic session info for DAU/MAU calculation\n            # Remove all private information:\n            prompt_text = None  # No user prompts\n            metadata = None  # No code snippets, params, screenshots, scene info\n            # Keep error_message for debugging, but sanitize it\n            if error_message:\n                # Only keep generic error type, not specific details\n                error_message = \"Error occurred (details withheld without consent)\"\n\n        # Truncate prompt if needed (only if consent was given)\n        if prompt_text and len(prompt_text) > self.config.max_prompt_length:\n            prompt_text = prompt_text[:self.config.max_prompt_length] + \"...\"\n\n        # Truncate error messages (only if consent was given and not already sanitized)\n        if error_message and user_consent and len(error_message) > 200:\n            error_message = error_message[:200] + \"...\"\n\n        event = TelemetryEvent(\n            event_type=event_type,\n            customer_uuid=self._customer_uuid,\n            session_id=self._session_id,\n            timestamp=time.time(),\n            version=MCP_VERSION,\n            platform=platform.system().lower(),\n            tool_name=tool_name,\n            prompt_text=prompt_text,\n            success=success,\n            duration_ms=duration_ms,\n            error_message=error_message,\n            blender_version=blender_version,\n            metadata=metadata\n        )\n\n        # Enqueue for background worker\n        try:\n            self._queue.put_nowait(event)\n        except queue.Full:\n            logger.debug(\"Telemetry queue full, dropping event\")\n\n    def _worker_loop(self):\n        \"\"\"Background worker that sends telemetry\"\"\"\n        while True:\n            event = self._queue.get()\n            try:\n                self._send_event(event)\n            except Exception as e:\n                logger.debug(f\"Telemetry send failed: {e}\")\n            finally:\n                with contextlib.suppress(Exception):\n                    self._queue.task_done()\n\n    def _send_event(self, event: TelemetryEvent):\n        \"\"\"Send event to Supabase\"\"\"\n        if not HAS_SUPABASE:\n            return\n\n        try:\n            # Create Supabase client with explicit options\n            from supabase import ClientOptions\n\n            options = ClientOptions(\n                auto_refresh_token=False,\n                persist_session=False\n            )\n\n            supabase: Client = create_client(\n                self.config.supabase_url,\n                self.config.supabase_anon_key,\n                options=options\n            )\n\n            # Prepare data for insertion\n            data = {\n                \"customer_uuid\": event.customer_uuid,\n                \"session_id\": event.session_id,\n                \"event_type\": event.event_type.value,\n                \"tool_name\": event.tool_name,\n                \"prompt_text\": event.prompt_text,\n                \"success\": event.success,\n                \"duration_ms\": event.duration_ms,\n                \"error_message\": event.error_message,\n                \"version\": event.version,\n                \"platform\": event.platform,\n                \"blender_version\": event.blender_version,\n                \"metadata\": event.metadata or {},\n                \"event_timestamp\": int(event.timestamp),\n            }\n\n            response = supabase.table(\"telemetry_events\").insert(data, returning=\"minimal\").execute()\n            logger.debug(f\"Telemetry sent: {event.event_type}\")\n\n        except Exception as e:\n            logger.debug(f\"Failed to send telemetry: {e}\")\n\n\n# Global telemetry instance\n_telemetry_collector: TelemetryCollector | None = None\n\n\ndef get_telemetry() -> TelemetryCollector:\n    \"\"\"Get the global telemetry collector instance\"\"\"\n    global _telemetry_collector\n    if _telemetry_collector is None:\n        _telemetry_collector = TelemetryCollector()\n    return _telemetry_collector\n\n\ndef record_tool_usage(\n    tool_name: str,\n    success: bool,\n    duration_ms: float,\n    error: str | None = None\n):\n    \"\"\"Convenience function to record tool usage\"\"\"\n    get_telemetry().record_event(\n        event_type=EventType.TOOL_EXECUTION,\n        tool_name=tool_name,\n        success=success,\n        duration_ms=duration_ms,\n        error_message=error\n    )\n\n\ndef record_startup(blender_version: str | None = None):\n    \"\"\"Record server startup event\"\"\"\n    get_telemetry().record_event(\n        event_type=EventType.STARTUP,\n        blender_version=blender_version\n    )\n\n\ndef is_telemetry_enabled() -> bool:\n    \"\"\"Check if telemetry is enabled\"\"\"\n    try:\n        return get_telemetry().config.enabled\n    except Exception:\n        return False\n"
  },
  {
    "path": "src/blender_mcp/telemetry_decorator.py",
    "content": "\"\"\"\nTelemetry decorator for Blender MCP tools\n\"\"\"\n\nimport functools\nimport inspect\nimport logging\nimport time\nfrom typing import Callable, Any\n\nfrom .telemetry import record_tool_usage\n\nlogger = logging.getLogger(\"blender-mcp-telemetry\")\n\n\ndef telemetry_tool(tool_name: str):\n    \"\"\"Decorator to add telemetry tracking to MCP tools\"\"\"\n    def decorator(func: Callable) -> Callable:\n        @functools.wraps(func)\n        def sync_wrapper(*args, **kwargs) -> Any:\n            start_time = time.time()\n            success = False\n            error = None\n\n            try:\n                result = func(*args, **kwargs)\n                success = True\n                return result\n            except Exception as e:\n                error = str(e)\n                raise\n            finally:\n                duration_ms = (time.time() - start_time) * 1000\n                try:\n                    record_tool_usage(tool_name, success, duration_ms, error)\n                except Exception as log_error:\n                    logger.debug(f\"Failed to record telemetry: {log_error}\")\n\n        @functools.wraps(func)\n        async def async_wrapper(*args, **kwargs) -> Any:\n            start_time = time.time()\n            success = False\n            error = None\n\n            try:\n                result = await func(*args, **kwargs)\n                success = True\n                return result\n            except Exception as e:\n                error = str(e)\n                raise\n            finally:\n                duration_ms = (time.time() - start_time) * 1000\n                try:\n                    record_tool_usage(tool_name, success, duration_ms, error)\n                except Exception as log_error:\n                    logger.debug(f\"Failed to record telemetry: {log_error}\")\n\n        # Return appropriate wrapper based on function type\n        if inspect.iscoroutinefunction(func):\n            return async_wrapper\n        else:\n            return sync_wrapper\n\n    return decorator\n"
  }
]