InvalidCastException

 
 
 
Posted by:Benelli
Data created:3 April 2012

I'm trying to rewrite this example - http://download.autodesk.com/global/docs/3dsmaxsdk2012/en_us/files/GUID-511D17BA-88AE-42EE-9ADD-AED27495EC8-312.htm - using Max.NET.

global = Autodesk.Max.GlobalInterface.Instance;
inter = global.COREInterface13;
IINode node = inter.RootNode;
if (node == null)
    return;
    IMtl nodeMaterial = node.Mtl;
    if (nodeMaterial != null)
    {
        Mtl material = (Mtl)nodeMaterial;
        if (material != null)
        {
            if (AreEqual(material.ClassID, global.Class_ID.Create((uint)BuiltInClassIDA.DMTL_CLASS_ID, 0)))
            {
                StdMat stdMaterial = (StdMat)material; // exception #1
                Texmap tmap = (Texmap)nodeMaterial.GetSubTexmap(1);
                if (tmap != null && ClassIDsAreEqual(tmap.ClassID, global.Class_ID.Create((uint)BuiltInClassIDA.BMTEX_CLASS_ID, 0)))
                {
                    BitmapTex bitmapTex = (BitmapTex)tmap; // exception #2
                    StdUVGen uv = (StdUVGen)bitmapTex.UVGen;
                }
            }
        }
    }
AreEqual() function is from this topic: https://www.ephere.com/autodesk/max/forums/general/thread_1015.html
But I get similar exceptions here.
StdMat stdMaterial = (StdMat)material; line throws:
[System.InvalidCastException] = {"Unable to cast object of type 'Autodesk.Max.Wrappers.Mtl' to type 'Autodesk.Max.Wrappers.StdMat'."}
BitmapTex bitmapTex = (BitmapTex)tmap; line throws:
[System.InvalidCastException] = {"Unable to cast object of type 'Autodesk.Max.Wrappers.Texmap' to type 'Autodesk.Max.Wrappers.BitmapTex'."}
And when I set breakpoints there I see that type of material is really just Autodesk.Max.Wrappers.Mtl, but not Autodesk.Max.Wrappers.StdMat.
How this problem could be solved?