Isotropix Forums

Script for converting Legacy materials to Physical?

Clarisse Scripting related topics

Script for converting Legacy materials to Physical?

Unread postby rmfx » Thu Jul 14, 2016 10:42 pm

Hi, I'm currently working in Clarisse 3.0 RC3 at my studio, and all the new PBR stuff is mostly working well. However, the subject of converting an old legacy-based project to work with PBR has come up, and It's just too much work to be done manually. I've had some limited success doing a simple find and replace in a text editor, replacing any instance of 'Material Standard' with 'MaterialPhysicalMultiblend', with a few standard layers, and replacing 'diffuse {' with 'MaterialPhysicalDiffuse {....front_color {' and so forth so that the old rgb values and maps are kept. It's a little bit of a manual process running several search/replaces but it seems to do the job on a simple test file, and with a little care it can be done in a way that everything retains it connections.

However, once the materials get more complicated, things start to get messy and can easily break when there are missing brackets etc. It seems like a more complicated script is required that pulls variables for values and maps from the old unified Legacy shaders, creates and populates Physical sub-shaders with the appropriate diffuse and spec values, inverts glossiness values to roughness, and plugs all these sub-shaders into a MultiBlend shader, then links that to the appropriate geometry. I'm also not clear if there is an equivalent for Backlight other than SSS...

I'm hoping that such a script exists out there somewhere, otherwise we now have all these huge Clarisse 2.0 environments and assets that will have to be scrapped going forward, if we are to take advantage of the new PBR engine.

Any help would be appreciated!
Thanks :)
rmfx
 
Posts: 15
Joined: Tue Jun 09, 2015 8:28 pm

Re: Script for converting Legacy materials to Physical?

Unread postby support_team » Mon Jul 18, 2016 10:03 am

Hi,

we have started thinking about such a script (#5323) but it's not done yet.

rmfx wrote:I'm also not clear if there is an equivalent for Backlight other than SSS...

This corresponds to the back color of the physical diffuse material.

Cheers
Isotropix
Support Team
User avatar
support_team
 
Posts: 1020
Joined: Thu Jan 31, 2013 11:10 am

Re: Script for converting Legacy materials to Physical?

Unread postby rmfx » Wed Jul 20, 2016 7:47 pm

Thank you
rmfx
 
Posts: 15
Joined: Tue Jun 09, 2015 8:28 pm

Re: Script for converting Legacy materials to Physical?

Unread postby devinj898 » Thu Dec 14, 2017 8:08 pm

Is there any update on this request? I have quite a few scenes using the legacy materials, and it would really save a lot of time if this could be implemented.
devinj898
 
Posts: 21
Joined: Mon May 05, 2014 7:36 am

Re: Script for converting Legacy materials to Physical?

Unread postby arnaud » Fri Dec 22, 2017 9:56 am

Hello,

This hasn't been done yet.
Sorry about that.

Cheers,
Arnaud
Arnaud

Isotropix
Chief Process & Innovation Officer
User avatar
arnaud
 
Posts: 190
Joined: Mon Dec 14, 2015 2:48 pm

Re: Script for converting Legacy materials to Physical?

Unread postby dboude » Fri Dec 22, 2017 4:46 pm

Hi,

Here is a script I use personally. It converts standard legacy material to PBR standard material. It is not perfect and could be optimised in the code but it saves a lot of time. Subtract nodes are added for converting glossiness to roughness.

:!: It doesn't take SSS into account because there is too much difference between legacy and PBR for this channel
:!: Doesn't work in references

Code: Select all
materials = ix.api.OfObjectVector()
filter = "*"
type = ["MaterialStandard"]
for i in type:
    ix.application.get_matching_objects(materials, filter, i)

def delockAttrs(item):
    count = item.get_attribute_count()
    for i in range(count):
        item.get_attribute(i).set_read_only(False)

for mat in materials:

    diff = mat.attrs.diffuse
    back = mat.attrs.backlighting
    lumi = mat.attrs.luminosity
    spec = mat.attrs.specularity
    glos = mat.attrs.glossiness
    refl = mat.attrs.reflection
    rglos = mat.attrs.reflection_glossiness
    bump = mat.attrs.bump
    transp = mat.attrs.transparency
    transColor = mat.attrs.transmittance_color
    transDensity = mat.attrs.transmittance_density
    ior = mat.attrs.index_of_refraction
    aniso = mat.attrs.anisotropy
    anisoRot = mat.attrs.anisotropy_rotation

    ix.selection.select(ix.get_item(str(mat)))
    ix.application.select_next_outputs()
    sel = ix.application.get_selection()

    connected = sel.get_item(0).is_kindof("Geometry")


####################

    context = mat.get_context()
    pbr = ix.cmds.CreateObject(str(mat).split("/")[-1] + "_pbr", "MaterialPhysicalStandard", "Global", str(context))
    delockAttrs(pbr)

    if diff.attr.is_textured() :
        map = diff.attr.get_texture()
        pbr.attrs.diffuse_front_color.attr.set_texture(map)
    else :
        pbr.attrs.diffuse_front_color[0] = diff[0]
        pbr.attrs.diffuse_front_color[1] = diff[1]
        pbr.attrs.diffuse_front_color[2] = diff[2]

    if back.attr.is_textured() :
        map = back.attr.get_texture()
        pbr.attrs.diffuse_back_strength = 1
        pbr.attrs.diffuse_back_color.attr.set_texture(map)
    else :
        pbr.attrs.diffuse_back_strength = 1
        pbr.attrs.diffuse_back_color[0] = back[0]
        pbr.attrs.diffuse_back_color[1] = back[1]
        pbr.attrs.diffuse_back_color[2] = back[2]

    if lumi.attr.is_textured() :
        map = lumi.attr.get_texture()
        pbr.attrs.emission_strength = 1
        pbr.attrs.emission_color.attr.set_texture(map)

    else :
        pbr.attrs.emission_strength = 1
        pbr.attrs.emission_color[0] = lumi[0]
        pbr.attrs.emission_color[1] = lumi[1]
        pbr.attrs.emission_color[2] = lumi[2]

################

    if spec.attr.is_textured() :
        map = spec.attr.get_texture()
        pbr.attrs.specular_1_color.attr.set_texture(map)
        pbr.attrs.specular_1_strength = 1
        pbr.attrs.specular_1_index_of_refraction = ior[0]
    else :
        pbr.attrs.specular_1_color[0] = spec[0]
        pbr.attrs.specular_1_color[1] = spec[1]
        pbr.attrs.specular_1_color[2] = spec[2]
        if (spec[0] + spec[1] + spec[2]) > 0 :
            pbr.attrs.specular_1_strength = 1
            pbr.attrs.specular_1_index_of_refraction = ior[0]

    if glos.attr.is_textured() :
        map = glos.attr.get_texture()
        sub = ix.cmds.CreateObject("subtract", "TextureSubtract")
        ix.cmds.SetValues([str(sub) + ".input1"], ["1", "1", "1"])
        ix.cmds.SetTexture([str(sub) + ".input2"], map)
        pbr.attrs.specular_1_roughness.attr.set_texture(sub)
    else :
        pbr.attrs.specular_1_roughness[0] = 1 - glos[0]
        pbr.attrs.specular_1_roughness[1] = 1 - glos[1]
        pbr.attrs.specular_1_roughness[2] = 1 - glos[2]

    if aniso.attr.is_textured() :
        map = aniso.attr.get_texture()
        pbr.attrs.specular_1_anisotropy.attr.set_texture(map)
    else :
        pbr.get_attribute("specular_1_anisotropy").set_read_only(False)
        pbr.attrs.specular_1_anisotropy = (0.5 + (aniso[0]/2))

    if anisoRot.attr.is_textured() :
        map = anisoRot.attr.get_texture()
        pbr.attrs.specular_1_anisotropy_rotation.attr.set_texture(map)
    else :
        pbr.attrs.specular_1_anisotropy_rotation = anisoRot[0]

################

    if refl.attr.is_textured() :
        map = refl.attr.get_texture()
        pbr.attrs.specular_2_color.attr.set_texture(map)
        pbr.attrs.specular_2_strength = 1
        pbr.attrs.specular_2_index_of_refraction = ior[0]
    else :
        pbr.attrs.specular_2_strength = 1
        pbr.attrs.specular_2_index_of_refraction = ior[0]
        pbr.attrs.specular_2_color[0] = refl[0]
        pbr.attrs.specular_2_color[1] = refl[1]
        pbr.attrs.specular_2_color[2] = refl[2]

    if rglos.attr.is_textured() :
        map = rglos.attr.get_texture()
        sub = ix.cmds.CreateObject("subtract", "TextureSubtract")
        ix.cmds.SetValues([str(sub) + ".input1"], ["1", "1", "1"])
        ix.cmds.SetTexture([str(sub) + ".input2"], map)
        pbr.attrs.specular_2_roughness.attr.set_texture(sub)
    else :
        pbr.attrs.specular_2_roughness[0] = 1 - rglos[0]
        pbr.attrs.specular_2_roughness[1] = 1 - rglos[1]
        pbr.attrs.specular_2_roughness[2] = 1 - rglos[2]

#################

    if transp.attr.is_textured() :
        pbr.attrs.transmission_strength = 1
        map = transp.attr.get_texture()
        pbr.attrs.transmission_color.attr.set_texture(map)
    else :
        pbr.attrs.transmission_strength = 1
        pbr.attrs.transmission_color[0] = transp[0]
        pbr.attrs.transmission_color[1] = transp[1]
        pbr.attrs.transmission_color[2] = transp[2]

    if transColor.attr.is_textured() :
        map = transColor.attr.get_texture()
        pbr.attrs.transmittance_color.attr.set_texture(map)
    else :
        pbr.attrs.transmittance_color[0] = transColor[0]
        pbr.attrs.transmittance_color[1] = transColor[1]
        pbr.attrs.transmittance_color[2] = transColor[2]

    if transDensity.attr.is_textured() :
        map = transDensity.attr.get_texture()
        pbr.attrs.transmittance_density.attr.set_texture(map)
    else :
        pbr.attrs.transmittance_density[0] = transDensity[0]
        pbr.attrs.transmittance_density[1] = transDensity[1]
        pbr.attrs.transmittance_density[2] = transDensity[2]

    if ior.attr.is_textured() :
        map = ior.attr.get_texture()
        pbr.attrs.specular_1_index_of_refraction.attr.set_texture(map)
    else :
        pbr.get_attribute("specular_1_index_of_refraction").set_read_only(False)
        pbr.attrs.specular_1_index_of_refraction = ior[0]

    if bump.attr.is_textured() :
        map = bump.attr.get_texture()
        bumpnode = ix.cmds.CreateObject("bump_map", "TextureBumpMap", "Global", str(context))
        bumpnode.attrs.input = bump[0]
        ix.cmds.SetTexture([str(bumpnode) + ".input"], str(map))
        pbr.attrs.normal_input.attr.set_texture(bumpnode)


    if connected:
        ix.selection.select(ix.get_item(str(mat)))
        ix.application.select_next_outputs()
        sel = ix.selection
        for i in range(sel.get_count()):
            shadingG = sel[i].get_module().get_geometry().get_shading_group_names()
            count = shadingG.get_count()
            for j in range(count):
                shaders = sel[i].attrs.materials[j]
                if shaders == mat:
                    ix.cmds.SetValues([sel[i].get_full_name() + ".materials" + str([j])], [str(pbr)])

### Delete the old legacy material
    ix.cmds.DeleteItems([str(mat)])
### Set the exact same name as the old legacy material (for shading layers rules)
    ix.cmds.RenameItem(pbr, str(pbr).split("/")[-1][:-4])


Happy Christmas !!!

Cheers ;)
Démian
Isotropix
Technical Artist - Clarisse Specialist
User avatar
dboude
 
Posts: 813
Joined: Mon Jul 03, 2017 10:51 am

Re: Script for converting Legacy materials to Physical?

Unread postby igbw712 » Thu Jan 31, 2019 8:11 pm

Hi,
I get the following error when I run the script any ideas?
"File "<string>", line 1
00:40:30 materials = ix.api.OfObjectVector()
00:40:30 ^
00:40:30 IndentationError: unexpected indent
00:40:30 File "<string>", line 1
00:42:20 materials = ix.api.OfObjectVector()
00:42:20 ^
00:42:20 IndentationError: unexpected indent
00:42:20 File "<string>", line 1
00:50:46 materials = ix.api.OfObjectVector()
00:50:46 ^
00:50:46 IndentationError: unexpected indent
00:50:46 IndexError: attribute index out of range
01:07:50
OfContext.create_object: In context 'project://Compositing', failed to create object 'z_depth': class 'MaterialZDepth' not found.
igbw712
 
Posts: 50
Joined: Tue Nov 29, 2016 4:11 pm

Re: Script for converting Legacy materials to Physical?

Unread postby bvaldes » Fri Feb 01, 2019 11:14 am

Hi,

The error is about the indentation. I copied the script and pasted it into Clarisse and I had to select all and press shift+tab to fix the indent.

Regards
Benoit VALDES
Isotropix
Clarisse QA
User avatar
bvaldes
 
Posts: 361
Joined: Mon Sep 26, 2016 10:44 am

Re: Script for converting Legacy materials to Physical?

Unread postby igbw712 » Fri Feb 01, 2019 4:57 pm

Weird I removed the indent in notepad++ before I got the error but I just did what you said in the Clarisse editor and it worked fine. Thanks!
igbw712
 
Posts: 50
Joined: Tue Nov 29, 2016 4:11 pm


Return to Scripting