Here are a selection of Maya Python scripts I've been using to make my workflow easier:

Vertex to Locator (credit to Henry Lockett for this script - note this is MEL and not Python

Random Vertex Colour

Random Deselect

Assign Different Collision Layer

Delete Keyframes after a Specified Time

Add Suffix to Name

Create Lightning Mesh

Sort Outliner Alphabetically


Automatically add a gradient of vertex colour to a mesh

string $vertices[] = `ls -sl -fl`;

float $max_iterations = 5;
float $current_iteration = $max_iterations;
float $colour_step = 1/($max_iterations+1);

for($j=0; $j<=$max_iterations; ++$j)
{
    for($i=0; $i<$current_iteration; ++$i)
    {
        select `ls -sl`;PolySelectTraverse 1;select `ls -sl`;
    }

    float $current_colour = $current_iteration * $colour_step;    
    print ($current_colour + "\n");
    polyColorPerVertex -r $current_colour -cdo;
    select -cl;
    select $vertices;
    $current_iteration = $current_iteration - 1;
}

Random Vertex Colours (default is Red channel only)

import maya.cmds as cmds
import random

sel = cmds.ls(sl=1)

for item in sel:
    colour = random.random()
    cmds.select(item)     
    cmds.polyColorPerVertex(r=colour)

Vertex Position to Locators

// Select the vertices you want a locator at and run this MEL script...

string $vertices[] = `ls -sl -fl`;
for($v in $vertices){
    float $pos[] = `xform -q -ws -t $v`;
    string $newLoc[] = `spaceLocator`;
    setAttr ($newLoc[0]+".translate") $pos[0] $pos[1] $pos[2];
}

Random Deselect

import maya.cmds as cmds
import random

keep = 0.5;
        
# save selection as a list, create empty list
sel = cmds.ls(sl=1)
new_sel = []
        
# iterate over list only keeping some objects
for item in sel:
    print item
    if random.random() < keep:
        new_sel.append(item)
                
# select new selection
cmds.select(new_sel)


This simple script just randomly selects a percentage of the objects in your current selection - very useful for quickly creating randomness.  

 

Assign Different Collision Layer

import maya.cmds as cmds

# save selection as a list 
sel = cmds.ls(sl=True)

# iterate over items in the list setting each to have a different Collision Layer
for i in range(len(sel)):
    cmds.setAttr('{0}.collisionLayer'.format(sel[i]), i)

This script again iterates over a selection and sets each object to have a different Collision Layer - useful for speeding up things like brick wall explosions where the inter-object collisions aren't really important.  Remember to set you impact object and ground plane to Collision Layer -1 so that it still collides with everything. 

 

Delete Keyframes after a Specified Time

import maya.cmds as cmds
import math

# setup inital variables 
start_frame = 0.0
last_frame = 200
frames_to_keep = 20.0
move_tolerance = 0.1

# list selected objects
sel = cmds.ls(sl=1)

# iterate over each mesh in list
for mesh in sel:
    
    # re-initalalise
    print mesh 
    start_frame = 0.0
    cmds.currentTime(start_frame)
    
    # find the first frame the mesh moves 
    for frame in range(last_frame):
        if not start_frame > 0:
            if cmds.getAttr('{0}.ty'.format(mesh)) > move_tolerance:
                start_frame = cmds.currentTime(query=1)
            cmds.currentTime(frame)
            
    # delete all frames after start_frame + frames_to_keep        
    cmds.cutKey('{0}'.format(mesh), time=(math.trunc(start_frame + frames_to_keep), last_frame))

cmds.currentTime(last_frame)

This script iterates over a selection and only keeps a set number of keyframes after the object first moves vertically.  This is useful for creating 'ground crack' style simulations - simulate your pieces being impacted without gravity and then just keep the first 10-20 frames of the simulation

 

Add Suffix to Name

import maya.cmds as cmds

# Suffix text to add
suffix = '_low'

# List all selected objects
sel = cmds.ls(sl=1)

# For each item in the list add the suffix
for item in sel:
    cmds.rename(item,'{0}{1}'.format(item,suffix))

Adds the specified suffix to the object names - useful for baking by name in Substance.

 

Create Lightning Mesh

import maya.cmds as cmds
import random

''' 
Create and deform a number of meshes and uv's for use with a scrolling UV2 material 
'''

numMeshes = 10

divisions = 20
moveRepeats = 3  #more repeats results in a more jagged mesh
offsetfactor = 1

#Loop over numMeshes

for mesh in xrange(0, numMeshes):

    #Create Mesh and randomly move Edges
    
    lightningmesh = cmds.polyPlane( sx=divisions, sy=1, w=20, h=0.5)[0]
    
    for rep in xrange(0,moveRepeats):
        for x in xrange(3,(divisions*2),2):
            offset = (random.random()-0.5)*2 * offsetfactor
            cmds.select("{0}.e[{1}]".format(lightningmesh,x))
            cmds.move(offset, z=1, r=1)
            
            
    #Create Second UV Set
    
    cmds.select("{0}.map[:]".format(lightningmesh))
    cmds.polyEditUV(pivotV=0, scaleV=0.01)
    
    cmds.select("{0}.map[:]".format(lightningmesh))
    cmds.polyEditUV(r=1, v=mesh*(1.0/numMeshes))

    cmds.select('{0}'.format(lightningmesh))
    cmds.polyCopyUV(createNewMap=1)
        
    cmds.polyNormalizeUV(pa=0)

    #Add a Wave Deformer
    
    Amp = (random.random()-0.5) * 0.4
    Wavelength = random.random() + 0.5
       
    cmds.select('{0}'.format(lightningmesh))
    WaveDef = cmds.nonLinear(type="wave")[0] 
    
    cmds.setAttr('{0}.amplitude'.format(WaveDef), Amp)
    cmds.setAttr('{0}.wavelength'.format(WaveDef), Wavelength)
    cmds.setAttr('{0}.dropoff'.format(WaveDef), 1)
    
    #Add a Twist Deformer
    
    TwistAngle = (random.random()-0.5)*2 * 720
    
    cmds.select('{0}'.format(lightningmesh))
    TwistDef = cmds.nonLinear(type='twist')
    
    cmds.setAttr('{0}.rz'.format(TwistDef[1]), 90)
    cmds.setAttr('{0}.endAngle'.format(TwistDef[0]), TwistAngle)
    


 

#Sort objects in the Outliner Alphabetically

import maya.cmds as cmds

#Select all Geometry and reverse
sel = cmds.ls(tr=1) 
sel.reverse
#print sel

#Create an Empty Group
cmds.group(em=1, name='temp')

#Parent to the Empty Group and then back to World
for item in sel:
    cmds.parent('{0}'.format(item), 'temp')
    cmds.parent('{0}'.format(item), world=1)
    
#Select all Cameras 
sel = cmds.ls(ca=1) 

#Parent to the Empty Group and then back to World
for item in sel:
    cmds.parent('{0}'.format(item), 'temp')
    cmds.parent('{0}'.format(item), world=1)

#Delete the Empty Group    
cmds.delete('temp')