Get faces by Material ID (IIGameMesh)

 
 
 
Posted by:Vincentt
Data created:23 December 2014

Hello,

 

I’m trying to select faces by Material ID. I found this method in the Object Browser:

Autodesk.Max.IIGameMesh.GetFacesFromMatID(int matID). This seems to be exactly what I want, however I don’t seem to be able to cast any of my objects to IIGameMesh.

IIGameMesh is apparently a “Simple wrapper for tri mesh objects.” I tried doing this:

 

for (int i = 0; i < iface.SelNodeCount; i++)

{

IINode node = iface.GetSelNode(i);

ITriObject triObj = (ITriObject)node.EvalWorldState(0, true).Obj.ConvertToType(0, m_Global.TriObjectClassID);

var mesh = triObj.Mesh;

IIGameMesh doesntWork = mesh as IIGameMesh;

....

}

 

Error: Object reference not set to an instance of an object.

I thought since it was a simple wrapper I could cast the mesh I got from TriObj.

I’m using Max2012 (64bit)(I found a similar question but with no answer unfortunately: http://www.ephere.com/autodesk/max/forums/general/thread_1852.html)

 

If there is another/better way of getting faces by Material ID I’d be all ears too :)

 

Thanks in advance,

Vin

You would typically need to use GetInterface function to get an instance of IIGameMesh interface, however, I just checked and it is not UpCast(ed?) so I don't think that'll work. How about just iterating over the IMesh faces and looking at their material ids?

Marsel Khadiyev (Software Developer, EPHERE Inc.)

Hey Marsel,

 

Thanks for you reply, I was already thinking of that as a work around, I'll try that! Smile

I seem to be stuck at a certain point. The main idea of the program is to select faces by their Material ID and then move them. 

So far I have this:

Dictionary<ushort, List> testDict = new Dictionary<ushort, List>();

TriObject triObj = (ITriObject)node.EvalWorldState(0, true).Obj.ConvertToType(0, m_Global.TriObjectClassID);

for (int index = 0; index < mesh.Faces.Count; index++)
{
       var face = mesh.Faces[index];
       ushort matId = face.MatID;

       if (testDict.ContainsKey(matId))
              testDict[matId].Add(index);
       else
              testDict.Add(matId, new List(){index});
}

I thought with this I would be saving the FaceID together with the MatID as key. So I'd be able to get a list of faceID's according to certain Key values (matID). However the index I'm saving is not the face index and I have no idea how to get it either. So now I'm wondering if anyone could help me finding the correct FaceID (the one you see when you select the face in Max or in the listener: $.EditablePoly.SetSelection #Face #{2} <-- "2"). 

Or am I wrong trying to get the same ID as maxscript and should I just use index to select my faces, but I also have no idea how... 

 

**EDIT:
I should've done some more research before posting this. I found out by checking mesh.FaceSel that the face index is correct afterall. Now I just need to find out how to set a selection based of a BitArray. 

 

Any help would be appreciated!