Tuesday, August 25, 2015

Some useful shelf tools...

SNAP TO PLACE

How to use:

Copy the code below to the script editor and drag it onto your custom shelves so you can access it at any time (remember to save your shelves before quitting Maya)

Select object with correct location first and then the object you want to move to match the first. Then run the script found below. It snaps the second object(s) selected to the location and rotation of the first object's pivot.
If the second object(s) has any keyframes on any translation or rotation channels, it sets a keyframe at the current frame on those channels.
The rotations may not match if either one of the objects has freeze transforms.

import maya.cmds as mc
selection = mc.ls(sl=True)
posSel = selection[0]
objSel = selection[1:]
#create dummy to find posSel pivot
dummy = mc.spaceLocator(name= 'dummy')
constraint = mc.parentConstraint( posSel, dummy)
mc.delete(constraint)
#align objSel to dummy
mc.align( objSel, dummy, x='mid', y='mid', z='mid', atl=True )
#get dummy rotation
rValues = mc.xform( dummy, ws=True, q=True, ro=True)
rx,ry,rz = rValues[0],rValues[1],rValues[2]
#key existing anim curves
for object in objSel:
#assign rot values to objSel
mc.xform( object, a=True, ro=(rx,ry,rz))
attributes = ['tx', 'ty', 'tz', 'rx', 'ry', 'rz']
for attribute in attributes:
keyCount = mc.keyframe(object, at=attribute, q=True, kc=True)
if keyCount>0:
mc.setKeyframe(object, at=attribute)
mc.delete(dummy[0])
view raw SnapToPlace hosted with ❤ by GitHub


CREATE CONTROL

How to use:

Copy the code below to the script editor and drag it onto your custom shelves so you can access it at any time (remember to save your shelves before quitting Maya)

This is a tool to create nurb curves as controls for a rig. When rigging I found myself going through the same steps a bunch of times to create all the controls I needed, so I created this shelf button that does about 90% of the job for me. Here's how it works:

Select the object and then run the script. It will create a nurbs circle and snap it to the object. It will  rotate the circle so that it's perpendicular to the longest dimension of the object and will resize it to make it slightly bigger than the second longest dimension of the object. It will also delete history on the circle and freeze transforms. This usually gets me pretty close to where I want to get and all that's left is moving or tweaking the shape in component mode to get it to look the way I want.
import maya.cmds as mc
selObj = mc.ls(sl=True)
for obj in selObj:
#get bounding box
bbValues = mc.exactWorldBoundingBox(obj)
x = bbValues[3] - bbValues[0]
y = bbValues[4] - bbValues[1]
z = bbValues[5] - bbValues[2]
maximo = max(x,y,z)
if maximo == x:
normalPlane = (1,0,0)
radio = max(y,z)
if maximo == y:
normalPlane = (0,1,0)
radio = max(x,z)
if maximo == z:
normalPlane = (0,0,1)
radio = max(y,x)
#create circle
nCircle = mc.circle(n= str(obj)+ '_ctrl', nr=normalPlane, r=radio*1.2)
#constraint to obj
constraint = mc.parentConstraint( obj, nCircle )
#break constraint
mc.delete(constraint)
#delete history
mc.delete(ch=True)
#freeze transforms
mc.makeIdentity(apply=True, t=1, r=1, s=1, n=0)
view raw create control hosted with ❤ by GitHub


CREATE CAMERA

How to use:

Copy the code below to the script editor and drag it onto your custom shelves so you can access it at any time (remember to save your shelves before quitting Maya)

This tool speeds up the process of creating a camera and opening it in a new window. First find the camera angle you want using your perspective camera (or any other camera you don't mind moving around). With that viewport selected, run the script. It will create a new camera with the same properties and open a new panel to display the new camera view, with a resolution gate on, no grid and objects displayed in smooth shaded mode. It will also name the cameras incrementally as cam01, cam02, and so on.


import maya.cmds as mc
#find current cam
currentPan = None
currentPan = mc.getPanel(wf=True)
#check viewport is selected
exists = mc.modelPanel(currentPan, ex=True)
def createCam():
if exists:
#find current cam
cam = mc.modelPanel(currentPan, q=True, camera=True)
else:
mc.confirmDialog(title='Warning', message='Please select viewport', button='ok')
return
newCam = mc.duplicate(cam, name= 'cam0#')
#show resolution gate
mc.camera( newCam, e=True, dr=True )
mc.setAttr(str(newCam[0])+'.visibility',1, e=True)
#open in another window
window = mc.window(widthHeight=(800, 800))
mc.paneLayout()
pan = mc.modelPanel()
# display smooth shaded, no nurb curves
mc.modelEditor(pan, e=True, da="smoothShaded", nc=False, gr=False)
mc.lookThru(pan, newCam)
mc.showWindow( window )
createCam()
view raw createCam hosted with ❤ by GitHub
LOCATOR AT SELECTION

How to use:

Copy the code below to the script editor and drag it onto your custom shelves so you can access it at any time (remember to save your shelves before quitting Maya)

Select an object and run the script. It will create a locator named 'objectName_loc' at the position of the pivot point of the selected object.

import maya.cmds as mc
name = mc.ls(sl=True)
newLoc = mc.spaceLocator(name= name[0]+'_loc')
constraint = mc.parentConstraint( name, newLoc)
mc.delete(constraint)

No comments:

Post a Comment