ZBrushCentral

Running Zscripts through a commandline?

Hi guys, i was wondering if there was a way to run ZBrush through a command line?
Basicly i would like to pass a zscript to zbrush through the command line and the zscript will perform a few 3D functions then output a OBJ file.

Is this possible?
Thanks.

I don’t think you can do it directly but it is possible to launch ZBrush from a separate executable, something along the lines of:

#include <windows.h>

static const char *zbrushFolder = “C:\Program Files (x86)\Pixologic\ZBrush 4R6”;
static const char *zbrushExe = “zbrush.exe”;
static const char *scriptPath = “C:\Users\Desktop\MyZScript.txt”;

int WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
ShellExecute(NULL, “open”, zbrushExe, scriptPath, zbrushFolder, SW_MINIMIZE );
return 0;
}

However, note that only one zscript/plugin can be active at a time, so if your zscript calls someting like UV Mapper control passes to UV Mapper and no more commands will run in your zscript.

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LoadFileInZBrush
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 0 || args[0] == “?” || args[0] == “help”)
{
Console.Out.WriteLine(“LoadScript Help:”);
Console.Out.WriteLine(“syntax: LoadScript.exe [Full Path to file]”);
Console.Out.WriteLine(“example: LoadScript.exe “C:\users\default\Desktop\myScript.zsc””);
return;
}
FileInfo fileToLoad = new FileInfo(args[0]);
DirectoryInfo zbrushExeDirectory = new DirectoryInfo(FindZBrushWorkingDir());
if (fileToLoad.Extension.ToUpper() == “.ZTL” || fileToLoad.Extension.ToUpper() == “.TXT” || fileToLoad.Extension.ToUpper() == “.ZSC”)
{

            if (fileToLoad.Exists && zbrushExeDirectory.Exists)
            {
                Process p = new Process();
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.FileName = zbrushExeDirectory.FullName + "ZBrush.exe";
                p.StartInfo.Arguments = fileToLoad.FullName;


                p.OutputDataReceived += new DataReceivedEventHandler(
                    (s, e) =&gt;
                    {
                        Console.Out.WriteLine(e.Data);
                    }
                );
                p.ErrorDataReceived += new DataReceivedEventHandler((s, e) =&gt; { Console.Error.WriteLine(e.Data); });
                p.Start();
                p.BeginOutputReadLine();
                p.BeginErrorReadLine();
                p.WaitForExit();
            }
            else
            {
                if (!fileToLoad.Exists)
                {
                    Console.Error.WriteLine(String.Format("{0} not found.", args[0]));
                }
                if (!zbrushExeDirectory.Exists)
                {
                    Console.Error.WriteLine(String.Format("{0} not found.", args[0]));
                }
            }
        }
        else
        {
            Console.Error.WriteLine("File must be a .ZTL, .TXT, or .ZSC");
            return;
        }
    }


    private static string FindZBrushWorkingDir()
    {
        List&lt;string&gt; zbrushAppNames = new List&lt;string&gt;();


        string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
        try
        {
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey))
            {
                foreach (string subkeyName in key.GetSubKeyNames())
                {
                    using (RegistryKey subkey = key.OpenSubKey(subkeyName))
                    {
                        //Console.WriteLine(subkey.GetValue("DisplayName"));
                        object objName = subkey.GetValue("DisplayName");
                        object objVers = subkey.GetValue("DisplayVersion");
                        object objInstallPath = subkey.GetValue("InstallLocation");
                        if (objName != null && objVers != null)
                        {
                            string name = objName.ToString();
                            string version = objVers.ToString();
                            if (name.Contains("ZBrush"))
                            {
                                zbrushAppNames.Add(objInstallPath.ToString());
                            }
                        }
                    }
                }
            }
            //return the latest version
            return (zbrushAppNames.Last&lt;string&gt;() + "\\");
        }
        catch(Exception e)
        {
            Console.Error.WriteLine(e.Message);
            return null;
        }
    }
}

}

Sorry for the missing [tabs].

Probably it’s nothing special but i have found that when my script/macro structure instead of:


[IButton,test,"test script",

some script code

] /*End of ZScript*/

Looks like this:


[If,1,

some script code 

]/*End of ZScript*/
[pd]

Than you can execute such script from command line or just run it like standard windows application.

ps.
*.zsc extension is assigned to open with Zbrush Executable in my system
You have to load *.txt script manualy to zbrush first to create *.zsc file (Zscript -> Load)

Hello ,have you solved the problem?Is it convenient to communicate with each other?