Archived entries for Python

Scripted Clipboard Access in Maya

I recently had a situation where we needed to access the data in the clipboard from within a Mel script; in straight mel/python there is no way to access this data (I guess because Maya is cross platform, and clipboard support is OS specific). But by using the pyWin32 extensions we can get access to the clipboard in Windows.

I’ve written two very simple mel functions that allow you to copy and paste a string to the clipboard directly.

clipboardCopy( string ) will put the string onto the clipboard.

global proc clipboardCopy( string $text )
{
	//install pyWin32Extensions, needed to access the clipboard
	pyWin32Extensions();

	python( "import win32clipboard" );

	python( "win32clipboard.OpenClipboard()\nwin32clipboard.EmptyClipboard()\nwin32clipboard.SetClipboardText('" + $text + "')\nwin32clipboard.CloseClipboard()" );
}

clipboardPaste() will return the conents of the clipboard.

global proc string clipboardPaste()
{
	//install pyWin32Extensions, needed to access the clipboard
	pyWin32Extensions();

	python( "import win32clipboard" );

	python( "win32clipboard.OpenClipboard()\nd = win32clipboard.GetClipboardData( win32clipboard.CF_TEXT )\nwin32clipboard.CloseClipboard()" );
	string $clipboardText = python( "d" );

	return $clipboardText;
}

Python Win 32 Extensions for Maya

When using Windows OS, there are lots of features and functions of the OS that normal applications can access, however when using Python these aren’t available. To get round this a set of extensions to Python have been released that give access to all these OS specific functions – http://sourceforge.net/projects/pywin32/

Different versions of Maya use different versions of Python so you need to get the right version of the extensions, 8.5 uses Python 2.4, 2008 and 2009 use Python 2.5.

The downloaded executable will try to install the extensions to a python installation, and annoyingly it doesn’t detect Maya’s copy. You can use 7zip to extract the juicy bits from the .exe file, the PLATLIB folder is the one you want. The contents need to be copied into your Maya install directory, in Python\lib\site-packages\ and also the contents of pywin32_system32 (pythoncom25.dll and pywintypes25.dll) need to be copied into bin in the Maya install folder.

Script it

To make this whole process easier for our studio I wrote a mel script that handles all this automatically; a single function that checks and installs the extensions. Any script or tool can execute this function to ensure that the extensions are installed and available to it.

You just need to edit the script so that it points to the location of your downloaded extensions, we have these on a network share so they are accessible to everyone. If you want to use them then you just need to call pyWin32Extensions() in mel at the beginning of your script, the function will also return an int to indicated the status of the extensions. ( Returns 0 if not available, 1 if available, 2 if just installed )

//check and install win32 extension
//returns 0 if not available, 1 if available, 2 if just installed.

global proc int pyWin32Extensions()
{
	// need to check and copy the files over
	string $dest = `getenv "MAYA_LOCATION"` + "/Python/lib/site-packages/";
	$fileNames = `getFileList -folder $dest`;
	string $fileName = "";

	for ($fileName in $fileNames)
	{
		if ($fileName == "win32")
		{
			//return 1 to indicated that the extensions are available.
			return 1;
		}
	}
	
	progressWindow
		-title "Python Win32 Extensions"
		-progress 50
		-status "Installing Required Extensions";

//******************************************************************************
// Enter the location of the extracted pyWin32 files here
	string $source = "D:/MayaTools";
	
//If you want to support multiple versions of Maya then you can alter the 
//directories in the switch/case structure to point to your folders containing
//the different versions of pywin32
//******************************************************************************

	string $mayaVersion = `about -version`;
	int $invalidVersion = false;

	switch($mayaVersion)
	{
		case "2009":
		case "2008":
		case "2008 Extension 2":
			$source = $source + "/python/2008_2009_pythonWin32Extension/*.*";
			break;
			
		case "8.5":
		case "8.5 Service Pack 1":
			$source = $source + "/python/85_pythonWin32Extension/*.*";
			break;
			
		default:
			$invalidVersion = true;
	}
	
	if ( $invalidVersion )
	{
		warning "This version of Maya is not currently supported by Win32com extension, please contact your techincal artist to see if support can be added";
		progressWindow -endProgress;
		//return 0 to indicate that the extensions are not available.
		return 0;
	}
	
	$dest = substituteAllString($dest, "/", "\\");
	$source = substituteAllString($source, "/", "\\");

	string $command = "xcopy \"" + $source + "\" "+"\"" + $dest + "\" /s";
	system ($command);
	
	$source = $dest + "pywin32_system32\\*.*";
	$dest = `getenv "MAYA_LOCATION"` + "/bin/";
	$dest = substituteAllString($dest, "/", "\\");

	string $command = "xcopy \"" + $source + "\" "+"\"" + $dest + "\" /s";
	system ($command);
	
	warning "you may have to restart Maya to take the installation into effect";
	
	progressWindow -endProgress;
	
	//return 2 to indicate that the extension was installed.
	return 2;
}


RSS Feed. Copyright © 2004–2009. All rights reserved. Admin Login