Wednesday 14 December 2011

Locking vertex normals using Maya API

OpenMaya.MFnMesh.lockFaceVertexNormals is the function to use but it's got not very clear from the documentation what you need to pass to it. It needs an array of faces and of vertices formed like this:
faces = ( 0, 0, 0, 0, 1, 1, 1, 1 )
verts = ( 1, 2, 3, 4, 1, 2, 5, 6 )
In this example the face with index 0 will have vertices 1, 2, 3 and 4 locked. Face 1 will have vertices 1, 2, 5, 6 locked. This code will lock all vertex face normals:

verts = OpenMaya.MIntArray()
faces = OpenMaya.MIntArray()
vertsForFace = OpenMaya.MIntArray()
for faceID in range( meshFn.numPolygons() ) :
meshFn.getPolygonVertices( faceID, vertsForFace )
for i in range( vertsForFace.length() ) :
faces.append( faceID )
verts.append( vertsForFace[i] )
meshFn.lockFaceVertexNormals( faces, verts )

Wednesday 2 November 2011

Context sensitive hotkeys

Want the same hotkey to do different things dependent on which Maya window you're in?

string $panelWithFocus = `getPanel -withFocus`;
if( $panelWithFocus == "polyTexturePlacementPanel1" ){
//we're in the UV Editor!
}
else{
//We're somewhere else
}
Great for having different marking menus under one hotkey.

UV editor marking menus

How do you get a custom Maya marking menu to pop up in the UV editor instead of the viewport? Go to the hotkey editor and change the marking menu script to say:
-parent polyTexturePlacementPanel1Window
instead of:
-parent viewPanes
This should work with any Maya UI element.

Saturday 29 October 2011

Maya Tool Settings disappeared

I fire up Maya 2012 this morning and the Tool Settings widget is gone. It seemed to have shrunk to nothing whilst docked in the default left side of the window. Fixed it like this:
import maya.cmds as mc
mc.dockControl( "dockControl5", e=True, area="right" )
This moves it to the right side of the window like in older versions of Maya, from there it can be moved around as normal.

Tuesday 18 October 2011

Handling HasNoEffect state on a plugin node.

If you're writing a Maya plugin node, you might want to handle the case where the user sets the node state to HasNoEffect. The state is an input attribute that is implicitly defined for any node. You can query it in a similar way to any other input attribute:
stateHnd = dataBlock.inputValue( self.state ) #MDataHandle
if stateHnd.asInt() == 1 :
inMeshHnd = dataBlock.inputValue( self.inMesh )
outMeshHnd = dataBlock.outputValue( self.outMesh )
inMeshObj = inMeshHnd.asMesh() # MObject
outMeshHnd.setMObject( inMeshObj )
dataBlock.setClean( plug )
return OpenMaya.MStatus.kSuccess

Thursday 13 October 2011

Wing IDE up and running!

Got Wing IDE set up and debugging Maya. This page was very useful for that: Mel Wiki. However I had to change something to get it to send hi-lighted text from the editor to Maya. The line:
af, socktype, proto, canonname, sa = res[0]
became:
af, socktype, proto, canonname, sa = res[1]

Wednesday 7 September 2011

ddx and ddy HLSL Intrinsic Function

The ddx and ddy functions return the partial derivatives of a given value with respect to screen coordinates.
In other words: the slope of the function described by the value. Or in other words again: the rate at which a given value changes across the screen.
For example ddx( position ) will tell you how much the surface slopes either left or right. So normalize( cross( ddx(position), ddy(position) ) ) returns a normal to the surface.

Friday 2 September 2011

Maya itemFilter nodes

Maya deletes itemFilter nodes when a user creates a new scene or opens another one. This is unfortunate when the itemFilter is filtering something in a custom Outliner you've made. Unfortunately, specifying the Outliner as a parent is not supported. Setting the category of the itemFilter to "builtIn" however, makes them stick around, even after the current scene is closed.