1. #1
    Senior Member Follow User Gallery
    Join Date
    May 2007
    Location
    Bellevue,WA
    Posts
    483

    Default 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...
    __________________________________________________

    My GumRoad

    ZTree
    Terrain Tools (OS X)
    Terrain Tools (Win x64)

  2. #2
    Senior Member Follow User Gallery
    Join Date
    Jul 2002
    Location
    Copenhagen
    Age
    43
    Posts
    2,561

    Default

    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?
    Mark

  3. #3

  4. #4
    Senior Member Follow User Gallery
    Join Date
    May 2007
    Location
    Bellevue,WA
    Posts
    483

    Default

    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...
    __________________________________________________

    My GumRoad

    ZTree
    Terrain Tools (OS X)
    Terrain Tools (Win x64)

  5. #5
    Senior Member Follow User Gallery
    Join Date
    Jul 2002
    Location
    Copenhagen
    Age
    43
    Posts
    2,561

    Default

    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 ...)."
    Mark

  6. #6
    Senior Member Follow User Gallery
    Join Date
    May 2007
    Location
    Bellevue,WA
    Posts
    483

    Default

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

    My GumRoad

    ZTree
    Terrain Tools (OS X)
    Terrain Tools (Win x64)

  7. #7
    Moderator Follow User Gallery
    Join Date
    Jun 2004
    Location
    UK
    Posts
    10,576

    Default

    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


    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.

  8. #8
    Senior Member Follow User Gallery
    Join Date
    Jul 2002
    Location
    Copenhagen
    Age
    43
    Posts
    2,561

    Default

    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!
    Mark

  9. #9
    Senior Member Follow User Gallery
    Join Date
    May 2007
    Location
    Bellevue,WA
    Posts
    483

    Default

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

    My GumRoad

    ZTree
    Terrain Tools (OS X)
    Terrain Tools (Win x64)

  10. #10
    Senior Member Follow User Gallery
    Join Date
    Feb 2015
    Location
    Paris
    Posts
    209

    Default

    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

    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

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

    Nicolas
    Plugins & Toolsets for Zbrush 4R8 : zCycler | zTexturer | Twitter | @Facelessmindz

  11. #11
    Moderator Follow User Gallery
    Join Date
    Jun 2004
    Location
    UK
    Posts
    10,576

    Default

    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

  12. #12
    Senior Member Follow User Gallery
    Join Date
    Feb 2015
    Location
    Paris
    Posts
    209

    Default

    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 ^^
    Plugins & Toolsets for Zbrush 4R8 : zCycler | zTexturer | Twitter | @Facelessmindz

  13. #13
    Moderator Follow User Gallery
    Join Date
    Jun 2004
    Location
    UK
    Posts
    10,576

    Default

    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...)

  14. #14
    Senior Member Follow User Gallery
    Join Date
    Feb 2015
    Location
    Paris
    Posts
    209

    Default

    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
    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
    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 ?
    Plugins & Toolsets for Zbrush 4R8 : zCycler | zTexturer | Twitter | @Facelessmindz

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •