I have a Maya plugin in c++. In this plugin, a timeChange function is fired whenever the time changes in Maya.
I want to create a specific Mesh Shape when a given time is reached. The shape is properly created, but when wanting to assign a shader to the shape, it tells that the shape cannot be found. Reexecuting the assignment via a button or via the MEL interface works properly.
Algorithm is the following:
When it fails:
- Time change
- Event-function fired (static function)
- Shape created
- Assignment of shader via API with a mel command --> Error
- Assignment of shader via interface with same mel command --> Works
When it works:
- Button-clicked-function fired
- Shape created
- Assignment of shader via API with a mel command --> Works
void MyClass::CreateOneShape()
{
MDagPath thisDagNode;
MFnDagNode dagPath;
MString rootName;
// CREATING SHAPE
log("Will create shape");
MFnDependencyNode fnPolySp;
MObject objPolySp = fnPolySp.create("polyPrimitiveMisc");
MFnDagNode fnPolyTrans;
MObject objPolyTrans = fnPolyTrans.create("transform");
MFnDagNode fnPolyShape;
MObject objPolyShp = fnPolyShape.create("mesh", objPolyTrans,&status);
rootName="Unit_";
fnPolyShape.setName(rootName+MString()+UnitId+"_Shape");
MFnTransform m(objPolyTrans);
m.setName(rootName+MString()+UnitId);
MFnDagNode n(theMesh,&status);
MDagPath dp;
status=n.getPath(dp);
MFnMesh fn(dp,&status);
MObjectArray shaders;
MIntArray indices;
status=fn.getConnectedShaders(0,shaders,indices);
for (int sh=0;sh<shaders.length();sh++)
{
MFnDependencyNode dep(shaders[sh]);
// Below is the command that fails when called in an event function and works when called via a button function.
MString cmd="select -cl ; select -r "+rootName+MString()+UnitId+"_Shape.f[" + MString()+int(surfaceNb) + "] ; sets -e -forceElement " + dep.name() + ";";
MGlobal::executeCommand(cmd);
surfaceNb++;
}
}
void timeChanged(MyClass *clientData)
{
clientData->CreateOneShape(); --> Gives error that shape cannot be found
}
void buttonClicked()
{
CreateOneShape(); --> Works fine
}
How to have my shader properly assigned in an event-fired function ?