Page 1 of 1

Load ZBrush Polygroups as Material Inputs

PostPosted: Sat Apr 12, 2014 6:34 am
by tonysculptor
Hey all. I finally made the lua script to load polygroups from ZBrush as material inputs. Mike Pavlovich was asking for this, so I made it for him. Now its for everyone.
This is my first Lua script -- the tough part was figuring out how to get the attribute values of the Octane nodes. When you pass a string value as the name of an attribute it errors out. So I had to type in integers, one at a time, until I found the ones that were associated with "A_FILENAME" and "A_RELOAD". Crappy process, but hopefully the documentation on the API will improve.
Anyhow, it works without updating the mtl file. So have fun ZBrushers!

Here's the script :

Code: Select all
function updateFileData(objFile)
    local f = io.open(objFile, "r")
    local fileData = f:read("*all")
    f:close()
    fileData = fileData:gsub("g (%w+)", "usemtl %1")
   
   f = io.open(objFile, "w")
   f:write(fileData)
   f:close()
   --print (fileData)
   
end

-- main function body

-- get the selected mesh
local selectedRt = octane.project.getSelection()[1]
if not selectedRt or selectedRt.type ~= octane.NT_GEO_MESH then
   showError("no mesh selected")
   return nil
end
--print (selectedRt)

-- get the file path for the mesh
local objFileName = octane.node.getAttribute( selectedRt , 29 )

--print (objFileName )
updateFileData(objFileName)

--reload mesh
octane.node.setAttribute(selectedRt, 94, true, true)

Re: Load ZBrush Polygroups as Material Inputs

PostPosted: Sat Apr 12, 2014 8:11 am
by stratified
Hi Tony,

Thanks for the script.

Indeed the documentation still lacks. But the API browser is just meant as a reference. The best information is found here on the forum in the tutorials and examples. For example have a look at this one http://render.otoy.com/forum/viewtopic.php?f=73&t=37333.

getAttribute and setAttribute take the attribute id. You could rewrite your script like this:

Code: Select all
-- get the file path for the mesh
local objFileName = selectedRt:getAttribute(octane.A_FILENAME)

--print (objFileName )
updateFileData(objFileName)

--reload mesh
selectedRt:setAttribute(octane.A_FILENAME, true, true)


cheers,
Thomas

Re: Load ZBrush Polygroups as Material Inputs

PostPosted: Sat Apr 12, 2014 12:53 pm
by tonysculptor
Thanks. I did a bit of searching on the forums, but missed that notation. Thanks.
As for using the script, import thr geometry into octane, then select the mesh node and run the script. I will try to see if I can highjack the initial mesh import process later, so it can be one step.

Re: Load ZBrush Polygroups as Material Inputs

PostPosted: Sat Apr 12, 2014 3:06 pm
by Tugpsx
Getting the dreaded nil value error on run. If I recall this happens when the variable is undefined.

Code: Select all
[string "function updateFileData(objFile)..."]:3: attempt to index local 'f' (a nil value)

Re: Load ZBrush Polygroups as Material Inputs

PostPosted: Sun Apr 13, 2014 1:43 am
by tonysculptor
Fixed the error.

Code: Select all
-- helper to pop-up an error dialog
local function showError(text)
    octane.gui.showDialog
    {
        type  = octane.gui.dialogType.BUTTON_DIALOG,
        icon  = octane.gui.dialogIcon.WARNING,
        title = "Daylight Animation Error",
        text  = text,
    }
end



function updateFileData(objFile)
    local f = io.open(objFile, "r")
    local fileData = f:read("*all")
    f:close()
    fileData = fileData:gsub("g (%w+)", "usemtl %1")
   
   f = io.open(objFile, "w")
   f:write(fileData)
   f:close()
   --print (fileData)
   
end

local function reloadMeshGroupsAsMaterials()
   -- get the selected mesh
   local selected = octane.project.getSelection()[1]
   if not selected or selected.type ~= octane.NT_GEO_MESH then
      showError("no mesh selected")
      return nil
   end

   -- get the file path for the mesh
   local objFileName = octane.node.getAttribute( selected , 29 )

   --print (objFileName )
   updateFileData(objFileName)

   --reload mesh
   octane.node.setAttribute(selected, 94, true, true)
end

reloadMeshGroupsAsMaterials()

Re: Load ZBrush Polygroups as Material Inputs

PostPosted: Sun Aug 03, 2014 3:10 pm
by Metalkid1974
What a nice script - That will make my life easier from this point on ;)

It works in standalone 1.55, but i 2.05 i get: "[string "-- helper to pop-up an error dialog..."]:15: bad argument #1 to 'open' (string expected, got boolean"

It's not a huge problem, since running the script on a mesh in 1.55 means it can then be loaded into 2.05 with its new material inputs, but just thought I'd mention it in case someone can make it work directly in 2.xx :)

Re: Load ZBrush Polygroups as Material Inputs

PostPosted: Fri Sep 29, 2017 8:28 am
by bepeg4d
Hi,
here is a revisited version of the script to make it works also with recent versions of OctaneRender.
You need to select the imported obj node and run the attached script, to have all the polygroups listed as material pins:
Screen Shot 2017-09-26 at 11.00.58.jpg

LoadPolyGroupAsMaterial_b.lua
(1.08 KiB) Downloaded 224 times

Basically, the script was not working with v2 or v3 because the octane attribute were called via ID number instead of name, and the id numbers have changed during versions.
ciao beppe