Awesome MAYA scripts

* Change the specific attribute of all the phong nodes in the Maya scene:

{
    string $m;
    string $ms[] = `ls -type "phong"`;
    for ($m in $ms)
    {
        setAttr ($m + ".reflectivity") 0;
        setAttr ($m + ".specularColor") -type double3 0 0 0;
        setAttr ($m + ".cosinePower") 2;
        setAttr ($m + ".translucenceDepth") 0;
        setAttr ($m + ".translucenceFocus") 0;
    }
};

* To all textures set “alphaIsLuminance” 0

{
    string $m;
    string $ms[] = `ls -tex`;
    for ($m in $ms)
    {
        setAttr ($m + ".alphaIsLuminance") 0;

    }
};

* Clean Up _Pasted :

## This script requires pymel

## This script is a quick and simple way to clean up a scene after
## copy and pasting in Maya. It searches for objects with the name
## starting with pasted__, ungroups them if they came in a group,
## and renames them. Pymel automatically renames them uniquely if
## needed.

## Import pymel
from pymel.core import *

## Stores a list of all objects starting with pasted__
pastedObjects = ls('pasted__*')

## For each thing in the list, store it as object
for object in pastedObjects:
    ## Get a list of it's direct parent
    rel = listRelatives(object, p=True)
    ## If that parent starts with group
    if 'group*' in rel:
        ## It came in root level and is pasted in a group, so ungroup it
        rel[0].ungroup()
    ## The new name is the objects name without pasted__
    newName = object.replace('pasted__', '')
    ## Rename the object with the new name
    rename(object, newName)

* BVH Import

/*  This file downloaded from Highend3d.com
''  
''  Highend3d.com File Information:
''  
''    Script Name: BVH Import v2.0
''    Author: Sergiy
''    Last Updated: August 4, 2002
''    Update/Change this file at:
''    http://www.highend3d.com/maya/mel/?section=utilities#1840
''  
''  Please do not alter any information above this line
''  it is generated dynamically by Highend3d.com and will
''  be changed automatically on any updates.
*/

/*
BVH file import. Builds a skeleton under bvh_import group and imports the motion.
Sets up zero initial position at the frame -20. Works with Maya 4.0.
Just run the script and pick a bvh file.

NOTE: New Curve Default option (uder Preferences/Settings/Keys) MUST be "Independent Euler-Angle Curves"

Written by Sergiy Migdalskiy <migdalskiy(at)hotmail.com>, comments and suggestions are welcome.

Originally based on the bvh_import.mel script by sung joo, Kang (Gangs) / sjkang(at)bigfilm.co.kr, gangs2(at)nownuri.net
*/

string $filename=`fileDialog -dm "*.bvh"`;

$fileId=`fopen $filename "r"`;

select -cl;
int $joint_name_val = 0;
string $joint_name[];
float $offset_joint_x[], $offset_joint_y[], $offset_joint_z[];
string $index_joint[];
int $index = 0;
int $index_ch = 0;
string $index_channel[];
string $make_joint_cmd;
string $ch_tmp;
float $frame_interval;
string $temp_buff[];
string $name, $name_temp;

clear $joint_name $offset_joint_x $offset_joint_y $offset_joint_z $index_joint $index_channel;

$lbAuto=`autoKeyframe -q -st`;		//Get auto keyframing status.
autoKeyframe -st false;			//Turn off automatic keyframing.

$name = `group -em -n bvh_import`;
tokenize $name "bvh_import" $temp_buff;
if (size($temp_buff) == 0 ) {
	$name_temp = "";
}
else {
	$name_temp = $temp_buff[0];
}



string $nextWord = `fgetword $fileId`;

float $offsetx = 0;
float $offsety = 0;
float $offsetz = 0;

int $frames;
float $time_count = 0;

string $last_joint_name_val = "";

proc float Turn180Degrees (float $fAngle)
{
float $gPi = 3.1415926535897932384626433832795;
	return $fAngle >= 0 ? $fAngle - $gPi : $fAngle + $gPi;
}

// mirrors the rotation: returns pi-fAngle
// normalized to the given normalized [-pi,pi) angle
proc float Mirror180Degrees (float $fAngle)
{
float $gPi = 3.1415926535897932384626433832795;
	return $fAngle > 0 ? $gPi - $fAngle: -$gPi - $fAngle;
}

proc setRotation (string $strObject, float $fRotate[])
{
	float $fRotateX, $fRotateY, $fRotateZ;

	$fRotateX = $fRotate[0];
	$fRotateY = $fRotate[1];
	$fRotateZ = $fRotate[2];

	rotate -r -os 0 0 $fRotateZ $strObject;
	rotate -r -os $fRotateX 0 0 $strObject;
	rotate -r -os 0 $fRotateY 0 $strObject;
	setKeyframe -at "rotateX" $strObject;
	setKeyframe -at "rotateY" $strObject;
	setKeyframe -at "rotateZ" $strObject;
}

while (  size($nextWord) >0 )	{

		if ($nextWord == "ROOT")	{
				$jointname = `fgetword $fileId`;
				$joint_name[0] = $jointname+$name_temp;
				$index_joint[$index] = $jointname+$name_temp;
				joint -n $joint_name[0] -p 0 0 0;

			}

		if (($nextWord=="JOINT") || ($nextWord=="End"))    {

		// find Joint name

				$jointname = `fgetword $fileId`;
				$joint_name[$joint_name_val] = $jointname+$name_temp;
				$index_joint[$index] = $jointname+$name_temp;
			}

		if ($nextWord == "{")	{

			$nextWord = `fgetword $fileId`;
	
			if ($nextWord == "OFFSET" )
			{
				// find Joint offset data
				float $offset_x=`fgetword $fileId`;
				float $offset_y=`fgetword $fileId`;
				float $offset_z=`fgetword $fileId`;
				$offset_joint_x[$joint_name_val] = $offset_x;
				$offset_joint_y[$joint_name_val] = $offset_y;
				$offset_joint_z[$joint_name_val] = $offset_z;

				$offsetx = $offsetx + $offset_joint_x[$joint_name_val];
				$offsety = $offsety + $offset_joint_y[$joint_name_val];
				$offsetz = $offsetz + $offset_joint_z[$joint_name_val];

				if ($joint_name_val != 0)
				{
					if ($joint_name[$joint_name_val] == "Site")
						$joint_name[$joint_name_val] = "Effector" + $joint_name[$joint_name_val-1];
					$last_joint_name_val = $joint_name_val;
					$make_joint_cmd = "joint -n "+ $joint_name[$joint_name_val]+ " -p " + $offsetx + " " + $offsety + " " + $offsetz;
					$sel_joint_cmd = "select -r " + $joint_name[$joint_name_val-1];
					$ord_joint_cmd = "setAttr " + $joint_name[$joint_name_val-1] + ".rotateOrder 2";

					eval($sel_joint_cmd);
					eval($make_joint_cmd);
					eval($ord_joint_cmd);
				}

				}
				$joint_name_val ++;

			}

		if ($nextWord == "}")	{
				$joint_name_val --;
				$offsetx = $offsetx - $offset_joint_x[$joint_name_val];
				$offsety = $offsety - $offset_joint_y[$joint_name_val];
				$offsetz = $offsetz - $offset_joint_z[$joint_name_val];


			}

 		if ($nextWord == "CHANNELS") {

				int $tmp = `fgetword $fileId`;
				for ($i = 1; $i	<= $tmp; $i++)	{
						string $tmp2 = `fgetword $fileId`;
						switch ($tmp2)	{
								case "Xposition" :
									$ch_tmp = "translateX";
									break;

								case "Yposition" :
									$ch_tmp = "translateY";
									break;

								case "Zposition" :
									$ch_tmp = "translateZ";
									break;

								case "Xrotation" :
									$ch_tmp = "rotateX";
									break;

								case "Yrotation" :
									$ch_tmp = "rotateY";
									break;

								case "Zrotation" :
									$ch_tmp = "rotateZ";
									break;

						}
						$index_channel[$index_ch] = $index_joint[$index] + "." + $ch_tmp;
						$index_ch++;


					}

					$index ++;

			}

	    if ($nextWord == "MOTION") {

			$nextWord = `fgetword $fileId`;

			if  ($nextWord == "Frames:") {
				$frames = `fgetword $fileId`;
			}

			$nextWord = `fgetword $fileId`;
			$nextWord = `fgetword $fileId`;

			if ($nextWord == "Time:") {
				$frame_interval = `fgetword $fileId`;
			}

			$nextWord = `fgetword $fileId`;
			float $fRotation[3];
                        int $axies=0;
			for ( $k = 1; $k <= $frames; $k++) {

				currentTime $k;
				print ("currentTime " + $k + "\n");
				
				for ($chan = 0; $chan < size($index_channel); $chan++)
				{
					setAttr $index_channel[$chan] 0;
				}

				for ($j=1; $j<=$index_ch; $j++) { float $value = $nextWord; string $buffer[]; tokenize $index_channel[$j-1] "." $buffer; switch ($buffer[1]) { case "translateX": case "translateY": case "translateZ": setAttr $index_channel[$j-1] $value; setKeyframe -at $buffer[1] $buffer[0]; break; case "rotateX": $fRotation[0] = $value; $axies=$axies+1; break; case "rotateY": $fRotation[1] = $value; $axies=$axies+2; break; case "rotateZ": $fRotation[2] = $value; $axies=$axies+4; break; break; }; if ($axies==7) { setRotation ($buffer[0], $fRotation); $axies=0; } if ($axies>7) {
                                   print("Some kind of axial problem.\n");
                                }
/*
				if ($k >= 40 && $k <= 50 )
				{
					print ("\tsetAttr " + $index_channel[$j-1] + "  " + $value + ";\n");
				};
*/
				if($j<$index_ch) {
				   $nextWord =`fgetword $fileId`;
                                }

				}

				$time_count += ($frame_interval*30);
				$nextWord = `fgetword $fileId`;
			}
		}

		currentTime -20;
		// make up the initial pose
		for ($chan = 3; $chan < size($index_channel); $chan++)
		{
			string $buffer[];
			tokenize $index_channel[$chan] "." $buffer;
			setAttr $index_channel[$chan] 0;
			setKeyframe -at $buffer[1] $buffer[0];
		}


		$nextWord = `fgetword $fileId`;
	}

select -cl;
fclose $fileId;
autoKeyframe -st $lbAuto;		//Restore auto keyframing mode.

* Export SL Sculptie

//  * Copyright (c) 2007-$CurrentYear$, Linden Research, Inc.
//  * $License$


global proc string llFirst(string $list[])
{
	return $list[0];
}





global proc llSculptExport(string $object, string $file_name, string $file_format,
						   int $resolution_x, int $resolution_y,
						   int $maximize_scale, int $fix_orientation, int $flip_horizontal)
						   
{
	// copy it, because we're going to mutilate it.  MUHAHAHAAAaa...
	string $object_copy = llFirst(duplicate($object));

	// disentangle from groups
	string $parents[] = listRelatives("-parent", $object_copy);
	if (size($parents) != 0)
		$object_copy = llFirst(parent("-world", $object_copy));

	// scale it to unit cube
	float $bounding_min[3];
	float $bounding_max[3];

	$bounding_min = getAttr($object_copy + ".boundingBoxMin");
	$bounding_max = getAttr($object_copy + ".boundingBoxMax");

	float $scale[3];
	int $i;
	for ($i = 0; $i < 3; $i++)
		$scale[$i] = $bounding_max[$i] - $bounding_min[$i];

	float $scale_max = 0;
	for ($i = 0; $i < 3; $i++) if ($scale[$i] > $scale_max)
			$scale_max = $scale[$i];

	if ($maximize_scale)
	{
		print($object + " scale normalized - scale by " +
			  $scale[0] + " " + $scale[1] + " " + $scale[2] + " inside SL to get original shape\n");
	}
	else
	{
		for ($i = 0; $i < 3; $i++)
			$scale[$i] = $scale_max;
	}

	
	scale("-relative", 1/$scale[0], 1/$scale[1], 1/$scale[2], $object_copy);

	// position it in unit cube
	$bounding_min = getAttr($object_copy + ".boundingBoxMin");
	$bounding_max = getAttr($object_copy + ".boundingBoxMax");
	
	float $center[3];
	for ($i = 0; $i < 3; $i++)
		$center[$i] = ($bounding_min[$i] + $bounding_max[$i]) / 2.0;

	move("-relative", 0.5 - $center[0], 0.5 - $center[1], 0.5 - $center[2], $object_copy);


	//
	// nurbs surfaces can be adjusted to ensure correct orientation
	//

	if ($fix_orientation)
	{
		string $shape = llFirst(listRelatives("-shapes", $object_copy));
		if ((nodeType($object_copy) == "nurbsSurface") ||
			(($shape != "") && (nodeType($shape) == "nurbsSurface")))
		{
			// try to determine the "north pole";
			float $pole[] = pointOnSurface("-turnOnPercentage", 1,
										   "-parameterU", 0.5,
										   "-parameterV", 0,
										   $object_copy);
			float $total_distance = 0;

			float $v;
			for ($v = 0; $v <= 1; $v += 0.1)
			{
				float $point[] = pointOnSurface("-turnOnPercentage", 1,
												"-parameterU", $v,
												"-parameterV", 0,
												$object_copy);
				float $distance = 0;
				int $i;
				for ($i = 0; $i < 3; $i++) $distance += pow($pole[$i] - $point[$i], 2); $distance = sqrt($distance); $total_distance += $distance; } if ($total_distance > 0.1)  // the points don't converge on the pole - swap
			{
				print("swapping UVs to orient poles for " + $object + "\n");
				reverseSurface("-direction", 3, $object_copy);
			}

			// now try to ensure the normal points "out"
			// note: this could easily fail - but there's no better way (i think.)

			float $total_orientation = 0;
			float $u;
			for ($u = 0; $u <= 1; $u += 0.1)
				for ($v = 0; $v <= 1; $v += 0.1)
				{
					float $point[] = pointOnSurface("-turnOnPercentage", 1,
													"-parameterU", $u,
													"-parameterV", $v,
													$object_copy);
			
					float $normal[] = pointOnSurface("-normal",
													 "-turnOnPercentage", 1,
													 "-parameterU", $u,
													 "-parameterV", $v,
													 $object_copy);

					// check the orientation of the normal w/r/t the direction from center

					float $center_dir[];
					for ($i = 0; $i < 3; $i++)
						$center_dir[$i] = $point[$i] - 0.5;

					float $orientation = 0;  // dot product
					for ($i = 0; $i < 3; $i++) $orientation += $center_dir[$i] * $normal[$i]; $total_orientation += $orientation; } if ($total_orientation > 0)  // need to invert
			{
				print("reversing V for " + $object + "\n");
				reverseSurface("-direction", 1, $object_copy);
			}
			if ($flip_horizontal)
			{
			// reverse U, for compatibility with surface textures
			print("reversing U for " + $object + "\n");
			reverseSurface("-direction", 0, $object_copy);
			}
		}
		
		else
		{
			warning("cannot fix orientation on non-nurbs object: " + $object);
		}


	}
	
	// create temporary shading network
	string $sampler_info = createNode("samplerInfo");

	print("exporting sculpt map for " + $object + " into file " + $file_name + "\n");
	
	// bake sculpt texture
	string $fileNodes[] = convertSolidTx("-fileImageName", $file_name,
										 "-fileFormat", $file_format,
										 "-force", 1,
										 "-resolutionX", $resolution_x,
										 "-resolutionY", $resolution_y,
										 $sampler_info + ".pointWorld",
										 $object_copy);

	delete($fileNodes);  // we don't want 'em.  why do you make 'em?

	delete($sampler_info);
	delete($object_copy);
}



global proc int llSculptEditorCallback()
{
	string $objects[] = ls("-sl");

	if (size($objects) == 0)
	{
		warning("please select objects to export");
		return 0;
	}
	
	string $filename      = textFieldButtonGrp("-query", "-fileName", "llSculptEditorFilename");
	int $resolution_x     = intSliderGrp("-query", "-value", "llSculptEditorResolutionX");
	int $resolution_y     = intSliderGrp("-query", "-value", "llSculptEditorResolutionY");
	int $fix_orientation  = checkBoxGrp("-query", "-value1", "llSculptEditorFixOrientation");
	int $flip_horizontal  = checkBoxGrp("-query", "-value1", "llSculptEditorFlipHorizontal");
	int $maximize_scale   = checkBoxGrp("-query", "-value1", "llSculptEditorMaximizeScale");

	// get filetype
	string $file_type;
	string $file_base;
	string $file_extension;
	
	string $tokens[];
	tokenize($filename, ".", $tokens);

	if (size($tokens) == 1)  // no extension, default to bmp
	{
		$file_base = $filename;
		$file_type = "bmp";
		$file_extension = "bmp";
	}
	else
	{
		$file_extension = $tokens[size($tokens) - 1];
		int $i;
		for ($i = 0; $i < size($tokens) - 1; $i++) { $file_base += $tokens[$i]; if ($i != size($tokens) - 2) $file_base += "."; } if ($file_extension == "bmp") $file_type = "bmp"; else if (($file_extension == "jpg") || ($file_extension == "jpeg")) $file_type = "jpg"; else if (($file_extension == "tif") || ($file_extension == "tiff")) $file_type = "tif"; else if ($file_extension == "tga") $file_type = "tga"; else { warning("unknown image type (" + $file_extension + "). switching to bmp"); $file_type = "bmp"; $file_extension = "bmp"; } } string $object; for ($object in $objects) { string $this_filename = $file_base; if (size($objects) > 1)
			$this_filename += "-" + $object;
		$this_filename += "." + $file_extension;

		llSculptExport($object, $this_filename, $file_type, $resolution_x, $resolution_y,
					   $maximize_scale, $fix_orientation, $flip_horizontal);
	}

	
	select($objects);
	
	return 1;
}

global proc llSculptEditorSetFilenameCallback(string $filename, string $filetype)
{
	textFieldButtonGrp("-edit", "-fileName", $filename, "llSculptEditorFilename");
}

global proc llSculptEditorBrowseCallback()
{
	fileBrowser("llSculptEditorSetFilenameCallback", "Export", "image", 1);
}


global proc llSculptEditor()
{
	string $commandName = "llSculptExport";

	string $layout = getOptionBox();
	setParent $layout;

	setOptionBoxCommandName($commandName);

	setUITemplate -pushTemplate DefaultTemplate;

	scrollLayout;

	tabLayout -tabsVisible 0 -scrollable 0;

	string $parent = `columnLayout -adjustableColumn 1`;

	separator -height 10 -style "none";

	textFieldButtonGrp
		-label "Filename"
		-fileName "sculpt.bmp"
		-buttonLabel "Browse"
		-buttonCommand "llSculptEditorBrowseCallback"
		llSculptEditorFilename;

	intSliderGrp
		-field on
		-label "X Resolution"
		-minValue 1
		-maxValue 512
		-fieldMinValue 1
		-fieldMaxValue 4096
		-value 64
		llSculptEditorResolutionX;

	intSliderGrp
		-field on
		-label "Y Resolution"
		-minValue 1
		-maxValue 512
		-fieldMinValue 1
		-fieldMaxValue 4096
		-value 64
		llSculptEditorResolutionY;

	checkBoxGrp
		-label ""
		-label1 "Maximize scale"
		-numberOfCheckBoxes 1
		-value1 off
		llSculptEditorMaximizeScale;

	checkBoxGrp
		-label ""
		-label1 "Flip Horizontal"
		-numberOfCheckBoxes 1
		-value1 on
		llSculptEditorFlipHorizontal;

	checkBoxGrp
		-label ""
		-label1 "Correct orientation"
		-numberOfCheckBoxes 1
		-value1 on
		llSculptEditorFixOrientation;

	setUITemplate -popTemplate;

	string $applyBtn = getOptionBoxApplyBtn();
	button -edit
		-label "Export"
		-command "llSculptEditorCallback"
		$applyBtn;

	string $applyAndCloseBtn = getOptionBoxApplyAndCloseBtn();
	button -edit
		-label "Export and Close"
		-command "llSculptEditorCallback"
	$applyAndCloseBtn;


	setOptionBoxTitle("Export Sculpt Texture");

	setOptionBoxHelpTag( "ConvertFileText" );

	showOptionBox();
}



//

llSculptEditor;

* Select all joints in hierarchy

select -hi root;