Jump to content
Sign in to follow this  
Atom

.MTL File Reader

Recommended Posts

Hi All,

 

I have put together a simple .MTL file reader that will create a mantra surface for each entry listed in a .MTL file. Currently only diffuse and specular are only supported but it is a way to at least get all your materials created and named correctly with basic coloring intact. If your OBJ file was exported with material groups then the group names in the OBJ will match the newly create material names.

 

Replace the file_name string variable with a path to your .MTL file.

# Simple .MTL reader.
# Run after .OBJ import.
# Reads Blender generated .MTL files and creates a Houdini Mantra surface as best possible.
# EXAMPLE: Expected format.
#newmtl Reflector.001               <- Material name.
#Ns 96.078431                       <- Specular intensity
#Ka 0.000000 0.000000 0.000000      <- Ambient color.
#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.
#illum 2                            <- Illumination model, expect 2..probably will not support.
 
import hou
 
cur_mat = None
file_name = '/Users/Developer/Downloads/cc0_Enterprise_NCC-1701/27_Enterprise-1701.004.mtl'
with open(file_name, 'r') as f:
    lines = f.read().splitlines()   # Remove slash n character at the end of each line.
f.close()
 
for line in lines:
    ary = line.split(' ')
    if ary[0] == 'newmtl':
        # Grab the name of this new material and create a mantra surface that we can populate.
        mat_name = ary[1]   # Kind of assuming that only one space will be between the token and the value.
        gal_entry = hou.galleries.galleryEntries('mantrasurface')[0]
        gal_entry.createChildNode(hou.node("/shop"))
        
        # NOTE: The newly created material is always called 'mantrasurface' so we can fetch it reliably from a constant name.
        cur_mat = hou.node("/shop/mantrasurface")
        # Set the name to the name from the .MTL file.
        cur_mat.setName(mat_name)
    if ary[0] == 'Kd':
        # Found a diffuse color.
        if len(ary) == 4:
            cur_mat.setParms({'baseColorr': float(ary[1]),'baseColorg': float(ary[2]),'baseColorb': float(ary[3])})
    if ary[0] == 'Ks':
        # Found a specular color.
        if len(ary) == 4:
            cur_mat.setParms({'specColor1r': float(ary[1]),'specColor1g': float(ary[2]),'specColor1b': float(ary[3])})
            cur_mat.setParms({'specColor2r': float(ary[1]),'specColor2g': float(ary[2]),'specColor2b': float(ary[3])})
    if ary[0] == 'd':
        # Found 'disolve' parameter which is really opacity.
        cur_mat.setParms({'opac_int': float(ary[1])})
Edited by Atom
  • Like 2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×