[Maya C++ API] Per vertex color update in MPxDeformerNode

Code snippet in C++ to be able to update the color of a mesh on a per vertex basis inside a custom Maya node. Useful for debugging by displaying weights / float values associated to each vertex.

Don't forget to activate "Color" -> "Toggle Per Vertex Colors Attribute" in Maya's menu or call the MEL equivalent: toggleShadeMode();.

Note: contrary to setVertexColors() setVertexColor() won't work for some reasons, but the later is slower anyway.

#define mayaCheck(code) do{ const g_status_(code); const int l = __LINE__;\
    if(g_status_ != MS::kSuccess) \
        throw My_exception(g_status_, __TO_STR(__FILE__), l); \

// -----------------------------------------------------------------------------

MStatus Inherits_mpxdeformer::deform(MDataBlock& block,
                                     MItGeometry& geom_it,
                                     const MMatrix& object_matrix,
                                     unsigned int multi_index)
        MStatus status;
        MArrayDataHandle out_array = block.outputArrayValue(MPxDeformerNode::outputGeom, &status);
        MDataHandle houtput = out_array.inputValue(&status);
        MFnMesh mesh_fn ( houtput.asMesh(), &status);
        MColorArray colors;
        MIntArray vertex_idx;
        for (geom_it.reset(); !geom_it.isDone(); geom_it.next()) {
            colors.append(1, 0, 0, 1);

        mesh_fn.setVertexColors(colors, vertex_idx);
    catch (std::exception& e)
        maya_print_error( e );
        return MS::kFailure;
    return MStatus::kSuccess;

// -----------------------------------------------------------------------------

void Inherits_mpxdeformer::postConstructor()
    mayaCheck( MPxDeformerNode::setDeformationDetails(MPxDeformerNode::kDeformsColors) );

Original post here

No comments

All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.
Spam bot question: