stassius

Houdini Obj+Mtl importer

Mar 3rd, 2016
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.64 KB | None | 0 0
  1. import hou
  2. import os
  3. import re
  4. import string
  5.  
  6. # removes all path till the last two slashes
  7. # C:/Model/model1/maps/aaa.jpg -> maps/aaa.jpg
  8. def pathCorrect(path):
  9.     path=path.replace("\\","/")
  10.     split=path.rsplit("/",2)[-2:]
  11.     result=split[0]
  12.     if len(split)>1:
  13.         result+="/"+split[1]
  14.     return result
  15.  
  16. ################################################################
  17. # Shader definition -- Change it to match your shader
  18. ################################################################
  19. # name of the shader
  20. shader='mantrasurface'
  21. # parameters for diffuse color
  22. difr='diff_colorr'
  23. difg='diff_colorg'
  24. difb='diff_colorb'
  25. # parameters for specular color
  26. specr='spec_colorr'
  27. specg='spec_colorg'
  28. specb='spec_colorb'
  29. # parameter for opacity
  30. opacity='opac_int'
  31. #parameter for IOR
  32. ior='ior_in'
  33. # parameters for maps
  34. use_diffmap='diff_colorUseTexture'
  35. diff_map='diff_colorTexture'
  36. use_specmap='refl_colorUseTexture'
  37. spec_map='refl_colorTexture'
  38. use_alphamap='opacity_colorUseTexture'
  39. alpha_map='opacity_colorTexture'
  40. use_bumpmap='enableBumpOrNormalTexture'
  41. bump_map='normalTexture'
  42.  
  43. ###############################################################
  44.  
  45. #input obj
  46. file_name=hou.ui.selectFile(file_type=hou.fileType.Geometry)
  47. file_old=file_name
  48. file_name=string.replace(file_name,'$HIP',hou.expandString('$HIP'),1)
  49. file_path=os.path.split(file_old)[0]
  50.  
  51. geo = hou.node('/obj').createNode('geo')
  52. geo.node('file1').parm('file').set(file_old)
  53. geo.moveToGoodPosition()
  54. name=os.path.splitext(os.path.basename(file_name))[0].replace(' ','_')
  55. if name[0].isdigit(): #Houdini does not allow to name nodes with first digit character
  56.     name="_"+name
  57. geo.setName(name,True) # replace space with _
  58.  
  59. mysop=geo.node('file1').createOutputNode('attribstringedit')
  60. mysop.moveToGoodPosition()
  61.  
  62. shop=geo.createNode('shopnet')
  63. shop.moveToGoodPosition()
  64.  
  65. # create node to relink materials to internal shopnet
  66. mysop.parm('primattriblist').set('shop_materialpath')
  67. mysop.parm('regex0').set(1)
  68. mysop.parm('from0').set('/shop/')
  69. mysop.parm('to0').set('`opfullpath("../shopnet1")+"/"`')
  70. mysop.setDisplayFlag(True)
  71. mysop.setRenderFlag(True)
  72.  
  73. # Create shaders
  74.  
  75. file_name=os.path.splitext(file_name)[0]+".mtl"
  76. # if mtl file not found
  77. if not os.path.isfile(file_name):
  78.     print('No mtl file, choose manually')
  79.     file_name=hou.ui.selectFile()
  80. error=0
  81. last=mysop
  82. cur_mat = None
  83. with open(file_name, 'r') as f:
  84.     lines = f.read().splitlines()   # Read lines
  85. f.close()
  86.  
  87. for line in lines:
  88.     line=line.lstrip()          # remove beginning spaces
  89.     line=' '.join(line.split()) # remove double spaces
  90.     ary = line.split(' ')
  91.     if ary[0] == 'newmtl':
  92.         # Grab the name of this new material
  93.         mat_name = ary[1]  
  94.         cur_mat = shop.createNode(shader)
  95.         cur_mat.moveToGoodPosition()
  96.         if cur_mat.parm('normalTexType'):
  97.             cur_mat.parm('normalTexType').set('bump') # Only for Mantra
  98.         # check if first symbol of mat name is digit (prohibited in Houdini)
  99.         if mat_name[0].isdigit():
  100.             print('MAT NAME CHANGED!')
  101.             # Create node to rename material
  102.             mysop=last.createOutputNode('attribstringedit')
  103.             mysop.moveToGoodPosition()
  104.             mysop.parm('primattriblist').set('shop_materialpath')
  105.             mysop.parm('regex0').set(1)
  106.             mysop.parm('from0').set('shopnet1/'+mat_name)
  107.             mysop.parm('to0').set('shopnet1/_'+mat_name)
  108.             mysop.setDisplayFlag(True)
  109.             mysop.setRenderFlag(True)
  110.             last=mysop
  111.             mat_name='_'+mat_name
  112.        
  113.         # check if mat name has prohibited characters
  114.         if not re.match(r'[:\w-]*$', mat_name):
  115.             print('Mat name is changed')
  116.             mysop=last.createOutputNode('attribstringedit')
  117.             mysop.moveToGoodPosition()
  118.             mysop.parm('primattriblist').set('shop_materialpath')
  119.             mysop.parm('regex0').set(1)
  120.             mysop.parm('from0').set('shopnet1/'+mat_name)
  121.             mysop.parm('to0').set('shopnet1/_'+'mat_changed_'+str(error))
  122.             mysop.setDisplayFlag(True)
  123.             mysop.setRenderFlag(True)
  124.             last=mysop
  125.             mat_name='_'+'mat_changed_'+str(error)
  126.             error+=1
  127.            
  128.         cur_mat.setName(mat_name,True) #rename material
  129.  
  130.     if ary[0] == 'Kd':
  131.         # Found a diffuse color.
  132.         if len(ary) == 4:
  133.             cur_mat.setParms({difr: float(ary[1]),difg: float(ary[2]),difb: float(ary[3])})
  134.     if ary[0] == 'Ks':
  135.         # Found a specular color.
  136.         if len(ary) == 4:
  137.             cur_mat.setParms({specr: float(ary[1]),specg: float(ary[2]),specb: float(ary[3])})
  138.  
  139.     if ary[0] == 'd':
  140.         # Found opacity.
  141.         cur_mat.setParms({opacity: float(ary[1])})
  142.  
  143.     if ary[0] == 'Ni':
  144.         # Found IOR parameter
  145.         cur_mat.setParms({ior: float(ary[1])})
  146.  
  147.  
  148.     # maps
  149.     if ary[0]== 'map_Kd' and len(ary)>1:
  150.         cur_mat.setParms({use_diffmap:1})
  151.         cur_mat.setParms({diff_map:file_path+'/'+pathCorrect(ary[-1])}) # only last word in a string
  152.  
  153.     if ary[0]== 'map_Ks' and len(ary)>1:
  154.         cur_mat.setParms({use_specmap:1})
  155.         cur_mat.setParms({spec_map:file_path+'/'+pathCorrect(ary[-1])})
  156.  
  157.     if ary[0]== 'map_d' and len(ary)>1:
  158.         cur_mat.setParms({use_alphamap:1})
  159.         cur_mat.setParms({alpha_map:file_path+'/'+pathCorrect(ary[-1])})
  160.  
  161.     if ary[0]== 'map_bump' and len(ary)>1:
  162.         cur_mat.setParms({use_bumpmap:1})
  163.         cur_mat.setParms({bump_map:file_path+'/'+pathCorrect(ary[-1])})
  164.  
  165.  
  166. mysop=last.createOutputNode('xform')
  167. mysop.moveToGoodPosition()
  168. mysop.setDisplayFlag(True)
  169. mysop.setRenderFlag(True)
Add Comment
Please, Sign In to add comment