private static ToolResult CopyUsedTexturesObj(string objFile, string outputDirectory, string textureDirectory) { bool success = false; string message = ""; ObjData objData = ObjParser.TryParseObj(objFile); if (objData != null) { objData.UpdateMtlData(); MtlData mtlData = objData.Mtl; if (mtlData != null) { success = ObjModifier.CopyUsedTextures(mtlData, outputDirectory, textureDirectory); message = MessageBoxConstants.GetMessageExecutionGeneric(success); } else { message = MessageBoxConstants.MessageErrorCopyTextures + MessageBoxConstants.MessageMtlNotFound; } } else { message = MessageBoxConstants.GetMessageExecutionErrorParse(objFile); } return(new ToolResult(message, success)); }
private static ToolResult ConvertObjToSmd(string objFile, string smdOutput, bool useTextureName) { bool success = false; string message = ""; ObjData objData = ObjParser.TryParseObj(objFile); if (objData != null) { objData.UpdateMtlData(); MtlData mtlData = objData.Mtl; if (mtlData == null) { // No MTL means the SMD will use a default material name for all objects useTextureName = false; } success = SmdExporter.WriteSmd(objData, mtlData, smdOutput, useTextureName); message = MessageBoxConstants.GetMessageExecutionCreation(success, smdOutput); } else { message = MessageBoxConstants.GetMessageExecutionErrorParse(objFile); } return(new ToolResult(message, success)); }
private static ToolResult DeleteUnusedMaterialsObj(string objFile, string objOutput) { bool success = false; string message = ""; ObjData objData = ObjParser.TryParseObj(objFile); if (objData != null) { objData.UpdateMtlData(); MtlData mtlData = objData.Mtl; if (mtlData != null) { ObjModifier.DeleteUnusedMaterials(objData, mtlData); success = ObjExporter.WriteObj(objData, mtlData, objOutput, makeMtl: true, useExistingMtl: true); message = MessageBoxConstants.GetMessageExecutionCreation(success, objOutput); } else { message = MessageBoxConstants.MessageErrorDeleteUnusedMaterials + MessageBoxConstants.MessageMtlNotFound; } } else { message = MessageBoxConstants.GetMessageExecutionErrorParse(objFile); } return(new ToolResult(message, success)); }
/// <summary> /// Parse the obj file to make the data useable /// </summary> /// <param name="file">File to parse</param> /// <param name="parseMtl">Parse the mtl file directly</param> /// <returns></returns> public static ObjData ParseObj(string file, bool parseMtl = true) { string[] lines = GenericUtils.RemoveBlankSpaces(FileUtilsShared.ReadFile(file)); // Read the file and remove blankspaces if (lines != null) { UpdateParseLineGroup(lines); AllVerticesDataObj verticesData = new AllVerticesDataObj(); // To store the vertices data // Store all the other data to parse List <Tuple <string, string> > listKeysValues = GetListKeysValues(lines, verticesData); ObjData objData = new ObjData(file, verticesData.MtlLib); lines = null; // To let the garbage collector free the file from memory int i = 0; int length = listKeysValues.Count; while (i < length) { Tuple <string, string> keyValue = listKeysValues[i]; if (keyValue != null) { if (isObjectGroup(keyValue.Item1)) { int objectStart = i; int objectEnd = GetIndexEnd(listKeysValues, length, objectStart); if (objectEnd != -1) { objData.ObjectsList.Add(ObjectData(listKeysValues, verticesData, objectStart, objectEnd)); i = objectEnd; // Set to next object continue; } } } i++; // Unless error or final line } if (parseMtl) { objData.UpdateMtlData(); } return(objData); } return(null); }
private static ToolResult ModifyObj(string objFile, string objOutput, ObjOptions objOptions, Dictionary <string, string> inputs) { bool success = false; bool warn = false; StringBuilder sb = new StringBuilder(); ObjData objData = ObjParser.TryParseObj(objFile); if (objData != null) { objData.UpdateMtlData(); MtlData mtlData = objData.Mtl; if (objOptions.HasFlag(ObjOptions.BlackList)) { // Delete Blacklisted Objects first if (mtlData != null) { if (!ObjModifier.TryDeleteMatchingGroups(objData, mtlData, texturesList)) { warn = true; sb.AppendLine(MessageBoxConstants.MessageErrorDeleteBlackList + MessageBoxConstants.MessageErrorExecution); } } else { warn = true; sb.AppendLine(MessageBoxConstants.MessageErrorDeleteBlackList + MessageBoxConstants.MessageMtlNotFound); } } ObjModifier.CalculateNormal(objData); if (objOptions.HasFlag(ObjOptions.UniformScale)) { if (inputs.TryGetValue(DictConstants.ScaleValue, out string scaleStr)) { if (Double.TryParse(scaleStr, out double scaleValue)) { ObjModifier.UniformScale(objData, scaleValue); // Scale Model } else { warn = true; sb.AppendLine(MessageBoxConstants.MessageErrorScale + MessageBoxConstants.MessageInvalidScaleValue); } } } if (objOptions.HasFlag(ObjOptions.NonUniformScale)) { bool isScaleValueX = false; bool isScaleValueY = false; bool isScaleValueZ = false; double scaleValueX = 0.0; double scaleValueY = 0.0; double scaleValueZ = 0.0; if (inputs.TryGetValue(DictConstants.ScaleValueX, out string scaleStr)) { isScaleValueX = Double.TryParse(scaleStr, out scaleValueX); } if (inputs.TryGetValue(DictConstants.ScaleValueY, out scaleStr)) { isScaleValueY = Double.TryParse(scaleStr, out scaleValueY); } if (inputs.TryGetValue(DictConstants.ScaleValueZ, out scaleStr)) { isScaleValueZ = Double.TryParse(scaleStr, out scaleValueZ); } if (isScaleValueX && isScaleValueY && isScaleValueZ) { // Scale Model ObjModifier.NonUniformScale(objData, scaleValueX, scaleValueY, scaleValueZ); } else { warn = true; sb.AppendLine(MessageBoxConstants.MessageErrorScale + MessageBoxConstants.MessageInvalidScaleValues); } } if (objOptions.HasFlag(ObjOptions.Rotate)) { bool isRotateValueX = false; bool isRotateValueY = false; bool isRotateValueZ = false; double rotateValueX = 0.0; double rotateValueY = 0.0; double rotateValueZ = 0.0; if (inputs.TryGetValue(DictConstants.RotateValueX, out string rotationStr)) { isRotateValueX = Double.TryParse(rotationStr, out rotateValueX); } if (inputs.TryGetValue(DictConstants.RotateValueY, out rotationStr)) { isRotateValueY = Double.TryParse(rotationStr, out rotateValueY); } if (inputs.TryGetValue(DictConstants.RotateValueZ, out rotationStr)) { isRotateValueZ = Double.TryParse(rotationStr, out rotateValueZ); } if (isRotateValueX && isRotateValueY && isRotateValueZ) { // Rotate Model ObjModifier.RotateModel(objData, rotateValueX, rotateValueY, rotateValueZ); } else { warn = true; sb.AppendLine(MessageBoxConstants.MessageErrorRotate + MessageBoxConstants.MessageInvalidRotateValues); } } if (objOptions.HasFlag(ObjOptions.ReverseVertex)) { // Reverse Vertex Order ObjModifier.ReverseVertexOrder(objData); } if (objOptions.HasFlag(ObjOptions.Merge)) { if (mtlData != null) { // Merge groups and materials ObjModifier.MergeGroups(objData, mtlData); } else { warn = true; sb.AppendLine(MessageBoxConstants.MessageErrorMergeGroups + MessageBoxConstants.MessageMtlNotFound); } } if (objOptions.HasFlag(ObjOptions.Sort)) { // Sort groups if (!ObjModifier.SortGroups(objData)) { warn = true; sb.AppendLine(MessageBoxConstants.MessageErrorSortGroups + MessageBoxConstants.MessageInvalidGroupName); } if (mtlData != null) { // Sort materials if (!ObjModifier.SortMaterials(mtlData)) { warn = true; sb.AppendLine(MessageBoxConstants.MessageErrorSortMaterials + MessageBoxConstants.MessageInvalidMaterialName); } } else { warn = true; sb.AppendLine(MessageBoxConstants.MessageErrorSortMaterials + MessageBoxConstants.MessageMtlNotFound); } } success = ObjExporter.WriteObj(objData, mtlData, objOutput, makeMtl: true, useExistingMtl: true); sb.Append(MessageBoxConstants.GetMessageExecutionCreation(success, objOutput)); } else { sb.Append(MessageBoxConstants.GetMessageExecutionErrorParse(objFile)); } return(new ToolResult(sb.ToString(), success, warn)); }