Useful tutorials

During my internet explorations I’ve discovered Zeth Willie.
He does Maya Tutorials and posts them on Vimeo. He explains himself and Maya very clearly, which is great for beginners and advanced users alike.

His introduction to Python is something all Maya users should watch :)

Have a look:
http://vimeo.com/67579845

Zeroing out. Or in.

Today I wrote a little tool that simply groups whatever you have selected, for the purposes of cleaning up transforms. Also works with multiple objects selected and groups them all under a group in the same worldspace as the first selected object.
Grab the code here and place it on your shelf.

import maya.cmds as cmds
import maya.OpenMaya as om

grpList = cmds.ls(sl = True)

parentObj = cmds.listRelatives( grpList[0], parent = True )
cmds.group( em = True, name = grpList[0] + '_Zero_GRP' )
cmds.parentConstraint( grpList[0], grpList[0] + '_Zero_GRP' )
cmds.delete( grpList[0] + '_Zero_GRP_parentConstraint1' )
cmds.scaleConstraint( grpList[0], grpList[0] + '_Zero_GRP' )
cmds.delete( grpList[0] + '_Zero_GRP_scaleConstraint1' )
cmds.parent( grpList[0], grpList[0] + '_Zero_GRP' )
try:
    cmds.parent( grpList[0] + '_Zero_GRP', parentObj )
    om.MGlobal.displayInfo( grpList[0] + ' now has a group above it' )
except:
    om.MGlobal.displayInfo( grpList[0] + ' has no parent object, grouped in world space' )

cmds.select( grpList[0] )
newParent = cmds.listRelatives( parent = True )
listEndNumber = len(grpList)
remainingObj = grpList[1:int(listEndNumber)]

for i in remainingObj:
    cmds.parent( i, newParent )

om.MGlobal.displayInfo( grpList[0] + ' now has a group above it' )

 

Google keep

I’ve found myself using Windows sticky notes to jot down thoughts, ideas and lists.
Then I bought an Android phone and started using a sticky notes app to do the same.
After some time I found my notes fragmented and difficult to manage, then a friend suggested Google Keep.

Google Keep is a note taking app for android phones, which works a charm. Though this still didn’t solve my problem.
I then decided to make the jump to the Chrome Browser where I can use the Google Keep browser app.
This means I can open chrome and start the Keep app and have it sync with my phone. Nifty!

But I was still not happy, as this removed some functionality form the Windows sticky notes, as they open on system logon.

I set about to solve this and have a solution for you!

1. install the Google Keep app for you phone and Chrome Browser. (links below)

2. on your PC right click on the Keep app in the Chrome apps window > create shortcut > desktop.

3. Now right click on that shortcut to Google Keep on you desktop > properties.

4. copy the whole line of text from the target text-field ( Ctrl + A > Ctrl + C )

5. start up Windows task scheduler > action > create basic task.

6. Name it “start google keep”, click Next

7. set the trigger to “When I Log on.”, click Next

8. action “start a program”, click Next

9. paste the text into the Text/script text-field.

10. click Next then Finish and you’re done!

Once this is complete you can delete the shortcut from your Desktop.

Google keep should now start when you log on and you can sync notes from you phone to you PC desktop with ease!

Chrome Browser – https://www.google.com/intl/en/chrome/browser/

Google Keep for Chrome – https://chrome.google.com/webstore/detail/google-keep/hmjkmjkepdijhoojdojkdfohbdgmmhki?hl=en

Google Keep for Android – https://play.google.com/store/apps/details?id=com.google.android.keep

Generative coding

I keep coming back to procedural generation. Something about the unpredictable nature of parametric design really piques my interest.
I’ve begun scripting a system that will be the basis of a generative project, here it is:

import maya.cmds as cmds
import random

curveNumber = 1
curveName = 'string' + str(random.randrange(1,100))
count = 0

for i in range(0,curveNumber):
    pointNumber = random.randrange(4,100)
    pointArray = [(0,0,0)] * pointNumber
    cmds.curve( name = curveName + str(count).zfill(3), degree = 3, point = pointArray )
    pointCount = 0
    directionX = (random.random() -0.5 )
    directionY = (random.random() -0.0 )
    directionZ = (random.random() -0.5 )
    curveAmount = 0.00001 * random.randrange(1,100)
    for i in range(0,int(pointNumber)):
        oldCurveAmount = curveAmount
        cmds.setAttr( curveName + str(count).zfill(3) + '.controlPoints' + str([pointCount]), pointCount * directionX * curveAmount, pointCount * directionY, pointCount * directionZ * curveAmount )
        curveAmount = curveAmount * 1.1
        print curveAmount
        print oldCurveAmount
        pointCount += 1
    count +=1

This script creates curves of different lengths and CV counts, then arcs them in different directions. I’m hoping to have them arc back as the angle becomes steeper to create an organic curving line.

Naming and renaming

During the last job I was on, we were receiving rigs from another company.
These rigs did not always match our hierarchy and naming conventions, so, part of my job was to recompile these rigs to match our conventions. One common and potentially time consuming job was the renaming of control curves.
We would receive them with naming conventions along the lines of ‘*_Ctrl_01’, ‘*_Ctrl_02’ and so on.
Renaming every control in the rig is neigh on impossible, so I wrote a script to do so!
The script I wrote was smashed together with no time for optimization but now with a little time on my hands I have rewritten it to be user friendly and fast.
Here it is, have a look!

import maya.cmds as cmds

selection = cmds.ls( sl = True)

for i in selection:
    shape = cmds.listRelatives( i, shapes = True )
    if shape == None:
        print 'No shape for ' + i + ', aborting.'
    else:
        try:
            a,b = i.split('_Ctrl')
            cmds.rename( i, a + b + '_Ctrl' )
        except:
            print 'No Ctrl in name of ' + i

Saving time

I spent today putting finishing touches on a script that saves a new version of the current scene. and a button icon to go with!
ALRsaveVer
Grab the script here and pop it on your shelf.

################################################################################################
################################################################################################
###                             Save new version of current scene                            ###
###                                  By Alastair Richardson                                  ###
###                                         May, 2013                                        ###
###                               Intended for Maya Ascii files                              ###
###          Use:                                                                            ###
###             padding is the number of digits in the version number.                       ###
###                                                                                          ###
###             versionIndicator is the characters that come before                          ###
###             the version number. May be _V or _v depending on your                        ###
###             pipeline.                                                                    ###
###                                                                                          ###
###             fileCountNumbering can be set to True or False.                              ###
###             If set to True, the next version number will be obtained                     ###
###             by counting the number of files in the working folder.                       ###
###             This method is unpredictable if folder contains additional                   ###
###             files or folders.                                                            ###
###                                                                                          ###
###             If set to False, the next version number will be obtained                    ###
###             using the current version number, can be risky if rolling                    ###
###             to old versions is required.                                                 ###
###                                                                                          ###
################################################################################################
################################################################################################

padding = 3
versionIndicator = '_v'
fileCountNumbering = True

################################################################################################
###                                                                                          ###
###                             Change script at your own risk                               ###
###                                                                                          ###
################################################################################################
import maya.cmds as cmds
from pymel.core import *
import maya.OpenMaya as om
import os

def saveNewVer():
    # find directory
    currentDir = cmds.file( query = True, location = True )
    splitDir = currentDir.split('/')
    length = len(splitDir)
    lessOne = length - 1
    shortDir = splitDir[0:lessOne]
    directoryToOpen = '/'.join(shortDir)
    # create next version number of file
    if fileCountNumbering == True :
        # find the next version based on number of files on directory
        currentFileName = splitDir[length -1]
        versionSpilt = currentFileName.split(versionIndicator)
        currentName = versionSpilt[0]
        files = os.listdir( directoryToOpen )
        mayaAscii = []
        for i in files:
            try:
                a,b = i.split('.')
            except:
                print i + ' has no extension'
            if b == 'ma':
                mayaAscii.append(i)
            else:
                print str(i) + ' is not a Maya ASCII'
        nextVersion =  len(mayaAscii) + 1   
    else:
        # find version by disecting current file name at _v
        currentFileName = splitDir[length -1]
        versionSpilt = currentFileName.split(versionIndicator)
        sansMA = versionSpilt[1].split('.')
        currentVersion = sansMA[0]
        currentName = versionSpilt[0]
        nextVersion = int(currentVersion) + 1
    # do the saving
    newDirName = directoryToOpen + '/' + currentName + versionIndicator + str(nextVersion).zfill(padding)
    Mel.eval( "source addRecentFile.mel" )
    Mel.eval('addRecentFile "%s" "%s";' % (newDirName + '.ma', 'mayaAscii'))
    cmds.file( rename = newDirName )
    cmds.file( save = True )
    om.MGlobal.displayInfo( 'saved to ' + newDirName )
saveNewVer()

 

Notepad ++

I use Notepad ++ as my script editor when I’m working on large coding projects.
Because when Maya crashes it takes any unsaved script changes with it!
But being used to the MEL command highlighting in the Maya script editor I went hunting and turned up this:
http://www.creativecrash.com/downloads/applications/syntax-scripting/c/mel-language-definition-for-notepad-
Then I picked up a Maya-ish theme and modified the language to work with Maya Python syntax highlighting:
N++themeFiles

How to:

Put Waher-style in C:\Program Files (x86)\Notepad++\themes

put jbMayaPython in C:\Program Files (x86)\Notepad++\plugins\APIs

put userDifineLang in C:\Users\*USER*\AppData\Roaming\Notepad++

 

load the theme with notepad++’s theme loader then
select the language with the language dropdown.