0

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 ?

Laurent Crivello
  • 3,809
  • 6
  • 45
  • 89
  • Can you re-implement the selection using in-line API commands instead of using executeCommand? This all sounds a lot like the Mel command is getting deferred until after the API calls are executing in some contexts. – theodox Feb 01 '16 at 18:06
  • Actually the issue seems to rather come from the fact that when a dagnode is created from a static function, then the behaviour is different. Strange... I tried as you suggested but same issue. – Laurent Crivello Feb 03 '16 at 15:50

1 Answers1

0

It main sounds strange, but the main issue is that you did not call MDGModifier::doIt() to force Maya to refresh its internal states before trying to build more connections. And I would use the MDagModifier::createNode() instead to capture it in the correct undo stack while calling the doIt() function. The way you do it is that you create a undo step for each, while you certainly want to capture all in one. MDagModifier is a derivative of MDGModifier, so it has the doIt() function as well. Because sometimes it is harder to create shaders/groups connections, calling MEL is fine, but call doIt() before, that should solve your issue. If not let me know and I write some code to demo that.

cyrille
  • 2,616
  • 1
  • 10
  • 18