Skip to main content
Glama

OpenSCAD MCP Server

by jhacksman
wrapper.cpython-312.pyc15.1 kB
� ��g�,��x�ddlZddlZddlZddlZddlmZmZmZmZm Z eje �Z Gd�d�Z y)�N)�Dict�Any�List�Tuple�Optionalc �H�eZdZdZdedefd�Zdedeeefdefd�Zd edeeefdefd �Z dd ede eeefdefd �Z dd ede eeefdededef d�Z dedefd�Z deeefdefd�Zdeeefdefd�Zdeeefdefd�Zdeeefdefd�Zdeeefdefd�Zy )�OpenSCADWrapperz� Wrapper for OpenSCAD command-line interface. Provides methods to generate SCAD code, STL files, and preview images. �scad_dir� output_dirc���||_||_tjj |d�|_tjj |d�|_tj|jd��tj|j d��tj|j d��|j|j|j|j|jd�|_ y)z� Initialize the OpenSCAD wrapper. Args: scad_dir: Directory to store SCAD files output_dir: Directory to store output files (STL, PNG) �stl�previewT)�exist_ok)�cube�sphere�cylinder�box� rounded_boxN)r r �os�path�join�stl_dir� preview_dir�makedirs�_cube_template�_sphere_template�_cylinder_template� _box_template�_rounded_box_template�shape_templates)�selfr r s �@/home/ubuntu/openscad-mcp-server/src/openscad_wrapper/wrapper.py�__init__zOpenSCADWrapper.__init__s���!�� �$����w�w�|�|�J��6�� ��7�7�<�<� �I�>��� � � �D�M�M�D�1� � � �D�L�L�4�0� � � �D�$�$�t�4��'�'��+�+��/�/��%�%��5�5�  ���� model_type� parameters�returnc��ttj��}tjj |j |�d��}|jj|�}|std|����||�}t|d�5}|j|�ddd�tjd|���|S#1swY�#xYw)a@ Generate OpenSCAD code for a given model type and parameters. Args: model_type: Type of model to generate (cube, sphere, cylinder, etc.) parameters: Dictionary of parameters for the model Returns: Path to the generated SCAD file �.scadzUnsupported model type: �wNzGenerated SCAD file: )�str�uuid�uuid4rrrr r �get� ValueError�open�write�logger�info)r!r%r&�model_id� scad_file� template_func� scad_code�fs r"�generate_scad_codez"OpenSCADWrapper.generate_scad_code*s����t�z�z�|�$���G�G�L�L����8�*�E�0B�C� ��,�,�0�0��<� ���7� �|�D�E� E�"�*�-� ��)�S� !�Q� �G�G�I� �"� � � �+�I�;�7�8��� "� !�s � B>�>Cr4c�F�tjj|j|�d��}tjj |�st d|����t |d�5}|j�}ddd�d}|jD]}|j�vs�|}n|s td��|j||�}t |d�5}|j|�ddd�tjd|���|S#1swY��xYw#1swY�/xYw)a Update an existing SCAD file with new parameters. Args: model_id: ID of the model to update parameters: New parameters for the model Returns: Path to the updated SCAD file r)zSCAD file not found: �rNz6Could not determine model type from existing SCAD filer*zUpdated SCAD file: )rrrr �exists�FileNotFoundErrorr0�readr �lowerr/r1r2r3) r!r4r&r5r8r7r%� shape_type� new_scad_codes r"�update_scad_codez OpenSCADWrapper.update_scad_codeGs ���G�G�L�L����8�*�E�0B�C� ��w�w�~�~�i�(�#�&;�I�;�$G�H� H��)�S� !�Q�����I�"�� ��.�.�J��Y�_�_�.�.�'� ��/� ��U�V� V�9��,�,�Z�8��D� ��)�S� !�Q� �G�G�M� "�"� � � �)�)��5�6���+"� !��""� !�s�'D �D� D�D Nr5c��tjj|�jd�d}tjj |j |�d��}dd|g}|r0|j �D]\}}|jd|�d|��g��|j|� tj|ddd� �}tjd |���tj|j�|S#tj$r?} tj!d | j"���t%d | j"����d } ~ wwxYw)a Generate an STL file from a SCAD file. Args: scad_file: Path to the SCAD file parameters: Optional parameters to override in the SCAD file Returns: Path to the generated STL file �.rz.stl�openscad�-o�-D�=T��check�capture_output�textzGenerated STL file: zError generating STL file: zFailed to generate STL file: N)rr�basename�splitrr�items�extend�append� subprocess�runr2r3�debug�stdout�CalledProcessError�error�stderr� RuntimeError) r!r5r&r4�stl_file�cmd�key�value�result�es r"� generate_stlzOpenSCADWrapper.generate_stlns%���7�7�#�#�I�.�4�4�S�9�!�<���7�7�<�<�� � �� �$�.?�@���4��*�� �(�.�.�0� ��U�� � �D�S�E��5�'�"2�3�4�1� � � �9�� K��^�^�C�t�D�t�T�F� �K�K�.�x�j�9� :� �L�L���� '��O���,�,� K� �L�L�6�q�x�x�j�A� B��!>�q�x�x�j�I�J� J�� K�s�(AC:�:E � :E�E �camera_position� image_sizec���tjj|�jd�d}tjj |j |�d��}dd|d|d|g}|r0|j �D]\}} |jd|�d | ��g��|j|� tj|d d d � �} tjd |���tj| j�|S#tj$rR} tj!d | j"���tj%d�|j'|�cYd} ~ Sd} ~ wwxYw)a� Generate a preview image from a SCAD file. Args: scad_file: Path to the SCAD file parameters: Optional parameters to override in the SCAD file camera_position: Camera position in format "tx,ty,tz,rx,ry,rz,dist" image_size: Image size in format "width,height" Returns: Path to the generated preview image rDrz.pngrEz--cameraz --imgsizerFrGrHTrIzGenerated preview image: z Error generating preview image: z.Using placeholder image due to rendering errorN)rrrMrNrrrOrPrQrRrSr2r3rTrUrVrWrX�warning�_create_placeholder_image) r!r5r&rarbr4� preview_filer[r\r]r^r_s r"�generate_previewz OpenSCADWrapper.generate_preview�s9���7�7�#�#�I�.�4�4�S�9�!�<���w�w�|�|�D�$4�$4�� �$�6G�H� ��:�� �Z�QU�Wc�d�� �(�.�.�0� ��U�� � �D�S�E��5�'�"2�3�4�1� � � �9�� @��^�^�C�t�D�t�T�F� �K�K�3�L�>�B� C� �L�L���� '�� ���,�,� @� �L�L�;�A�H�H�:�F� G� �N�N�K� L��1�1�,�?� ?��  @�s �,AC>�>E#�AE�E#�E#� output_pathc�"� ddlm}m}m}|j ddd��}|j |�}|j ddd � �|j|�|S#t$r-}tjd t|����|cYd }~Sd }~wwxYw) z7Create a simple placeholder image when rendering fails.r)�Image� ImageDraw� ImageFont�RGB)i iX)��rnrn)�color)i�i,zPreview not available)rrr)�fillz"Error creating placeholder image: N) �PILrjrkrl�new�DrawrL�save� Exceptionr2rWr+)r!rhrjrkrl�img�drawr_s r"rez)OpenSCADWrapper._create_placeholder_image�s��� � 7� 7��)�)�E�:�_�)�E�C��>�>�#�&�D� �I�I�j�"9� �I� J� �H�H�[� !�� ��� � �L�L�=�c�!�f�X�F� G�� �� �s�AA� B�!"B �B� B�paramsc�F�|jdd�}|jdd�}|jdd�}|jdd�j�dk(}d|�d |�d |�d t|�j��d |�d |�d|�dt|�j��d�S)zGenerate SCAD code for a cube.�width� �depth�height�center�false�truez$// Cube // Parameters: // width = � // depth = � // height = � // center = � width = � ; depth = � ; height = � ; center = z0; cube([width, depth, height], center=center); �r.r?r+)r!rx�size_x�size_y�size_zr~s r"rzOpenSCADWrapper._cube_template�s������G�R�(�����G�R�(�����H�b�)�����H�g�.�4�4�6�&�@����X���X���h���6�{� � �"�#�$ � �� � �� � �� � �f�+� � � � �� � r$c �h�|jdd�}|jdd�}d|�d|�d|�d|�d � S) z Generate SCAD code for a sphere.�radiusr{�segments� z'// Sphere // Parameters: // radius = � // segments = � radius = �; $fn = z; sphere(r=radius); �r.)r!rxr�r�s r"rz OpenSCADWrapper._sphere_template�sY�����H�b�)���:�:�j�"�-����h��� � � ����j�� � r$c�F�|jdd�}|jdd�}|jdd�j�dk(}|jdd �}d |�d |�d t|�j��d |�d|�d|�dt|�j��d|�d�S)z"Generate SCAD code for a cylinder.r�r{r}�r~rr�r�r�z)// Cylinder // Parameters: // radius = r�r�r�r�r�r�r�z0; cylinder(h=height, r=radius, center=center); r�)r!rxr�r}r~r�s r"rz"OpenSCADWrapper._cylinder_template�s������H�b�)�����H�b�)�����H�g�.�4�4�6�&�@���:�:�j�"�-����h���h���6�{� � �"�#�$�� � � �� � �� � �f�+� � � � ���j�� � r$c���|jdd�}|jdd�}|jdd�}|jdd�}d |�d |�d |�d |�d |�d|�d|�d|�d�S)z$Generate SCAD code for a hollow box.rz�r|r�r}�� thickness�z*// Hollow Box // Parameters: // width = r�r�z // thickness = r�r�r�z; thickness = a; module box(width, depth, height, thickness) { difference() { cube([width, depth, height]); translate([thickness, thickness, thickness]) cube([width - 2*thickness, depth - 2*thickness, height - thickness]); } } box(width, depth, height, thickness); r�)r!rxrzr|r}r�s r"rzOpenSCADWrapper._box_template s���� � �7�B�'��� � �7�B�'�����H�b�)���J�J�{�A�.� ���W���W���h���� � �� � �� � �� � �K� �� r$c���|jdd�}|jdd�}|jdd�}|jdd�}|jd d �}d |�d |�d |�d|�d|�d|�d|�d|�d|�d|�d�S)z%Generate SCAD code for a rounded box.rzr�r|r�r}r�r��r�r�z+// Rounded Box // Parameters: // width = r�r�z // radius = r�r�r�r�z ; radius = r�a2; module rounded_box(width, depth, height, radius) { hull() { translate([radius, radius, radius]) sphere(r=radius); translate([width-radius, radius, radius]) sphere(r=radius); translate([radius, depth-radius, radius]) sphere(r=radius); translate([width-radius, depth-radius, radius]) sphere(r=radius); translate([radius, radius, height-radius]) sphere(r=radius); translate([width-radius, radius, height-radius]) sphere(r=radius); translate([radius, depth-radius, height-radius]) sphere(r=radius); translate([width-radius, depth-radius, height-radius]) sphere(r=radius); } } rounded_box(width, depth, height, radius); r�)r!rxrzr|r}r�r�s r"rz%OpenSCADWrapper._rounded_box_template*s���� � �7�B�'��� � �7�B�'�����H�b�)�����H�a�(���:�:�j�"�-����W���W���h���h��� � � �� � �� � �� � ����j��+�+ r$)N)Nz0,0,0,0,0,0,50z800,600)�__name__� __module__� __qualname__�__doc__r+r#rrr9rBrr`rgrerrrrr�r$r"r r ss���  �� �#� �6�S��d�3��8�n��QT��:%��%�$�s�C�x�.�%�S�%�N!K�c�!K�x��S�#�X��7O�!K�[^�!K�FW[�/?�*3�'@�#�'@�8�D��c��N�;S�'@�),�'@�$'�'@�8;�'@�R�S��S��,�T�#�s�(�^����,�t�C��H�~��#�� ��c�3�h���C��,�D��c��N��s��<3�D��c��N�3�s�3r$r )rrRr,�logging�typingrrrrr� getLoggerr�r2r r�r$r"�<module>r�s5�� �� ��3�3� �� � �8� $��T�Tr$

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jhacksman/OpenSCAD-MCP-Server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server