private void ImportMesh(GrnMesh mesh, string mainObject, string boneArray)
{
string vertArray = "";
string texVerts = "";
string faceMats = "";
string faceArray = "";
string tFaceArray = "";
vertArray = Maxscript.NewArray("vertArray");
texVerts = Maxscript.NewArray("texVerts");
faceMats = Maxscript.NewArray("faceMats");
faceArray = Maxscript.NewArray("faceArray");
tFaceArray = Maxscript.NewArray("tFaceArray");
for (int i = 0; i < mesh.Vertices.Count; ++i)
{
Maxscript.Append(vertArray, Maxscript.Point3Literal(mesh.Vertices[i]));
}
for (int i = 0; i < mesh.TextureCoordinates.Count; ++i)
{
Maxscript.Append(texVerts, Maxscript.Point3Literal(mesh.TextureCoordinates[i].X, 1f-mesh.TextureCoordinates[i].Y, mesh.TextureCoordinates[i].Z));
}
foreach (var face in mesh.Faces)
{
Maxscript.Append(faceMats, face.MaterialIndex + 1);
Maxscript.Append(faceArray, Maxscript.Point3Literal(face.Indices[0] + 1, face.Indices[1] + 1, face.Indices[2] + 1));
Maxscript.Append(tFaceArray, Maxscript.Point3Literal(face.TextureIndices[0] + 1, face.TextureIndices[1] + 1, face.TextureIndices[2] + 1));
}
Maxscript.Command("meshBone = getNodeByName \"{0}\"", mesh.Name);
Maxscript.Command("{5} = mesh name:\"{0}\" vertices:{1} faces:{2} materialIDs:{3} tverts:{4}", mesh.Name, vertArray, faceArray, faceMats, texVerts, mainObject);
if (Maxscript.QueryBoolean("meshBone != undefined"))
{
Maxscript.Command("{0}.parent = meshBone.parent", mainObject);
Maxscript.Command("delete meshBone");
}
Maxscript.CommentTitle("TVert Hack"); // Needed <= 3ds Max 2014; idk about 2015+
Maxscript.Command("buildTVFaces {0}", mainObject);
for (int i = 0; i < mesh.Faces.Count; ++i)
{
Maxscript.Command("setTVFace {0} {1} {2}[{1}]", mainObject, i + 1, tFaceArray);
}
Maxscript.Command("max modify mode");
Maxscript.Command("select {0}", mainObject);
Maxscript.Command("addModifier {0} (Edit_Normals()) ui:off", mainObject);
Maxscript.Command("modPanel.setCurrentObject {0}.modifiers[#edit_normals]", mainObject);
Maxscript.Command("{0}.modifiers[#edit_normals].Break selection:#{{1..{1}}}", mainObject, mesh.Normals.Count);
Maxscript.Command("meshSetNormalIdFunc = {0}.modifiers[#edit_normals].SetNormalID", mainObject);
for (int i = 0; i < mesh.Faces.Count; ++i)
{
Maxscript.Command("meshSetNormalIdFunc {0} {1} {2}",
i + 1, 1, mesh.Faces[i].NormalIndices[0] + 1);
Maxscript.Command("meshSetNormalIdFunc {0} {1} {2}",
i + 1, 2, mesh.Faces[i].NormalIndices[1] + 1);
Maxscript.Command("meshSetNormalIdFunc {0} {1} {2}",
i + 1, 3, mesh.Faces[i].NormalIndices[2] + 1);
}
Maxscript.Command("{0}.modifiers[#edit_normals].MakeExplicit selection:#{{1..{1}}}", mainObject, mesh.Normals.Count);
Maxscript.Command("meshSetNormalFunc = {0}.modifiers[#edit_normals].SetNormal", mainObject);
for (int i = 0; i < mesh.Normals.Count; i++)
{
Maxscript.Command("meshSetNormalFunc {0} {1}", i + 1, Maxscript.Point3Literal(mesh.Normals[i]));
}
Maxscript.Command("collapseStack {0}", mainObject);
// Bones
Maxscript.Command("skinMod = Skin()");
Maxscript.Command("addModifier {0} skinMod", mainObject);
Maxscript.Command("modPanel.setCurrentObject skinMod");
Maxscript.Command("skinAddBoneFunc = skinOps.addBone");
for (int i = 0; i < mesh.BoneBindings.Count; ++i)
{
GrnBoneBinding boneBinding = mesh.BoneBindings[i];
string boundingScale = Maxscript.NewPoint3<float>("boundingScale",
(boneBinding.OBBMax.X - boneBinding.OBBMin.X), (boneBinding.OBBMax.Y - boneBinding.OBBMin.Y), (boneBinding.OBBMax.Z - boneBinding.OBBMin.Z));
Maxscript.Command("{0}[{1}].boxsize = {2}", boneArray, this.boneMap[this.File.Bones[boneBinding.BoneIndex].Name] + 1, boundingScale);
Maxscript.Command("skinAddBoneFunc skinMod {0}[{1}] {2}", boneArray,
this.boneMap[this.File.Bones[boneBinding.BoneIndex].Name] + 1, i + 1 == mesh.BoneBindings.Count ? 1 : 0);
}
Maxscript.Command("completeRedraw()"); // would get "Exceeded the vertex countSkin:skin" error without this
Maxscript.Command("skinReplaceVertWeightsFunc = skinOps.ReplaceVertexWeights");
for (int i = 0; i < mesh.VertexWeights.Count; ++i)
{
// Index correspond to order that the bones were added to Skin mod
string boneIndexArray = Maxscript.NewArray("boneIndexArray");
string weightsArray = Maxscript.NewArray("weightsArray");
for (int j = 0; j < mesh.VertexWeights[i].Weights.Count; ++j)
{
Maxscript.Append(boneIndexArray, mesh.VertexWeights[i].BoneIndices[j] + 1);
Maxscript.Append(weightsArray, mesh.VertexWeights[i].Weights[j]);
}
Maxscript.Command("skinReplaceVertWeightsFunc skinMod {0} {1} {2}",
i + 1, 1, 0.0);
Maxscript.Command("skinReplaceVertWeightsFunc skinMod {0} {1} {2}",
i + 1, boneIndexArray, weightsArray);
}
}