public void Import() { BrgFile brg = this.File as BrgFile; Maxscript.Output.Clear(); Maxscript.Command("importStartTime = timeStamp()"); Maxscript.Command("frameRate = 30 --{0}", Math.Round(1 / this.File.Animation.TimeStep)); Maxscript.Interval(0, this.File.Animation.Duration); if (this.File.Meshes.Count > 0) { string mainObject = "mainObj"; for (int i = 0; i <= this.File.Meshes[0].MeshAnimations.Count; i++) { Maxscript.CommentTitle("ANIMATE FRAME " + i); if (i > 0) { ImportBrgMesh(mainObject, ((BrgMesh)brg.Meshes[0].MeshAnimations[i - 1]), brg.Animation.MeshKeys[i]); } else { ImportBrgMesh(mainObject, brg.Meshes[0], brg.Animation.MeshKeys[0]); } } Maxscript.Command("update {0}", mainObject); if (this.modelAtCenter) { Maxscript.Command("moveAmount = [0,0,0] - $Dummy_hotspot.position"); Maxscript.Command("max select all"); Maxscript.Command("move $ moveAmount"); } Maxscript.Command("max select none"); Maxscript.Command("max zoomext sel all"); Maxscript.Command("max zoomext sel all"); if (this.File.Materials.Count > 0) { Maxscript.CommentTitle("LOAD MATERIALS"); Maxscript.Command("matGroup = multimaterial numsubs:{0}", this.File.Materials.Count); for (int i = 0; i < this.File.Materials.Count; i++) { Maxscript.Command("matGroup[{0}] = {1}", i + 1, ImportBrgMaterial(brg.Materials[i])); Maxscript.Command("matGroup.materialIDList[{0}] = {1}", i + 1, this.File.Materials[i].Id); } Maxscript.Command("{0}.material = matGroup", mainObject); } } Maxscript.Command("importEndTime = timeStamp()"); Maxscript.Format("Import took % seconds\n", "((importEndTime - importStartTime) / 1000.0)"); }
public void Export() { BrgFile brg = this.File; Maxscript.Command("exportStartTime = timeStamp()"); //Maxscript.Command("print heapSize"); BrgMeshFlag flags = brg.Meshes[0].Header.Flags; BrgMeshFormat format = brg.Meshes[0].Header.Format; BrgMeshAnimType animationType = brg.Meshes[0].Header.AnimationType; BrgMeshInterpolationType interpolationType = brg.Meshes[0].Header.InterpolationType; Maxscript.Command("ExportBrgData()"); int totalNumVerts = Maxscript.QueryInteger("brgtotalNumVerts"); int totalNumFaces = Maxscript.QueryInteger("brgtotalNumFaces"); int meshCount = Maxscript.QueryInteger("brgMeshes.count"); if (meshCount == 0) { throw new Exception("No Editable_Mesh objects detected!"); } if (Maxscript.QueryBoolean("keys.Count == 0")) { throw new Exception("Could not acquire animation keys!"); } brg.Header.NumMeshes = Maxscript.QueryInteger("keys.Count"); brg.Animation = new Animation(); for (int i = 1; i <= brg.Header.NumMeshes; ++i) { brg.Animation.MeshKeys.Add(Maxscript.QueryFloat("keys[{0}]", i)); } if (brg.Header.NumMeshes == 1) { brg.Animation.Duration = 0; } else { brg.Animation.Duration = Maxscript.QueryFloat("(animationRange.end.ticks - animationRange.start.ticks) / 4800.0"); } brg.Animation.TimeStep = brg.Animation.Duration / (float)brg.Header.NumMeshes; string mainObject = "mainObject"; brg.Materials = new List <BrgMaterial>(); brg.Meshes = new List <BrgMesh>(brg.Header.NumMeshes); for (int m = 0; m < meshCount; ++m) { Maxscript.Command("{0} = brgMeshes[{1}]", mainObject, m + 1); // Materials Dictionary <int, int> matIdMapping = new Dictionary <int, int>(); if (Maxscript.QueryBoolean("classof {0}.material == Multimaterial", mainObject)) { brg.Header.NumMaterials = Maxscript.QueryInteger("{0}.material.materialList.count", mainObject); for (int i = 0; i < brg.Header.NumMaterials; i++) { BrgMaterial mat = new BrgMaterial(brg); mat.Id = brg.Materials.Count + 1; Maxscript.Command("mat = {0}.material.materialList[{1}]", mainObject, i + 1); this.ExportBrgMaterial(mainObject, mat); int matListIndex = brg.Materials.IndexOf(mat); int actualMatId = Maxscript.QueryInteger("{0}.material.materialIdList[{1}]", mainObject, i + 1); if (matListIndex >= 0) { if (!matIdMapping.ContainsKey(actualMatId)) { matIdMapping.Add(actualMatId, brg.Materials[matListIndex].Id); } } else { brg.Materials.Add(mat); if (matIdMapping.ContainsKey(actualMatId)) { matIdMapping[actualMatId] = mat.Id; } else { matIdMapping.Add(actualMatId, mat.Id); } } } } else if (Maxscript.QueryBoolean("classof {0}.material == Standardmaterial", mainObject)) { BrgMaterial mat = new BrgMaterial(brg); mat.Id = brg.Materials.Count + 1; Maxscript.Command("mat = {0}.material", mainObject); this.ExportBrgMaterial(mainObject, mat); int matListIndex = brg.Materials.IndexOf(mat); if (matListIndex >= 0) { matIdMapping.Add(1, brg.Materials[matListIndex].Id); } else { brg.Materials.Add(mat); matIdMapping.Add(1, mat.Id); } } else { if (flags.HasFlag(BrgMeshFlag.MATERIAL)) { throw new Exception("Not all meshes have a material applied! " + Maxscript.QueryString("{0}.name", mainObject)); } } // Mesh Animations for (int i = 0; i < brg.Header.NumMeshes; i++) { if (i > 0) { if (m == 0) { BrgMesh mesh = new BrgMesh(brg); mesh.Vertices = new List <Vector3>(totalNumVerts); mesh.Normals = new List <Vector3>(totalNumVerts); mesh.TextureCoordinates = new List <Vector3>(totalNumVerts); mesh.Faces = new List <Face>(totalNumFaces); brg.Meshes[0].MeshAnimations.Add(mesh); } brg.UpdateMeshSettings(i, flags, format, animationType, interpolationType); this.ExportBrgMesh(mainObject, (BrgMesh)brg.Meshes[0].MeshAnimations[i - 1], brg.Animation.MeshKeys[i], matIdMapping); } else { if (m == 0) { BrgMesh mesh = new BrgMesh(brg); mesh.Vertices = new List <Vector3>(totalNumVerts); mesh.Normals = new List <Vector3>(totalNumVerts); mesh.TextureCoordinates = new List <Vector3>(totalNumVerts); mesh.Faces = new List <Face>(totalNumFaces); brg.Meshes.Add(mesh); } brg.UpdateMeshSettings(i, flags, format, animationType, interpolationType); this.ExportBrgMesh(mainObject, brg.Meshes[i], brg.Animation.MeshKeys[i], matIdMapping); } } } // Export Attachpoints, and Update some Mesh data HashSet <int> usedFaceMaterials = new HashSet <int>(); string attachDummy = Maxscript.NewArray("attachDummy"); Maxscript.Command("{0} = for helpObj in ($helpers/Dummy_*) where classof helpObj == Dummy collect helpObj", attachDummy);//"$helpers/Dummy_* as array"); for (int i = 0; i < brg.Header.NumMeshes; i++) { BrgMesh mesh; if (i > 0) { mesh = (BrgMesh)brg.Meshes[0].MeshAnimations[i - 1]; } else { mesh = brg.Meshes[i]; } this.ExportAttachpoints(attachDummy, mesh, brg.Animation.MeshKeys[i]); HashSet <int> diffFaceMats = new HashSet <int>(); if (!mesh.Header.Flags.HasFlag(BrgMeshFlag.SECONDARYMESH) && mesh.Header.Flags.HasFlag(BrgMeshFlag.MATERIAL)) { for (int j = 0; j < mesh.Faces.Count; ++j) { diffFaceMats.Add(mesh.Faces[j].MaterialIndex); } if (diffFaceMats.Count > 0) { mesh.ExtendedHeader.NumMaterials = (byte)(diffFaceMats.Count - 1); mesh.ExtendedHeader.NumUniqueMaterials = diffFaceMats.Count; } } usedFaceMaterials.UnionWith(diffFaceMats); mesh.ExtendedHeader.AnimationLength = this.File.Animation.Duration; } List <BrgMaterial> usedMats = new List <BrgMaterial>(brg.Materials.Count); for (int i = 0; i < brg.Materials.Count; ++i) { if (usedFaceMaterials.Contains(brg.Materials[i].Id)) { usedMats.Add(brg.Materials[i]); } } brg.Materials = usedMats; brg.Header.NumMaterials = brg.Materials.Count; //Maxscript.Command("print heapSize"); Maxscript.Command("exportEndTime = timeStamp()"); Maxscript.Format("Export took % seconds\n", "((exportEndTime - exportStartTime) / 1000.0)"); }