@cheng_xi_li, I have been doing more testing, and I get correct results using the maya.OpenMayaRender::MRenderUtil::sampleShadingNetwork python api, but only from the script editor.
In my c++ node, I tested both the c++ api as well as calling python code via MGlobal::executePythonCommandStringResult( pythonCommand, true, true );
For some reason neither give me valid results, i.e. all colors come back as 0,0,0).
This is the code I call from c++:
MFloatArray uX, vX;
if( !mFnMesh->getUVs( uX, vX ) ) return false;
MString us;
MString vs;
for( uint32_t i = 0; i < uX.length(); ++i )
{
us += uX[ i ]; if( i != uX.length() - 1 ) us += " ";
vs += vX[ i ]; if( i != uX.length() - 1 ) vs += " ";
}
MFloatMatrix matrix;
MFloatVectorArray colors, transparency;
MRenderUtil::sampleShadingNetwork( "layeredTexture1.outColor", uX.length(), false, false, matrix, nullptr, &uX, &vX, nullptr, nullptr, nullptr, nullptr, nullptr, colors, transparency );
const uint32_t cl = colors.length();
MString cs = "Num colors: ";
cs += cl;
for( uint32_t i = 0; i < cl; ++i )
{
cs += "( "; cs += colors[ i ][ 0 ];
cs += ", "; cs += colors[ i ][ 1 ];
cs += ", "; cs += colors[ i ][ 2 ]; cs += "), ";
}
MGlobal::displayInfo( cs + "\n" );
MString pythonCommand;
pythonCommand +=
pythonCommand += "import maya.cmds as cmds\n";
pythonCommand += "import maya.OpenMaya as om\n";
pythonCommand += "import maya.OpenMayaRender as omr\n\n";
pythonCommand += "def SampleLayeredTexture():\n";
pythonCommand += " cam_matrix = om.MFloatMatrix() \n";
pythonCommand += " uString = '" + us + "'\n";
pythonCommand += " vString = '" + vs + "'\n";
pythonCommand += " uParts = uString.split(' ')\n";
pythonCommand += " vParts = vString.split(' ')\n";
pythonCommand += " uArray = om.MFloatArray() #array for U coords\n";
pythonCommand += " vArray = om.MFloatArray() #array for V coords\n";
pythonCommand += " uArray.setLength(len(uParts))\n";
pythonCommand += " for i in range(0,len(uParts)):\n";
pythonCommand += " uArray[i] = float(uParts[i])\n";
pythonCommand += " vArray.setLength(len(vParts))\n";
pythonCommand += " for i in range(0,len(vParts)):\n";
pythonCommand += " vArray[i] = float(vParts[i])\n";
pythonCommand += " color = om.MFloatVectorArray()\n";
pythonCommand += " alpha = om.MFloatVectorArray()\n";
pythonCommand += " omr.MRenderUtil.sampleShadingNetwork('layeredTexture1.outColor',\n";
pythonCommand += " len(uArray),\n";
pythonCommand += " False,\n";
pythonCommand += " False,\n";
pythonCommand += " cam_matrix,\n";
pythonCommand += " None,\n";
pythonCommand += " uArray,\n";
pythonCommand += " vArray,\n";
pythonCommand += " None,\n";
pythonCommand += " None,\n";
pythonCommand += " None,\n";
pythonCommand += " None,\n";
pythonCommand += " None,\n";
pythonCommand += " color,\n";
pythonCommand += " alpha)\n";
pythonCommand += " color_result = [(color[i].x, color[i].y, color[i].z) for i in range(color.length())]\n";
pythonCommand += " print(color_result)\n";
pythonCommand += " return str(color_result)\n";
pythonCommand += "SampleLayeredTexture()\n";
MString commandResults = MGlobal::executePythonCommandStringResult( pythonCommand, true, true );
MGlobal::displayInfo( MString( "Command Results: " ) + commandResults );
Both python and c++ give me only zero colors.
Line 60 prints out the python results and I only get (0,0,0) results.
Now, the command echoes to the script editor, so I can literally copy and paste the code into a python executor:
import maya.cmds as cmds
import maya.cmds as cmds
import maya.OpenMaya as om
import maya.OpenMayaRender as omr
def SampleLayeredTexture():
cam_matrix = om.MFloatMatrix()
uString = '-0.590161 -0.490161 -0.490161 -0.590161 -0.390161 0.2 0.3 0.3 0.4 0.4 0.5 0.5 0.6 1.322534 1.422534 1.422534 1.522534 1.522534 1.622534 1.622534 1.722534 1.722534 -0.490161 -0.590161 0.2 0.3 0.4 0.5 1.322534 1.422534 1.522534 1.622534 1.722534 -0.490161 -0.590161 0.2 0.3 0.4 0.5 1.322534 1.422534 1.522534 1.622534 1.722534 -0.490161 -0.590161 0.2 0.3 0.4 0.5 1.322534 1.422534 1.522534 1.622534 1.722534 -0.490161 -0.590161 0.2 0.3 0.4 0.5 1.322534 1.422534 1.522534 1.622534 1.722534 -0.490161 -0.590161 0.2 0.3 0.4 0.5 1.322534 1.422534 1.522534 1.622534 1.722534 -0.490161 -0.590161 0.2 0.3 0.4 0.5 1.322534 1.422534 1.522534 1.622534 1.722534 -0.490161 -0.590161 0.2 0.3 0.4 0.5 1.322534 1.422534 1.522534 1.622534 1.722534 -0.490161 -0.590161 0.2 0.3 0.4 0.5 1.322534 1.422534 1.522534 1.622534 1.722534 -0.490161 -0.590161 0.2 0.3 0.4 0.5 1.322534 1.422534 1.522534 1.622534 1.722534 -0.390161 -0.390161 -0.390161 -0.390161 -0.390161 -0.390161 -0.390161 -0.390161 -0.390161 -0.390161 0.2 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 1.322534 -0.540161 -0.440161 0.25 0.35 0.45 0.55 1.372534 1.472534 1.572534 1.672534 -0.540161 -0.440161 0.25 0.35 0.45 0.55 1.372534 1.472534 1.572534 1.672534 -0.540161 -0.440161 0.25 0.35 0.45 0.55 1.372534 1.472534 1.572534 1.672534 -0.540161 -0.440161 0.25 0.35 0.45 0.55 1.372534 1.472534 1.572534 1.672534 -0.540161 -0.440161 0.25 0.35 0.45 0.55 1.372534 1.472534 1.572534 1.672534 -0.540161 -0.440161 0.25 0.35 0.45 0.55 1.372534 1.472534 1.572534 1.672534 -0.540161 -0.440161 0.25 0.35 0.45 0.55 1.372534 1.472534 1.572534 1.672534 -0.540161 -0.440161 0.25 0.35 0.45 0.55 1.372534 1.472534 1.572534 1.672534 -0.540161 -0.440161 0.25 0.35 0.45 0.55 1.372534 1.472534 1.572534 1.672534 -0.540161 -0.440161 0.25 0.35 0.45 0.55 1.372534 1.472534 1.572534 1.672534'
vString = '0.224719 0.224719 0.279775 0.279775 0.224719 0.279775 0.224719 0.279775 0.224719 0.279775 0.224719 0.279775 0.224719 0.279775 0.224719 0.279775 0.224719 0.279775 0.224719 0.279775 0.224719 0.279775 0.334831 0.334831 0.334831 0.334831 0.334831 0.334831 0.334831 0.334831 0.334831 0.334831 0.334831 0.389888 0.389888 0.389888 0.389888 0.389888 0.389888 0.389888 0.389888 0.389888 0.389888 0.389888 0.444944 0.444944 0.444944 0.444944 0.444944 0.444944 0.444944 0.444944 0.444944 0.444944 0.444944 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.555056 0.555056 0.555056 0.555056 0.555056 0.555056 0.555056 0.555056 0.555056 0.555056 0.555056 0.610112 0.610112 0.610112 0.610112 0.610112 0.610112 0.610112 0.610112 0.610112 0.610112 0.610112 0.665169 0.665169 0.665169 0.665169 0.665169 0.665169 0.665169 0.665169 0.665169 0.665169 0.665169 0.720225 0.720225 0.720225 0.720225 0.720225 0.720225 0.720225 0.720225 0.720225 0.720225 0.720225 0.775281 0.775281 0.775281 0.775281 0.775281 0.775281 0.775281 0.775281 0.775281 0.775281 0.775281 0.720225 0.775281 0.665169 0.610112 0.555056 0.5 0.444944 0.389888 0.334831 0.279775 0.224719 0.720225 0.775281 0.665169 0.610112 0.555056 0.5 0.444944 0.389888 0.334831 0.279775 0.224719 0.252247 0.252247 0.252247 0.252247 0.252247 0.252247 0.252247 0.252247 0.252247 0.252247 0.307303 0.307303 0.307303 0.307303 0.307303 0.307303 0.307303 0.307303 0.307303 0.307303 0.36236 0.36236 0.36236 0.36236 0.36236 0.36236 0.36236 0.36236 0.36236 0.36236 0.417416 0.417416 0.417416 0.417416 0.417416 0.417416 0.417416 0.417416 0.417416 0.417416 0.472472 0.472472 0.472472 0.472472 0.472472 0.472472 0.472472 0.472472 0.472472 0.472472 0.527528 0.527528 0.527528 0.527528 0.527528 0.527528 0.527528 0.527528 0.527528 0.527528 0.582584 0.582584 0.582584 0.582584 0.582584 0.582584 0.582584 0.582584 0.582584 0.582584 0.63764 0.63764 0.63764 0.63764 0.63764 0.63764 0.63764 0.63764 0.63764 0.63764 0.692697 0.692697 0.692697 0.692697 0.692697 0.692697 0.692697 0.692697 0.692697 0.692697 0.747753 0.747753 0.747753 0.747753 0.747753 0.747753 0.747753 0.747753 0.747753 0.747753'
uParts = uString.split(' ')
vParts = vString.split(' ')
uArray = om.MFloatArray() #array for U coords
vArray = om.MFloatArray() #array for V coords
uArray.setLength(len(uParts))
for i in range(0,len(uParts)):
uArray[i] = float(uParts[i])
vArray.setLength(len(vParts))
for i in range(0,len(vParts)):
vArray[i] = float(vParts[i])
color = om.MFloatVectorArray()
alpha = om.MFloatVectorArray()
omr.MRenderUtil.sampleShadingNetwork('layeredTexture1.outColor',
len(uArray),
False,
False,
cam_matrix,
None,
uArray,
vArray,
None,
None,
None,
None,
None,
color,
alpha)
color_result = [(color[i].x, color[i].y, color[i].z) for i in range(color.length())]
print(color_result)
return str(color_result)
SampleLayeredTexture()
And there I get the correct results.
So something about it being called from c++ messes up the functionality.
Any idea why and what's going on?
Cheers,
Sebastian