How to use setFaceColors with colorsets?

How to use setFaceColors with colorsets?

Anonymous
Not applicable
1,538 Views
6 Replies
Message 1 of 7

How to use setFaceColors with colorsets?

Anonymous
Not applicable

The function setColors allows to specify a colorset, the setFaceColors function does not have such an option.

 

I am currently using this code to color the faces:

 

MIntArray colorIds;
MColorArray colors;
double max_potential = potentials.max();
double min_potential = potentials.min();
for(uint i = 0; i < potentials.size(); i++) {
	float color = (potentials[i] - min_potential) / (max_potential - min_potential);
	colorIds.append(i);
	colors.append(MColor(color, 0, 0));
}
meshFn.setFaceColors(colors, colorIds);
MString potentialColorsName("potentialColors");
meshFn.assignColors(colorIds, &potentialColorsName);

The colorSet name in assignColors does not work, but the setFaceColors function does not even have an colorSet argument.

 

Should i use setColors and how do I determine there the face indices to set the colors there? In the end I want to have two colorSets for the same mesh, which can be selected in the color set editor to visualize different properties of my result mesh.

0 Likes
Accepted solutions (1)
1,539 Views
6 Replies
Replies (6)
Message 2 of 7

cheng_xi_li
Autodesk Support
Autodesk Support

Hi,

 

Do you created color set with createColorSetWithName or set active color set with setCurrentColorSetName first?

 

Yours,

Li

0 Likes
Message 3 of 7

Anonymous
Not applicable

I tried now

meshFn.setFaceColors(colors, colorIds);
MString potentialColorsName = meshFn.createColorSetWithName("potentialColors"); meshFn.assignColors(colorIds, &potentialColorsName);

But the set has still the name colorSet1.

But more important: How do it use setFaceColors with different colorsets? Do I need to get some index mapping to use setColors?

 

The input mesh in the node is polyTriangulate.asMesh() and the output mesh is a shape object.

0 Likes
Message 4 of 7

cheng_xi_li
Autodesk Support
Autodesk Support

Hi,

 

setColor should be called before assignColors. Here is a code snippet for how those functions should be used in python.

 

import maya.OpenMaya as om

#Select a shape first activeList = om.MSelectionList() om.MGlobal.getActiveSelectionList(activeList) dagPath = om.MDagPath() activeList.getDagPath(0, dagPath) dagPath.extendToShape() fnMesh = om.MFnMesh(dagPath) """ setFaceColors """ colorSet1 = fnMesh.createColorSetWithName("Blue") colorSet2 = fnMesh.createColorSetWithName("Red") blueColor = om.MColor(0,0,1.0) redColor = om.MColor(1.0,0,0) #setCurrentColorSetName first. fnMesh.setCurrentColorSetName(colorSet1) print fnMesh.currentColorSetName() for i in range(0,fnMesh.numPolygons()): fnMesh.setFaceColor(blueColor, i) fnMesh.setCurrentColorSetName(colorSet2) print fnMesh.currentColorSetName() for i in range(0,fnMesh.numPolygons()): fnMesh.setFaceColor(redColor, i) """ setColors/assignColors """ blueArraySet = fnMesh.createColorSetWithName("BlueArray") redArraySet = fnMesh.createColorSetWithName("RedArray") mixedArraySet = fnMesh.createColorSetWithName("MixedArray") oneColorIndices = om.MIntArray(4,0) mixedColorIndices = om.MIntArray() for i in range(4): mixedColorIndices.append(i) redColorArray = om.MColorArray() redColorArray.append(1.0,0,0) blueColorArray = om.MColorArray() blueColorArray.append(0,0,1.0) mixedColorArray = om.MColorArray() mixedColorArray.append(1.0,0,0) mixedColorArray.append(0,0,1.0) mixedColorArray.append(0,1.0,1.0) mixedColorArray.append(1.0,0,1.0) fnMesh.setColors(redColorArray,redArraySet) fnMesh.assignColors(oneColorIndices, redArraySet) fnMesh.setColors(blueColorArray,blueArraySet) fnMesh.assignColors(oneColorIndices, blueArraySet) fnMesh.setColors(mixedColorArray,mixedArraySet) fnMesh.assignColors(mixedColorIndices, mixedArraySet)

Hope it helps.

 

Yours,

Li

Message 5 of 7

Anonymous
Not applicable

It does not work this way for me.

 

Hmm, I think the problem could be, that i try to assign a color set to a mesh from .inputValue().asMesh() in a node, which does not have a name or dagPath.

I will try using a callback, which assigns the colors after the compute function to the output object (which has a dagPath). Or I try to set it via a node connection.

0 Likes
Message 6 of 7

Anonymous
Not applicable

My current status:

 

I can get the dagPath in connectionMade. When I store it, I can use MFnMesh to assign ColorSets in the nodeDirtyPlug callback.

But when the two colorsets are assigned, two createColorSet nodes are between the MPxNode and the Mesh(Shape) and connectionMade is triggered for the createColorSet nodes, which have no dagPath.

I cannot store the old dagPath, as it may be invalid (I do not know the shape at the end of the DG nodes), but I cannot get the new dagPath from the CreateColorSet nodes.

 

See https://forums.autodesk.com/t5/maya-programming/finding-a-shape-dag-node-through-the-dg/td-p/7168127 for the question how to retrieve the original shape. I am not even sure if this is the right solution, as there may be nodes with multiple output shapes in between.

 

I thought of tracking the shapes by adding a message connection in first call of connectionMade, but then again I do not know if the current DG path through the CreateColorSet nodes matches the original message connection.

0 Likes
Message 7 of 7

Anonymous
Not applicable
Accepted solution

I got it working now using createColorSetDataMesh(colorsetname) in the compute function and then assigning colors with setColors(colors, colorsetname) and assignColors(vertexIds, colors), where i just generate a colorarray with 3*number of triangles colors.

 

I think this is the most reasonable solution for now, even when I read that createColorSetDataMesh does not work in every DG setup.

0 Likes