Wednesday, 11 January 2012

Nested function variable scope in Javascript

The way scope works for nested functions in Javascript can trip you up:
#target photoshop
function test()
{
this.a10 = function()
{
a = 10;
this.a20();
return a;
}
this.a20 = function()
{
a = 20;
}
}
t = new test();
alert( t.a10() ); // Returns 20!
And now with the added "var" keyword:
#target photoshop
function test()
{
this.a10 = function()
{
var a = 10;
this.a20();
return a;
}
this.a20 = function()
{
var a = 20;
}
}
t = new test();
alert( t.a10() ); // Returns 10

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]