public static void FlattenHierarchy(this IINode node) { ////todo: replace this with C# string convertToEditablePoly = $"ConvertTo (maxOps.getNodeByHandle {node.Handle}) Editable_Poly"; ScriptsUtilities.ExecuteMaxScriptCommand(convertToEditablePoly); IPolyObject polyObject = node.GetPolyObjectFromNode(); IEPoly flattenEPoly = (IEPoly)polyObject.GetInterface(Loader.EditablePoly); IINodeTab toflatten = Loader.Global.NodeTab.Create(); foreach (IINode n in node.NodeTree()) { toflatten.AppendNode(n, false, 1); } flattenEPoly.EpfnMultiAttach(toflatten, node, Loader.Core.Time); }
public static IINode FlattenHierarchy(this IINode node) { IILayer nodeLayer = LayerUtilities.GetNodeLayer(node); if (nodeLayer != null) { Loader.Core.LayerManager.SetCurrentLayer(nodeLayer.Name); } List <IINode> children = node.NodeTree().ToList(); List <IINode> nonMeshChildren = children.Where(x => !x.IsMesh()).ToList(); List <IINode> meshChildren = children.Where(x => x.IsMesh()).ToList(); IClass_ID cid = Loader.Global.Class_ID.Create((uint)BuiltInClassIDA.SPHERE_CLASS_ID, 0); object obj = Loader.Core.CreateInstance(SClass_ID.Geomobject, cid as IClass_ID); IINode result = Loader.Core.CreateObjectNode((IObject)obj); result.Name = node.Name; result.SetNodeTM(Loader.Core.Time, node.GetNodeTM(Loader.Core.Time, Forever)); node.ParentNode.AttachChild(result, true); string convertToEditablePoly = $"ConvertTo (maxOps.getNodeByHandle {result.Handle}) Editable_Poly"; ScriptsUtilities.ExecuteMaxScriptCommand(convertToEditablePoly); IPolyObject polyObject = result.GetPolyObjectFromNode(); IEPoly nodeEPoly = (IEPoly)polyObject.GetInterface(Loader.EditablePoly); List <IMtl> newMultiMat = new List <IMtl>(); foreach (IINode n in meshChildren) { ITriObject triObject = n.GetTriObjectFromNode(); IMesh mesh = triObject.Mesh; if (n.Mtl.IsMultiMtl) { Dictionary <IMtl, List <IFace> > matIDtoFacesMap = new Dictionary <IMtl, List <IFace> >(); ////List<int> matIDsInUse = new List<int>(); foreach (IFace face in mesh.Faces) { int faceMatID = face.MatID; IMtl mat = n.Mtl.GetSubMtl(faceMatID); if (!matIDtoFacesMap.ContainsKey(mat)) { List <IFace> facesOfMat = new List <IFace>(); matIDtoFacesMap.Add(mat, facesOfMat); } matIDtoFacesMap[mat].Add(face); } foreach (KeyValuePair <IMtl, List <IFace> > matToFaceKeyValue in matIDtoFacesMap) { IMtl faceMat = matToFaceKeyValue.Key; //get the material from the list of the multimaterial if (!newMultiMat.Contains(faceMat)) { newMultiMat.Add(faceMat); } ushort newId = (ushort)(newMultiMat.IndexOf(faceMat)); foreach (IFace face in matToFaceKeyValue.Value) { face.MatID = newId; } } } else { //IMtl currentMat = n.Mtl; //int matID = newMultiMat.IndexOf(currentMat); //if (matID == -1) //{ // newMultiMat.Add(currentMat); // ushort newId = (ushort)newMultiMat.Count; // for (int fID = 0; fID < mesh.NumFaces; fID++) // { // mesh.SetFaceMtlIndex(fID, (ushort)newId); // } //} //else //{ // for (int fID = 0; fID < mesh.NumFaces; fID++) // { // mesh.SetFaceMtlIndex(fID, (ushort)matID); // } //} } triObject.Mesh = mesh; n.ObjectRef = triObject; //bool undo = false; //nodeEPoly.EpfnAttach(n, ref undo,result, Loader.Core.Time); } IClass_ID matCid = Loader.Global.Class_ID.Create((uint)BuiltInClassIDA.MULTI_MATERIAL_CLASS_ID, 0); IMtl finalMat = (IMtl)Loader.Core.CreateInstance(SClass_ID.Material, matCid); for (int i = 0; i < newMultiMat.Count; i++) { finalMat.SetSubMtl(i, newMultiMat[i]); } finalMat.Name = "final mat"; result.Mtl = finalMat; foreach (IINode n in nonMeshChildren) { result.AttachChild(n, true); } Loader.Core.DeleteNode(node, false, false); return(result); }