Ejemplo n.º 1
0
        public static IINode FlattenHierarchyMS(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();

            string groupName = node.Name + Random.Next(1, 100000);

            node.Name = groupName;
            //use the default one in the future. for some reason uv ar broken with C# version
            ScriptsUtilities.ExecuteMaxScriptCommand($@"
            groupNode = maxOps.getNodeByHandle {node.Handle.ToString()}
	        meshChildren = #()	
	        for c in groupNode.children do
	        (
               if superclassof c == GeometryClass then
		        (
			        append meshChildren  c
		        )
	        )
	        obj = mesh vertices:#([0,0,0],[10,0,0],[0,10,0]) faces:#([1,2,3]) name:groupNode.name transform:groupNode.transform
	        converttopoly obj
	        multimat_new=#()
	        for obj_temp in meshChildren do	
	        (
		        obj_temp.parent=undefined
		        converttomesh	obj_temp		
		        resetXform	obj_temp			
		        converttomesh	obj_temp		
		        local save_mat=obj_temp.material			
		        if classof  save_mat == Multimaterial then
		        (
			        ID_true_used=#{{}}
			        T_Faces_matID=#()
			        T_Faces_matID.count=save_mat.count
			        for f=1 to obj_temp.numfaces do	
			        (
				        local face_matID=(getFaceMatID  obj_temp	f)
				        if not(ID_true_used[face_matID]) then 
				        (
					        T_Faces_matID[face_matID]=#{{f}}
					        ID_true_used[face_matID]=true
				        )
				        else
				        (
					        append  (T_Faces_matID[face_matID])	 f
				        )
			        )
			        for index_mat in ID_true_used do
			        (
				        mat_current=save_mat[index_mat]
				
				        matID=finditem multimat_new mat_current
				        if matID==0 then
				        (
					        append multimat_new mat_current
					        new_Id=multimat_new.count

                            for f in (T_Faces_matID[index_mat]) do setFaceMatID obj_temp  f       new_Id
                        )
                        else for f in (T_Faces_matID[index_mat]) do setFaceMatID obj_temp  f       matID
                    )
                )
                else
                (
                    mat_current = save_mat
                    matID = finditem multimat_new mat_current
                    if matID == 0 then
                    (
                        append multimat_new mat_current
                        new_Id = multimat_new.count
                        for f = 1 to obj_temp.numfaces do setFaceMatID obj_temp  f       new_Id
			        )
                    else for f = 1 to obj_temp.numfaces do setFaceMatID obj_temp  f       matID
		        )
		        obj_temp.material = undefined
                with undo off(polyop.attach  obj obj_temp)
	        )
	        final_mat = multimaterial numsubs: (multimat_new.count)
            final_mat.name = obj.name + ""_mat""
            for i = 1 to(multimat_new.count) do
            (
                if multimat_new[i] == undefined
                then(final_mat[i] = FlightSim()     ; final_mat[i].name = ""Newmat""    )
                else final_mat[i] = multimat_new[i]
	        )
	        polyop.deleteFaces obj 1
            obj.material = final_mat
            obj.parent = groupNode.parent");
            Loader.Core.DeleteNode(node, false, false);
            IINode newNode = Loader.Core.GetINodeByName(groupName);

            foreach (IINode n in nonMeshChildren)
            {
                newNode.AttachChild(n, true);
            }
            return(newNode);
        }
Ejemplo n.º 2
0
        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);
        }