Exemplo n.º 1
0
        static string DoExport(string path, IEnumerable <Model> models, ObjOptions options)
        {
            string name      = Path.GetFileNameWithoutExtension(path);
            string directory = Path.GetDirectoryName(path);

            List <string> textures = null;
            string        obj, mat;

            if (ObjExporter.Export(name, models, out obj, out mat, out textures, options))
            {
                try
                {
                    FileUtility.WriteAllText(string.Format("{0}/{1}.obj", directory, name), obj);
                    FileUtility.WriteAllText(string.Format("{0}/{1}.mtl", directory, name.Replace(" ", "_")), mat);
                    CopyTextures(textures, directory);
                }
                catch (System.Exception e)
                {
                    Debug.LogWarning(string.Format("Failed writing obj to path: {0}\n{1}", string.Format("{0}/{1}.obj", path, name), e.ToString()));
                    return(null);
                }
            }
            else
            {
                Debug.LogWarning("No meshes selected.");
                return(null);
            }

            return(path);
        }
Exemplo n.º 2
0
    static void MenuInit()
    {
        List <string> textures;
        string        obj, mat;

        ObjOptions options = new ObjOptions()
        {
            applyTransforms = false
        };

        if (ObjExporter.Export("probuilder cube", MeshSelection.top.Select(x => new Model("Cube", x)), out obj, out mat, out textures, options))
        {
            System.IO.File.WriteAllText("/Users/karlh/Desktop/cube.obj", obj);
        }
    }
Exemplo n.º 3
0
    public static string ToObjData(Mesh mesh, ObjOptions options = null)
    {
        string name = mesh.name;

        if (string.IsNullOrEmpty(name))
        {
            name = name.Replace("(Clone)", "").Trim(' ');
        }
        if (string.IsNullOrEmpty(name))
        {
            name = "default";
        }
        var model = new ObjExporter.Model(mesh);

        ObjExporter.Export(name, new[] { model }, out string result, out string _, out List <string> _, options);
        return(result);
    }
Exemplo n.º 4
0
        private static ToolResult ConvertWrlToObj(string wrlFile, string objOutput, ObjOptions objOptions)
        {
            bool    success = false;
            string  message = "";
            WrlData wrlData = WrlParser.TryParseWrl(wrlFile);

            if (wrlData != null)
            {
                if (objOptions.HasFlag(ObjOptions.ReverseVertex))
                {
                    WrlModifier.ReverseVertexOrder(wrlData);
                }
                success = WrlExporter.WriteObj(wrlData, objOutput);
                message = MessageBoxConstants.GetMessageExecutionCreation(success, objOutput);
            }
            else
            {
                message = MessageBoxConstants.GetMessageExecutionErrorParse(wrlFile);
            }
            return(new ToolResult(message, success));
        }
Exemplo n.º 5
0
        private static ToolResult ConvertWrlsToObj(string directory, ObjOptions objOptions)
        {
            StringBuilder sb      = new StringBuilder();
            int           count   = 0;
            int           error   = 0;
            List <string> wrlList = FileUtils.GetFiles(directory, FileConstants.PatternExtensionWrl, true);

            foreach (string wrlFile in wrlList)
            {
                string     wrlDirectory     = System.IO.Path.GetDirectoryName(wrlFile);
                string     wrlFilenameNoExt = System.IO.Path.GetFileNameWithoutExtension(wrlFile);
                string     objOutput        = System.IO.Path.Combine(wrlDirectory, wrlFilenameNoExt + ".obj");
                ToolResult result           = ConvertWrlToObj(wrlFile, objOutput, objOptions);
                if (result.Success)
                {
                    count++;
                }
                else
                {
                    error++;
                }
            }
            string aa      = $"error{ ((error>0) ? "" :" " )}";
            bool   success = false;
            bool   warn    = false;

            if (error > 0)
            {
                warn = true;
                // Add a 's' to 'error' if there are more than one error
                sb.AppendLine($"{error} error{( error > 1 ? "s" : "") } converting wrl files");
            }
            if (count > 0)
            {
                success = true;
            }
            sb.Append($"Converted {count}/{wrlList.Count} wrl files located in '{directory}' and its subdirectories");
            return(new ToolResult(sb.ToString(), success, warn));
        }
Exemplo n.º 6
0
        /**
         *  Prompt user for a save file location and export meshes as Obj.
         */
        public static string ExportWithFileDialog(IEnumerable <ProBuilderMesh> meshes, bool asGroup = true, bool allowQuads = true, ObjOptions options = null)
        {
            if (meshes == null || !meshes.Any())
            {
                return(null);
            }

            IEnumerable <Model> models = allowQuads
                ? meshes.Select(x => new Model(x.gameObject.name, x))
                : meshes.Select(x => new Model(x.gameObject.name, x.mesh, x.GetComponent <MeshRenderer>().sharedMaterials, x.transform.localToWorldMatrix));

            string path = null, res = null;

            if (asGroup || models.Count() < 2)
            {
                ProBuilderMesh first = meshes.FirstOrDefault();
                string         name  = first != null ? first.name : "ProBuilderModel";
                path = UnityEditor.EditorUtility.SaveFilePanel("Export to Obj", "Assets", name, "obj");

                if (string.IsNullOrEmpty(path))
                {
                    return(null);
                }

                res = DoExport(path, models, options);
            }
            else
            {
                path = UnityEditor.EditorUtility.SaveFolderPanel("Export to Obj", "Assets", "");

                if (string.IsNullOrEmpty(path) || !Directory.Exists(path))
                {
                    return(null);
                }

                foreach (Model model in models)
                {
                    res = DoExport(string.Format("{0}/{1}.obj", path, model.name), new List <Model>()
                    {
                        model
                    }, options);
                }
            }

            return(res);
        }
Exemplo n.º 7
0
        public static ToolResult ToolObj(ActionN64 action, Dictionary <string, string> inputs, ObjOptions objOptions)
        {
            string outputFile = null;

            switch (action)
            {
            case ActionN64.WrlConversion:     // Convert Wrl to Obj
                if (inputs.TryGetValue(DictConstants.WrlFile, out string wrlFile))
                {
                    if (inputs.TryGetValue(DictConstants.ObjOutput, out outputFile))
                    {
                        return(ConvertWrlToObj(wrlFile, outputFile, objOptions));
                    }
                }
                break;

            case ActionN64.WrlsConversion:     // Convert Wrl files to Obj
                if (inputs.TryGetValue(DictConstants.WrlDirectory, out string wrlDirectory))
                {
                    return(ConvertWrlsToObj(wrlDirectory, objOptions));
                }
                break;

            case ActionN64.DeleteMaterials:     // Delete Obj Materials
                if (inputs.TryGetValue(DictConstants.ObjFile, out string objFile))
                {
                    if (inputs.TryGetValue(DictConstants.ObjOutput, out outputFile))
                    {
                        return(DeleteMaterialsObj(objFile, outputFile));
                    }
                }
                break;

            case ActionN64.DeleteUnusedMaterials:     // Delete Unused Obj Materials
                if (inputs.TryGetValue(DictConstants.ObjFile, out objFile))
                {
                    if (inputs.TryGetValue(DictConstants.ObjOutput, out outputFile))
                    {
                        return(DeleteUnusedMaterialsObj(objFile, outputFile));
                    }
                }
                break;

            case ActionN64.AddMaterials:     // Add Obj Materials
                if (inputs.TryGetValue(DictConstants.ObjFile, out objFile))
                {
                    if (inputs.TryGetValue(DictConstants.ObjOutput, out outputFile))
                    {
                        return(AddMaterialsObj(objFile, outputFile));
                    }
                }
                break;

            case ActionN64.ModifyObj:     // Modify Obj
                if (inputs.TryGetValue(DictConstants.ObjFile, out objFile))
                {
                    if (inputs.TryGetValue(DictConstants.ObjOutput, out outputFile))
                    {
                        return(ModifyObj(objFile, outputFile, objOptions, inputs));
                    }
                }
                break;

            case ActionN64.ObjToSmd:     // Obj to Smd
                if (inputs.TryGetValue(DictConstants.ObjFile, out objFile))
                {
                    if (inputs.TryGetValue(DictConstants.SmdOutput, out outputFile))
                    {
                        bool useTextureName = objOptions.HasFlag(ObjOptions.SmdUseTextureName);
                        return(ConvertObjToSmd(objFile, outputFile, useTextureName));
                    }
                }
                break;

            case ActionN64.RefModelSmd:     // Reference Model
                if (inputs.TryGetValue(DictConstants.ObjFile, out objFile))
                {
                    if (inputs.TryGetValue(DictConstants.SmdOutput, out outputFile))
                    {
                        return(ConvertRefModelSmd(objFile, outputFile, inputs));
                    }
                }
                break;

            case ActionN64.MergeObjFilesDirectory:     // Merge Obj Files in a Directory
                if (inputs.TryGetValue(DictConstants.ObjDirectory, out string objDirectory))
                {
                    if (inputs.TryGetValue(DictConstants.ObjOutput, out outputFile))
                    {
                        return(MergeObjFiles(objDirectory, outputFile));
                    }
                }
                break;

            case ActionN64.MergeObjFilesList:     // Merge Obj Files in a List
                if (inputs.TryGetValue(DictConstants.ObjOutput, out outputFile))
                {
                    return(MergeObjFiles(objFileList, outputFile));
                }
                break;
            }
            return(new ToolResult(ToolResultEnum.DefaultError));
        }
Exemplo n.º 8
0
        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));
        }