Jump to content

.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.
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]
        # 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.
    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
Link to comment
Share on other sites

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.

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...