Writing Dll files for Zbrush

Some of you have been talking about dynamic libraries and how to use them to extend zscripting and I was wondering if I could trouble you to share some advices and tips or even introductory little tutorials for those without any knowledge of C programming like myself.
It doesn’t have be done in one day, or week or be exhaustive… Just keep posting stuff you might think is useful and want to share, external links etc… Just enough to keep people going. I am not asking for a full C tutorial…

Hi Dargelos, I will give it a shot.

Before I begin let’s be clear about what we are talking about when you say “extend zscripting”.

What you can do with a dll is use zscript to get the positions of zspheres, store those in a memory block and pass it to the dll. From there you could generate polygons between the zsphere positions, put those in a .obj format and store it in a output memory block. Back in zbrush you would save the memory buffer to a file and import it in the tool palette.

What you cannot do with a dll is change the way zbrush subdivides geometry when you press Tool > Geometry > Divide, for example. You are basically limited to adding functionality to zbrush, not changing the way existing functions operate. Or put another way, you are not changing the ZBrush codebase, just adding to it.

With that out of the way your first step is to learn about the tools available to you. That means the compiler, the programming languages, libraries/frameworks, documentation for the libraries and a bucket or two of Regaine.

The compiler part is easy. On OS X you would use Xcode which is a free download from Apple. On Windows you would use Visual Studio Express 2013 for Windows or Visual Studio Community, not sure which version is the recommended one ( Marcus probably knows ).

On OS X you can intermingle C code with Objective C which is a superset of C. You can also use C++ which would make supporting OS X and Windows easier. On Windows you can use C and C++ without any problems and with a little work you can add C# (if you search this forum there was someone who created a C++ wrapper to call the C# portions).

With libraries you are in luck as the C programming language has been around since World War One. There are too many to mention but a good rule of thumb is to do a search before you start a project, you don’t want to reinvent the wheel. Libpng is a good example. With libpng you can create and manipulate PNG files. You wouldn’t want to write all that yourself.

Documentation is everywhere on the web. The C and C++ Reference, for example, can be found here.

So not any actual C code examples, just general advice. I will post some actual code later. Are you on Mac or PC?

:smiley: I’m on Windows 7 :wink:

Oh thanks TV eyes… That’s the kind of information I was looking for.
So , I can use C++ code as well? cool!
I am also on windows and I already installed visual studio 2013 .
I would like to start with something like a obj reader. as you said, they already invented that wheel so many times but I would be happy to have something that outputs vertex positions, distances or even give me the volume or area of the mesh, things like that related with maths calculations…

I won’t have access to my PC until tomorrow so if you are dying to get started maybe Marcus could post the dll template for Visual Studio? But I can give some more general advice.

Sometimes you hit a dead end and the documentation just isn’t helping. Google is great but StackOverflow is even better. As they mention on the frontpage: “Stack Overflow is a question and answer site for professional and enthusiast programmers”. Enter the Tag section and narrow down your search by selecting C and 3D, for example, and then your search term. I find it best to formulate the search as if I was asking a question. If you still can’t find what your searching for try the search term in Google using the site: prefix, e.g: site:stackoverflow.com volume of 3d object

Using the above search term I found exactly what I was looking for but searching for volume of 3d object directly on stackoverflow was not as succesful.

As I mentioned, a man could drown in the C libraries found on the internet. Here are a couple you might find fit your needs.

Wavefront (obj) Loader
“This is a basic .obj loader written in C. A C++ wrapper is included. It can parse vertices, texture coordinates, normals, 3 or 4 vertex faces, and .mtl files. There is also support for non-standard object types that are relevant to raytracing.”

The GTS Library
“GTS stands for the GNU Triangulated Surface Library. It is an Open Source Free Software Library intended to provide a set of useful functions to deal with 3D surfaces meshed with interconnected triangles. … Metric operations (area, volume, curvature …).”

Wow!!! Thanks again TVeyes! I needed this kind of general orientation , where to find help and what to expect . You helped a lot already

Here’s a template. It’s just the C++ code so it’s not specific to any IDE. The compiled DLL included is 32bit!!!

DLL_Template.zip (5.05 KB)

Oh and I like this C++ resource: http://www.cplusplus.com/

Also, sometimes it is simpler to write your own functions than incorporating a library. Certainly you don’t necessarily want to reinvent the wheel but you don’t want to unnecessarily bloat your code either. For example, OBJ files are pretty simple text files and ZBrush only uses a few of the tags so parsing a ZBrush-exported OBJ for the data you need is not difficult.DLL_Template.zip (5.05 KB)

I am not sure how I forgot to mention this one: Lynda.com have excellent video tutorials on nearly everything. Take the 7 day free trial and watch the videos on C and then continue on to the C++ tutorials. They even have video tutorials about these weird Objective-C and Swift languages that nobody uses ;).

I also posted an insanely long rambling of my development process >here<. I don’t know, might be of some use.

Edit: Dammit, every time I post I forget to commend you on your Ztree plugin. What a fantastic zplugin! :+1:

Thanks a lot Marcus and TV eyes, I am slowly making progress and learning a bit… Thanks a lot.

I gonna post some very specific question that would help probably not only myself about writing dll/lib to complete external treatment out Zbrush.

I’d like to know a bit more about the modern practices to have cross compile functional to build for Win for Mac using Cmake for example or MinGW.

I am new on dynamic library development , but I still got some c++ knowledge from my long experience coding character AI for a Mod project for Hal life 2 :slight_smile:

My starting point is the DLL template which Marcus has posted above.

So i had create a project using visual Studio 2017 community i can compile you code
it works partially in Zbrush, the helloDLL function break at the line after the messageBox().

Also i like to use pure c++ code with standard string style, so remove the C-Style Strings
then convert the two typedefs “float” compatible with unicode strings ( as it seems to be the norm now)
and remove the char* which seem to be a very very very old ways to handle strings :slight_smile:

Another point, what about the Extern “C”, is it important for the dll to have C linkage ?


The Extern C is necessary for any C functions to be visible for ZBrush. If you’re using pure C++ I suppose you don’t need it but sometimes it’s useful to include older code so I include it as a matter of course.

Are you compiling for Unicode? That will be necessary for 4R8.

There’s a new version of the template below. This includes the VS 2015 project files, so should be simple enough to load into VS 2017.

ZBrush_DLL_Template_4R8.zip (9.85 KB)ZBrush_DLL_Template_4R8.zip (9.85 KB)

So good man, thanx you a lot, so i can compare with my setup ad adjust few details on the configurations.

about the except “C” i understand why the linkage must to be done in C style, but can we write functions that use standard strings ?
Dealing with char* is just make the coding the get very hard to handle and read.

yeah this is exactly where we both end up with the dll and this code.
i managed to make it works for unicode with messageboxA , but the I’d like to use the not deprecated strcpy_s instead of not strcpy.
With strcpy, I ought to add a pre processor for the compiler to remove the warning (_CRT_SECURE_NO_WARNINGS)
well it compiles and the helloDLL do not make Zbrush crashes anymore.
That’s a good starting point ^^

With that template you can use any C++ or C code you want. Just leave the function parameters as they are. So, for example, if you want to pass a string back to ZBrush using outputBuffer then that needs to be char*.

You can use strcpy_s but remember that’s MS Windows specific if you are writing code that will be ported to MacOSX. (I know, a better template is needed with XCode version too…)

alright Marcus it’s all noted, will what i could do with that, maybe not right now.

I don’t think a template for Xcode is necessary just help me to compile with Cmake, then i will make the best zscript coding environment ever.
I am slowly developing an extension for visual studio code (http://code.visualstudio.com), which is a lightweighted visual studio IDE and that is cross platform ( mac+win).
We can compile with msbuild, netcore 2.0, cmake, probably with gcc etc…

I plan to make a complete extension to write plugins for zbrush, even dll writing.
I want language support, ( code highlighting…)

Support Versioning software Github/SVN etc… so we could easily backup locally with SVN or online using Github.

Code snippet presets + user snippets ( i already made a bunch of snippet presets )

Code navigation (goto definition, so we could easily retrieve any call of any routineCall for exmaple.
Intellisense, if i can use the java doctype style to store information of any routineDef we could just read what the routine does without to jump to the code declaration.

if i can also set up everything use a dll template with win and mac support, that would be great :slight_smile:
Code auto complete support.
Routine Task to copy the Txt file to Zplug64 directory in Zbrush installation. ( i did that for myself)

Debug support :
I know its possible to check in the code about the syntax and spot any typo or mistake, which sometime is very hard to debug in zbrush when the code is too long so that would complete the feedback for the user.

I have other important priority , but i planned to do that, i already have the base for working on this.

I will post the extension on github, so you could help me on that, if you wish Marcus :slight_smile:
and why not a repo with user scripts library, we can tag, and from zbrush we can directly download any scripts to use it instantly ?

hey, sorry guys for resurrecting that old thread, but i got some really stepped progress on this project and no time for that.
I want that project to be open source and free for anyone to contribute and reinforcing accessibility for plugins developpers, to learn from that github project

to have an easy way to learn how to build your own Dll for your Zbrush plugins.

Marcus, no need to reinvent the wheel, so i reused without to ask your permission ( i do now ;] ), for the hellodLL and Version function code + the plugin template to test out the communication between zbrush and the dll.

Next Stage is to have a code which can compiling with Xcode and not only Visual studio.
But this is something i cannot do myself, so i like to make this project supported by the community of plugins developers.
Ideally it would be even nicer, if it was possible to compile the project for OSX and windows, using simple IDE, like Visual Code, or Atom.

I have not much time to make this project to progress quickly, so fork the repo and submit pull request to help me in that task.

Marcus that could free you some time as you still haven’t get the time to release the template for Xcode, could be easier now with the github repo, don’t you think ?


Oh i a almost forget to mention and its important to mention that for now, the currentdll seems to not behave as it should on Windows.
how i proceed :
I compile the dll and test it with Version button in plugin template plugin interface in zbrush, then value returned is 1 which is correct.

if i change the value to anything else, then rebuild, and update by overwriting the old dll windows refuse to overwrite the file. I have to close Zbrush, before i can overwrite it again.
So probably something is wrong with the DLL_PROCESS_DETACH, but i don’t see what and i need some help :wink:

I am terrified by what i just had discovered now about Zbrush.
I develop a plugin and for that i am building a dll on my own, and I must to say it was a very confusing experience.
I always thought it was because of my dll, andits code if, zbrush prevent me from deletting my dll after i call a function from a zbrush plugin
it just impossible to build my dll, copy it in my plugin directory structure and then just try it
because zbrush never let the dll to properlly detach the process.
so its just impossible to overwrite the existing file I ought to reboot zbrush for that
i thought it was my code, but that wrong, it Zbrush’s Code or i have a big issue with my zbrush installation.
I tested that out with decimation master, it just impossible to delete DecimationMasterDLL.dll from decimation plugin fodler,
if you invoke the dll from decimation right from zbrush decimation plugin with process current and decimate current.

Marcus please helpme it seems to be a big huge issue with zbrush.

it finally just myself who is bugging, i just prepared my project to run zbrush with the debugger, then i open and close when i stop the debug mode :wink:

Yes, as far as I know this has always been the way it works with ZBrush - the dll is not unloaded until ZBrush closes. I don’t know why it works that way but presumably something to do with plugins and memory allocation.

thank you Marcus for the confirmation, i noticed that when i see the log in my debugger.
also i can’t say how many time the ZFileutils64.dll is loaded at startup, it depend how many plugin use it. don’t you think something could be done to improve the loaded resources.

Also my code and project can compile with CMake now, i am just not sure if it would compile the the current settings of the project for MAC OS.
Have you take a look at the github repo ? what you think if we push so stuff there so it can benefit to everyone who want to start a plugin project for zbrush ?