MRenderUtil::eval2dTexture does not work for Layered Texture.

MRenderUtil::eval2dTexture does not work for Layered Texture.

Sebastian_Wiendl
Enthusiast Enthusiast
751 Views
9 Replies
Message 1 of 10

MRenderUtil::eval2dTexture does not work for Layered Texture.

Sebastian_Wiendl
Enthusiast
Enthusiast

Hi all,

 

This is a c++ question regarding Maya 2022 and newer (tried 2023 and 2024 as well).

I am not sure if I am doing something wrong or not, but I am trying to evaluate a Layered Texture using MRenderUtil::eval2dTexture.

So far I have only used MRenderUtil::eval2dTexture for non-layered textures and never had a problem.

I guess I naively assumed it would support Layered Textures as well but this does not seem to be the case.

 

Any help would be much appreciated. Happy to use whatever workaround works (including mel and python scripting is necessary).

 

Cheers,

Sebastian

0 Likes
752 Views
9 Replies
Replies (9)
Message 2 of 10

cheng_xi_li
Autodesk Support
Autodesk Support

Hi,

 

I think for layeredTexture nodes, you can use MRenderUtil::sampleShadingNetwork instead. 

 

There is a sample(sampleCmd) using this API in the devkit. You can try it and check if it fits your need.

Hope it helps.

 

Yours,

Li

0 Likes
Message 3 of 10

Sebastian_Wiendl
Enthusiast
Enthusiast

Hi @cheng_xi_li,

 

Thanks for getting back to me.

 

Yes, I have been looking at this but it does not seem to work.

Have you tried this with a Layered Texture?

 

Cheers,

Sebastian

0 Likes
Message 4 of 10

Sebastian_Wiendl
Enthusiast
Enthusiast

@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

 

0 Likes
Message 5 of 10

Sebastian_Wiendl
Enthusiast
Enthusiast

Hi @cheng_xi_li,

 

I have tried this. Tried the c++ API and also the python API, called from c++ via MGlobal::executePythonCommandStringResult( pythonCommand, true, true );

 

Both give me zero vectors, i.e. all (0,0,0).

 

BUT, MGlobal::executePythonCommandStringResult( pythonCommand, true, true ); echoes the command in the script editor.

If I literally copy from there and execute from the Script Editor python executor, it gives me the correct results.

So it seems that there is an issue with context or something?

 

Any and all help would be much appreciated.

 

Cheers,

Sebastian

0 Likes
Message 6 of 10

cheng_xi_li
Autodesk Support
Autodesk Support

Hi,

 

I tried your code with Maya 2024.2, and it seems working fine.

sampletexture.png

My code and test scene are attached, please check it out.

 

Yours,

Li

0 Likes
Message 7 of 10

Sebastian_Wiendl
Enthusiast
Enthusiast

Hi @cheng_xi_li,

 

Thanks so much for following up.

Really appreciate it.

 

I can confirm that your testSampler example works.

My situation was a lot more complicated so I spent a lot of time trying to figure out where my use case differs from the simple example.

 

I have attached my command class. It works on your mb file from your zip.

As it turns out, the problem is that the MRenderUtil::sampleShadingNetwork function does not seem to work from within a MDGContextGuard.

 

Sebastian_Wiendl_0-1706721516857.png

 

I have never had an issue with the MRenderUtil::eval2dTexture from within a MDGContextGuard.

Is there a way to make MRenderUtil::sampleShadingNetwork work as well?

 

Thanks so much.

Cheers,

Sebastian

0 Likes
Message 8 of 10

Sebastian_Wiendl
Enthusiast
Enthusiast
Just wondering if you had a chance to look at my example with the MDGContextGuard?
I am struggling to find a work around.

Thanks so much,
Sebastian
0 Likes
Message 9 of 10

cheng_xi_li
Autodesk Support
Autodesk Support

Hi Sebastian,

 

Sorry for the delay. I am on vacation this month with my family. I found some spare time today and I tried your sample.

 

I couldn't find a workaround when using another context for this issue, so I've logged a change request on your behalf. I sent you the defect ID through another email, please check it out.

 

Yours,

Li

0 Likes
Message 10 of 10

Sebastian_Wiendl
Enthusiast
Enthusiast

Hi @cheng_xi_li,

 

Thank you very much for looking into this. I did not mean to disturb your vacation.

I have received the email about the Change Request. Much appreciated.

 

Cheers,

Sebastian

0 Likes