Load ZBrush Polygroups as Material Inputs

Forums: Load ZBrush Polygroups as Material Inputs
Forum for OctaneRender Lua scripting examples, discussion and support.

Load ZBrush Polygroups as Material Inputs

Postby tonysculptor » Sat Apr 12, 2014 6:34 am

tonysculptor Sat Apr 12, 2014 6:34 am
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)
Attachments
LoadPolyGroupAsMaterial.lua
(743 Bytes) Downloaded 471 times
Tony Reynolds

i7 6 Core Processor | 680 GTX x 3, 4 GB VRAM | 128 GB Ram
tonysculptor
Licensed Customer
Licensed Customer
 
Posts: 62
Joined: Fri Aug 16, 2013 12:27 am
Location: Austin, TX

Re: Load ZBrush Polygroups as Material Inputs

Postby stratified » Sat Apr 12, 2014 8:11 am

stratified Sat Apr 12, 2014 8:11 am
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
User avatar
stratified
OctaneRender Team
OctaneRender Team
 
Posts: 945
Joined: Wed Aug 15, 2012 6:32 am
Location: Auckland, New Zealand

Re: Load ZBrush Polygroups as Material Inputs

Postby tonysculptor » Sat Apr 12, 2014 12:53 pm

tonysculptor Sat Apr 12, 2014 12:53 pm
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.
Tony Reynolds

i7 6 Core Processor | 680 GTX x 3, 4 GB VRAM | 128 GB Ram
tonysculptor
Licensed Customer
Licensed Customer
 
Posts: 62
Joined: Fri Aug 16, 2013 12:27 am
Location: Austin, TX

Re: Load ZBrush Polygroups as Material Inputs

Postby Tugpsx » Sat Apr 12, 2014 3:06 pm

Tugpsx Sat Apr 12, 2014 3:06 pm
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)
Dell Win Vista 32 | NVIDIA Quadro NVS135M | Core2 Duo T7500 2.20GHz | 4GB
CyberPowerPC Win 7 64| NVIDIA GTX660Ti (3G) GTX480(1.5G) | 16GB
Tugpsx
Licensed Customer
Licensed Customer
 
Posts: 1145
Joined: Thu Feb 04, 2010 8:04 pm
Location: Chicago, IL

Re: Load ZBrush Polygroups as Material Inputs

Postby tonysculptor » Sun Apr 13, 2014 1:43 am

tonysculptor Sun Apr 13, 2014 1:43 am
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()
Attachments
LoadPolyGroupAsMaterial.lua
(1.04 KiB) Downloaded 455 times
Tony Reynolds

i7 6 Core Processor | 680 GTX x 3, 4 GB VRAM | 128 GB Ram
tonysculptor
Licensed Customer
Licensed Customer
 
Posts: 62
Joined: Fri Aug 16, 2013 12:27 am
Location: Austin, TX

Re: Load ZBrush Polygroups as Material Inputs

Postby Metalkid1974 » Sun Aug 03, 2014 3:10 pm

Metalkid1974 Sun Aug 03, 2014 3:10 pm
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 :)
Metalkid1974
Licensed Customer
Licensed Customer
 
Posts: 9
Joined: Sun Jul 28, 2013 7:13 pm

Re: Load ZBrush Polygroups as Material Inputs

Postby bepeg4d » Fri Sep 29, 2017 8:28 am

bepeg4d Fri Sep 29, 2017 8:28 am
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 565 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
User avatar
bepeg4d
Octane Guru
Octane Guru
 
Posts: 9940
Joined: Wed Jun 02, 2010 6:02 am
Location: Italy

Return to Lua Scripting


Who is online

Users browsing this forum: No registered users and 11 guests

Thu Mar 28, 2024 10:17 am [ UTC ]