Latest Entries

Save as TGA Photoshop Script

Our pipeline tends to work best when using TGA files for textures (it does work with PSDs but to be honest I don’t really trust it :)) However I’ve always found it a bit cumbersome to actually save out a copy from Photoshop, requiring you to ‘Save As…’, pick the file type, fix the name, yes I do want to overwrite, etc.

I figured a nice little script would streamline this considerbly.  And as an added bonus it’s possible to execute a script through an action, which also allows you to assign a keyboard shortcut to it. With this setup I just hit F2 and I get a tga of the current document, no interaction at all, it just spits it out without disrupting anything.

There’s just one thing to watch out for; by design, it will overwrite the old tga file without warning, so you need to make sure that the tgas are treated as a purely automatic intermediate step and shouldn’t be hand altered.


Just save this code to a .jsx file, create a new action and execute the script while it’s recording (File -> Scripts -> Browse…).

var docName = app.activeDocument.name
var docPath = app.activeDocument.path.fullName

var splitName = docName.split(".")

var fileName = splitName[0]

for (i=1; i<(splitName.length - 1); i++)
{
	fileName = fileName.concat( ".", splitName[i] )
}

var newFileName = docPath.concat( "/", fileName, ".tga")

targaFile = new File( newFileName )
targaSaveOptions = new TargaSaveOptions()
targaSaveOptions.resolution = TargaBitsPerPixels.THIRTYTWO
targaSaveOptions.alphaChannels = true

app.activeDocument.saveAs(targaFile, targaSaveOptions, true, Extension.LOWERCASE)

timed Maya Hotkeys

I like the functionality of the space bar in Maya, hit it quickly and it swaps between single and quad view, but hold it and it shows you the hotbox and release and it leaves the panel views the same. I wanted to replicate this timed nature in some of my own hotkeys. It was actually pretty simple; using the PRESS and RELEASE hotkey settings and a very simple timer check you can change the command depending on the length of time the hotkey was held for.

Below is a sample function using this idea, you would just need to rename some stuff and fill in the parts that actually do something when the key is pressed or held. I’ve used this idea to create some extra functionality for hiding/showing, and also a modified fit to panel command that allows different levels of zoom when holding the button.


global proc timedHotkeyFunction()
{
	global float $timedHotkey_timer;

	//Use -1 to reset the timer on release
	if ( $timedHotkey_timer < 0 )
	{
		$timedHotkey_timer = `timerX`;
	}
	else
	{
		float $heldTime = `timerX -startTime $timedHotkey_timer`;
		$timedHotkey_timer = -1;

		//Resets the timer if the time is too long, this is to guard against
		//cases where the press and release get mixed round.
		if ( $heldTime > 10 )
		{
			$timedHotkey_timer = `timerX`;
			print "\nReseting Hotkey timer - held for too long!";
		}
		//Time to define a press or a hold, 0.3 seems to work quite well
		else if ( $heldTime < 0.3 )
		{
			print "\nPressed!";
			//******************************************************************
			//Code for pressed function here.
			//******************************************************************
		}
		else
		{
			print "\nHeld!";
			//******************************************************************
			//Code for hold function here.
			//******************************************************************
		}

	}
}

//Define the runTimeCommand that will show up in the hotkey editor, this command
//needs to be assigned to both the PRESS and RELEASE hotkeys
if ( !`runTimeCommand -q -ex timedHotkeyTest` )
{
	runTimeCommand
		-annotation "Set this to both PRESS and RELEASE on your hotkey"
		-command ("timedHotkeyFunction")
		timedHotkeyTest;
}

diagnosisMaya

I find I have to diagnose problems with Maya fairly often; we’ve got a fairly complex setup environment to allow us to quickly switch projects with a completely different set of tools and scripts, but as it’s complex it inevitably goes wrong. I’ve written a nice little diagnosis script that basically just spits out a load of information about how Maya is setup, which allows me to check alot of different common problems on my own machine, allowing me to disrupt the user a little as possible.

The main part of the script is a diagnosis log output, this queries and then writes a log of different aspects of the Maya environment; ‘whatIs’ commands on various scripts, listings of environment variables, and listings of optionVars. Copies out a log of the script history, when scripts crash they usually output something into the history. Ability to copy various folders from the users machine so the scripts can be thoroughly checked, normally just the scripts directories in My Documents, but any folders could be copied that were important to the team setup. An extra section that will allow an extra script to be run and the results included in the log file.

To use the script you’ll have to alter the default output path to somewhere both you and your team can access, I’ve got it set to a network share. You can also easily alter some of the lists of things that are included in the log, these are all marked in the comments of the script.

Download: diagnosisMaya

optionVarBrowser Mel

Maya makes use of optionVars extensively throughout its UI, they are used to store information between Maya sessions. Every little bit of UI, settings, etc, are stored in optionVars when Maya shuts down so that everything is kept the same next time you load it up. When you start using them alot it can become cumbersome to query and debug the variables and their contents. This script helps by wrapping up the different useful operations into a nice UI. Start by typing a search string into the top input box and hit Search. It will show all optionVars that contain the string below. Selecting a variable in the top list will show its value(s) below. You can also add or delete variables with the two buttons at the top left. There are buttons to alter the contents of a variable at the bottom left; append adds a new item at the end, insert adds the item above the current selection, and remove removes the selected item. If you’ve got a non-array variable selected you can still use these commands and it will be changed into an array – be careful with this as any code that uses the variable will crash if it doesn’t get the sort of variable it was expecting. Any suggestions or feedback, leave a comment.


Download: djb_optionvarbrowser.mel


optionvarbrowser

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;
}

Windows Desktop Search for Mel

When customizing the Maya interface alot of the difficulty is in trying to find which of Mayas files you actually need to alter. this involves lots of scanning the scipt output to try and find command names, and ‘whatIs’ commands to get the mel files to look at. quite often you know the name of the command but you want to find out all the places that call it, and the only way to do this is to search the contents of all the mel scripts in the Maya directory. With a small tweak Windows desktop search (and probably google desktop and others) can index the contents of Mel files, and make searching them very quick and easy.

The options for windows desktop search are in Control Panel -> Indexing Options. You need to add the Maya directory to the list of folders to index. By default it won’t index the contents of Mel files, so you also need to tell it to consider these are plain text files; in the Indexing Options window, click Advanced, and go to the File Types tab. Search the list for the mel type, or you may need to add it if it’s not there, and set it to ‘Index Properties and File Contents’.

It will take a little while to index all the new files, but once done you can search the contents of Mayas mel files instantly.

EnvBrowser – Environment Variable Browser Mel

When trying to diagnose a broken Maya one of the first things I check is that all the scripts are being loaded from the right places. We have so many custom scripts that if some of the environment variable paths are wrong, or even just in the wrong order then all sort of strangeness can happen. Normally the way to check is to use ‘getenv’ on the different variables, which spits out a single line of text containing lots of different paths which is a real pain to read through.

This little UI tool is just a nice easy way to see the contents of the different variables. The main variables are added by default, you can add more or remove them with the buttons. Selecting one from the list will show its contents below, the full single list in the text box, and seperated out in the scrollField. It seperates simply, just by using the semicolons that split the path up; if you have a variable that uses semicolons for a different purpose then it will go a bit crazy, but I’ve not had that problem yet. If you press edit, you can then alter the contents in the text box line, but this change is only for this instance of Maya.

Any suggestions or feedback, leave a comment!


Download: djb_envbrowser


envbrowser



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