Website powered by

Maya - Python - Paste Image From Clipboard

General / 12 March 2024

I'll Back up and Share some nice scripts that come about messing with ChatGPT - This will create a plane from the clipboard (Windows). Pretty Handy for modeling from reference.  It can run from the script editor or shelf:


import maya.cmds as cmds
from PIL import ImageGrab
from PIL import Image
import tempfile
import time


def create_unique_poly_plane():
    global last_image_hash


    try:
        # Capture the image from the clipboard using PIL (Pillow)
        clipboard_image = ImageGrab.grabclipboard()


        if clipboard_image:
            # Generate a hash of the clipboard image to detect new images
            current_image_hash = hash(clipboard_image.tobytes())


            # Check if the image is new
            if current_image_hash != last_image_hash:
                last_image_hash = current_image_hash


                # Get image dimensions
                image_width = clipboard_image.width
                image_height = clipboard_image.height


                # Unique naming
                timestamp = int(time.time())
                plane_name = f"Clipboard_Poly_Plane_{timestamp}"
                texture_name = f"Clipboard_Texture_{timestamp}"


                # Create a polyPlane in Maya with the same dimensions
                poly_plane = cmds.polyPlane(
                    width=image_width / 100.0,  # Convert to Maya units (cm)
                    height=image_height / 100.0,  # Convert to Maya units (cm)
                    subdivisionsWidth=1,
                    subdivisionsHeight=1,
                    name=plane_name
                )


                # Save the clipboard image to a temporary file
                temp_image_path = tempfile.gettempdir() + f"/{texture_name}.png"
                clipboard_image.save(temp_image_path, "PNG")


                # Setup shader and texture
                setup_shader(poly_plane[0], temp_image_path, texture_name)


                print(f"PolyPlane '{plane_name}' created with new clipboard image as a texture!")


            else:
                print("Clipboard image is the same as the last one processed.")


        else:
            print("No image found on the clipboard.")


    except Exception as e:
        print("An error occurred:", str(e))


def setup_shader(poly_plane, image_path, texture_name):
    # Open Hypershade and create a shader network
    cmds.HypershadeWindow()
    lambert_shader = cmds.shadingNode('lambert', asShader=True, name=f"Lambert_{texture_name}")
    shading_group = cmds.sets(renderable=True, noSurfaceShader=True, empty=True, name=f"SG_{texture_name}")
    file_texture = cmds.shadingNode('file', asTexture=True, name=texture_name)


    # Connect file texture to Lambert shader
    cmds.connectAttr(file_texture + '.outColor', lambert_shader + '.color')
    cmds.connectAttr(lambert_shader + '.outColor', shading_group + '.surfaceShader')


    # Set the file texture path
    cmds.setAttr(file_texture + '.fileTextureName', image_path, type="string")


    # Assign the shader to the polyPlane
    cmds.select(poly_plane)
    cmds.hyperShade(assign=lambert_shader)


# Initialize the last image hash
last_image_hash = None


# Example usage: Call this function each time you want to create a polyPlane from a new clipboard image
create_unique_poly_plane()