Jump to content

MTL To Redshift Material


Atom

Recommended Posts

I added a new feature where a rsColorCorrector is placed after the rsTexture node. I reduce the saturation to zero and connect that to a rsBumpMap. The bump map is routed to the final output. The end result is that each material gains a slight bit a relief in the final render, based upon the greyscale value of the diffuse map.

Here is the new Redshift network that is generated.

untitled-1.jpg.b9dadf5edf323c54237d34740b452fff.jpg

 

Here is the same Colosseum, pictured above, with the new bump mapping applied.

untitled1.thumb.jpg.87623b9d0b766b7681094b0f7efd3bb5.jpg

 

untitled1.jpg

Edited by Atom
  • Thanks 1
Link to comment
Share on other sites

I made a change to the script when I imported a new model. This model was making use of cards with alpha channels which were not rendering correctly. It also had some materials without image maps at all, just colors.

Here is the new Redshift network to support textures that have alphas (detected via filename extension)

untitled-1.jpg.280c55b1e638e03c2ce111be79922f45.jpg

 

Here are the before and after images for alpha support.

Before, card backgrounds are not "knocked out".no_alpha_support.thumb.jpg.9e8b926e6b747f4009b29c6165ca499e.jpg

 

rsSprite node removes or "knocks out" card background.sprite_alpha_support.thumb.jpg.8d892557e9823acd33d969ba600bb6be.jpg

 

 

untitled1.jpg

Edited by Atom
  • Thanks 1
Link to comment
Share on other sites

If you find that the script crashes on certain .MTL files, it may be that the names used for materials violate the Houdini naming convention. Most notably, any material that starts with a number. i.e. 03, for example. These files may have been exported from 3DSMax.

An example of .OBJ files with number based materials can be found at this link.

Untitled-1.thumb.jpg.060a43134c055414d7dfed5e4cbacdeb.jpg

I have written a short fixup script that will scan a folder of .OBJ files and examine the .OBJ and .MTL for Houdini naming violations. If it finds them, it will re-write the .OBJ and .MTL file with a revised name that meets the Houdini naming convention.

# Rewrite the .OBJ and .MTL files to make material names Houdini compliant.
# Most notably, 3DSMax exported .OBJ files with a material that starts with a number i.e. 03, for example.
# Like the OBJ files found at this link. https://www.turbosquid.com/3d-models/free-3ds-mode-building-ready-city/690329

import os
    
def returnFilesLike(passedFolderName, passedFileExtension = ".obj"):
    result = []
    for file in os.listdir(passedFolderName):
        if file.endswith(passedFileExtension):
            result.append(os.path.join(passedFolderName,file))
    return result
    
def rewriteOBJFiles(passedPath):
    lst_files = returnFilesLike(passedPath, ".obj")
    if len(lst_files):
        for i,file in enumerate(lst_files):
            # Scan each .OBJ looking for illegal material names.
            the_path = os.path.dirname(file)
            obj_name = os.path.basename(file)
            just_name = os.path.splitext(obj_name)[0]
            mtl_name = "%s.mtl" % just_name
            mtl_filename = os.path.join(the_path,mtl_name)
            
            if os.path.isfile(mtl_filename):
                # A companion .MTL file exists, let's read the .OBJ file.
                with open(file, 'r') as f:
                    lines = f.read().splitlines()   # Remove slash n character at the end of each line.
                f.close()

                # Let's scan the .OBJ for illegal material names.
                needs_repair = False
                new_prefix = "%s_" % just_name
                new_lines = []
                for line in lines:
                    ary = line.split(' ')
                    if ary[0] == 'usemtl':
                        if ary[1][0].isdigit():
                            # This material name violates Houdini naming condition. First letter can not be a number.
                            # Add a prefix now.
                            ary[1] = "%s%s" % (new_prefix, ary[1])
                            new_lines.append("%s %s" % (ary[0],ary[1]))
                            needs_repair = True
                    else:
                        new_lines.append(line)
                        
                if needs_repair:
                    # This .OBJ needs re-written to correct bad material names.
                    f = open(file, 'w')
                    for line in new_lines:
                      f.write("%s\n" % line)
                    f.close()
                    
                    # Time to read .MTL file.
                    with open(mtl_filename, 'r') as f:
                        lines = f.read().splitlines()   # Remove slash n character at the end of each line.
                    f.close()
                    
                    # Let's scan the .MTL for illegal material names.
                    new_lines = []
                    for line in lines:
                        ary = line.split(' ')
                        if ary[0] == 'newmtl':
                            if ary[1][0].isdigit():
                                # This material name violates Houdini naming condition. First letter can not be a number.
                                # Add a prefix now.
                                ary[1] = "%s%s" % (new_prefix, ary[1])
                                new_lines.append("%s %s" % (ary[0],ary[1]))
                        else:
                            new_lines.append(line)
                            
                    # This .MTL needs re-written to correct bad material names.
                    f = open(mtl_filename, 'w')
                    for line in new_lines:
                      f.write("%s\n" % line)
                    f.close()

    else:
        print "No [.OBJ] files found at folder [%s]." % (passedFolderName)

# NOTE: Remember to make a backup of your original .OBJ/.MTL files before you run this script.
rewriteOBJFiles ("C:\Users\Admin\Documents\Models\Blendswap\cc0_14_building_set\obj")

 

Edited by Atom
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

  • 2 months later...
  • 1 month later...

I have added support for an additional .MTL token, the map_Kn token. I came across a game based .OBJ/.MTL file that referenced companion normal maps to the diffuse maps. In this particular mesh, the normal map definition always followed the diffuse map definition. The extension to the material takes the form of a new rsTexture node being plugged into the rsBumpMap node.

untitled-2.jpg.c154e13ccc0479dded9bce84c93803fd.jpg

This disconnects the previous attempt to "auto-bump" a map by using a color correct node to create a greyscale image from the diffuse. The color correct node is still left in place, but the new rsTexture simply takes it's place for supplying the input of the rsBump map node. If no map_Kn token is found, the rsColorCorrect will remain in place.

NOTE: For this to work you must have ObjParms applied to the object using this material and the checkbox for Compute Vertex Tangents From the object UV Map must be checked. The script does create the materials, but it does not auto-check the box on your object.

 

The bump results can be a bit nicer using a normal map instead of a faked out greyscale of a diffuse.

untitled-1.thumb.jpg.6f9cba83b5d1e1f24adbea6c176d0b0c.jpg

Edited by Atom
Link to comment
Share on other sites

  • 1 month later...

Briliant!  I'd  love to see a 3ds max script that exports redshift materials as a bunch of parameters to an xml file etc, and then suffixes each object name with a material identifier.  Then this script could import the obj file and create like for like materials and assign them to the imported geometry based on the suffix on the objects name.

Edited by 3di
Link to comment
Share on other sites

  • 2 months later...

this is brilliant and works great, thanks for making this

I was using this to bring in some archviz models that had like 30-40 materials each so I modified it slightly to automatically promote all the RS_material parameters so you can mass change them all at once without having to dive in and out of materials hundreds of times.  Also a lot of the material settings in other DCC apps do not translate at all to redshift settings, so this helps solve that as well

pretty much just 1 line of code per vop-   rs_mat.insertParmGeneratorsForAllInputs(hou.vopParmGenType.Parameter,True)

Link to comment
Share on other sites

  • 1 month later...
On 2018/2/24 at 10:53 PM, Atom said:

I put together a simple script to read the .mtl file, typically associated with a .obj, and create a Redshift material for each entry it finds in the .mtl with a map_Kd token. A Redshift material is created and a texture map is linked to the diffuse color with the filename for the map populated with what is found in the .mtl entry.

This is useful when importing architectural .obj files that typically have a large number of materials associated with them.

Expected .mtl format of supported tokens.


#newmtl _4_2_                       <- Material name.
#Ns 96.078431                       <- Specular intensity
#Kd 0.640000 0.640000 0.640000      <- Diffuse color.
#Ks 0.500000 0.500000 0.500000      <- Specular color.
#Ni 1.000000                        <- Index of refraction.
#d 1.000000                         <- Opacity.
#map_Kd 21_budova/_4_2_.jpg         <- Map name.
#map_Kn 21_budova/_4_2_n.jpg        <- Map name.

 

The result of one-click texture mapping.

untitled-1.jpg

 

Here is the Colosseum auto texture mapped.

untitled1.jpg

 

The path to the .mtl file and texture path is hard coded.

Place the code in a shelf tool button and adjust the path to point to your .mtl file.

 

mtl_to_redshift_061618.zip

you are  great !  any possible mtl to  mantra  tex map auto link ?

Link to comment
Share on other sites

  • 3 months later...

Thanks for making this. When I run the script, it brings in all the materials into shop but the obj links to the materials in mat. I just updated the three instances of "shop" in the code and changed to "mat" and it worked.

Link to comment
Share on other sites

  • 2 weeks later...

Place the code in a shelf tool button or you can just open the python source editor (Windows menu) and paste the script into.

Find the line #select MTL file to process

Adjust one of the path to point to your .mtl file.

Click apply (or click your new shelf button).

It's done

You find the materials in shop.

Super Cool !

However, it seems that it is better to leave the textures at the same level as the obj file.
When the textures are in a subfolder, the path in the texture is not good. At least for me. Spaces appear or they should not. Or maybe avoid spaces in the path.

Edited by flcc
Link to comment
Share on other sites

  • 1 year later...
  • 3 months later...
On 3/12/2019 at 11:31 PM, ncub said:

Thanks for making this. When I run the script, it brings in all the materials into shop but the obj links to the materials in mat. I just updated the three instances of "shop" in the code and changed to "mat" and it worked.

I'm having the same issue, but there's actually 5 instances of "shop" in the script.

Changed that to "mat" but only the first two materials get created and then it quits with error: "problem detecting redshift_material1 automatic closure."

how did you get it to work?

cheers,

 

Link to comment
Share on other sites

  • 1 month later...

Make sure you set n=0, if you modify the paths under that IF statement. You need to modify both variables, they are matched. One path points to the folder where the textures reside and the other points directly to the . MTL you want to process.

Link to comment
Share on other sites

Thanks Atom, your insanely helpful on this forum! Your answer implies there is a better place to modify the path rather than that IF statement? This part of the script is the only example of 

#select MTL

Is there a way of finding the MTL path in a material network? I only find the Houdini path for a material network i.e. /obj/KB3D_NeoCities/

So I need to set n = 0 because I only want it to process a single material networks

Where do I find the file name to point to the material network I want to process everything inside? Again I really apologise for being really incomprehensed by this

image.thumb.png.c4c05bb1f381a52e9f369f39972c5ea8.png

 

Link to comment
Share on other sites

  • 1 month later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...