View difference between Paste ID: C1H0D4HT and MwYPAZ1t
SHOW: | | - or go back to the newest paste.
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)
52+
fl=geo.createNode('file')
53
fl.parm('file').set(file_old)
54
fl.moveToGoodPosition()
55
geo.moveToGoodPosition()
56
name=os.path.splitext(os.path.basename(file_name))[0].replace(' ','_')
57
if name[0].isdigit(): #Houdini does not allow to name nodes with first digit character
58
    name="_"+name
59
geo.setName(name,True) # replace space with _
60
61
mysop=geo.node('file1').createOutputNode('attribstringedit')
62
mysop.moveToGoodPosition()
63
64
shop=geo.createNode('shopnet')
65
shop.moveToGoodPosition()
66
67
# create node to relink materials to internal shopnet
68-
mysop.parm('from0').set('/shop/')
68+
69
mysop.parm('regex0').set(1)
70
mysop.parm('from0').set('/mat/')
71
mysop.parm('to0').set('`opfullpath("../shopnet1")+"/"`')
72
mysop.setDisplayFlag(True)
73
mysop.setRenderFlag(True)
74
75
# Create shaders
76
77
file_name=os.path.splitext(file_name)[0]+".mtl"
78
# if mtl file not found
79
if not os.path.isfile(file_name): 
80
    print('No mtl file, choose manually')
81
    file_name=hou.ui.selectFile()
82
error=0
83
last=mysop
84
cur_mat = None
85
with open(file_name, 'r') as f:
86
    lines = f.read().splitlines()   # Read lines
87
f.close()
88
 
89
for line in lines:
90
    line=line.lstrip()          # remove beginning spaces
91
    line=' '.join(line.split()) # remove double spaces
92
    ary = line.split(' ')
93
    if ary[0] == 'newmtl':
94
        # Grab the name of this new material
95
        mat_name = ary[1]   
96
        cur_mat = shop.createNode(shader)
97
        cur_mat.moveToGoodPosition()
98
        if cur_mat.parm('normalTexType'):
99
            cur_mat.parm('normalTexType').set('bump') # Only for Mantra
100
        # check if first symbol of mat name is digit (prohibited in Houdini)
101
        if mat_name[0].isdigit():
102
            print('MAT NAME CHANGED!')
103
            # Create node to rename material
104
            mysop=last.createOutputNode('attribstringedit')
105
            mysop.moveToGoodPosition()
106
            mysop.parm('primattriblist').set('shop_materialpath')
107
            mysop.parm('regex0').set(1)
108
            mysop.parm('from0').set('shopnet1/'+mat_name)
109
            mysop.parm('to0').set('shopnet1/_'+mat_name)
110
            mysop.setDisplayFlag(True)
111
            mysop.setRenderFlag(True)
112
            last=mysop
113
            mat_name='_'+mat_name
114
        
115
        # check if mat name has prohibited characters
116
        if not re.match(r'[:\w-]*$', mat_name):
117
            print('Mat name is changed')
118
            mysop=last.createOutputNode('attribstringedit')
119
            mysop.moveToGoodPosition()
120
            mysop.parm('primattriblist').set('shop_materialpath')
121
            mysop.parm('regex0').set(1)
122
            mysop.parm('from0').set('shopnet1/'+mat_name)
123
            mysop.parm('to0').set('shopnet1/_'+'mat_changed_'+str(error))
124
            mysop.setDisplayFlag(True)
125
            mysop.setRenderFlag(True)
126
            last=mysop
127
            mat_name='_'+'mat_changed_'+str(error)
128
            error+=1
129
            
130
        cur_mat.setName(mat_name,True) #rename material
131
132
    if ary[0] == 'Kd':
133
        # Found a diffuse color.
134
        if len(ary) == 4:
135
            cur_mat.setParms({difr: float(ary[1]),difg: float(ary[2]),difb: float(ary[3])})
136
    if ary[0] == 'Ks':
137
        # Found a specular color.
138
        if len(ary) == 4:
139
            cur_mat.setParms({specr: float(ary[1]),specg: float(ary[2]),specb: float(ary[3])})
140
141
    if ary[0] == 'd':
142
        # Found opacity.
143
        cur_mat.setParms({opacity: float(ary[1])})
144
145
    if ary[0] == 'Ni':
146
        # Found IOR parameter
147
        cur_mat.setParms({ior: float(ary[1])})
148
149
150
    # maps
151
    if ary[0]== 'map_Kd' and len(ary)>1:
152
        cur_mat.setParms({use_diffmap:1})
153
        cur_mat.setParms({diff_map:file_path+'/'+pathCorrect(ary[-1])}) # only last word in a string
154
155
    if ary[0]== 'map_Ks' and len(ary)>1:
156
        cur_mat.setParms({use_specmap:1})
157
        cur_mat.setParms({spec_map:file_path+'/'+pathCorrect(ary[-1])})
158
159
    if ary[0]== 'map_d' and len(ary)>1:
160
        cur_mat.setParms({use_alphamap:1})
161
        cur_mat.setParms({alpha_map:file_path+'/'+pathCorrect(ary[-1])})
162
163
    if ary[0]== 'map_bump' and len(ary)>1:
164
        cur_mat.setParms({use_bumpmap:1})
165
        cur_mat.setParms({bump_map:file_path+'/'+pathCorrect(ary[-1])})
166
167
168
mysop=last.createOutputNode('xform')
169
mysop.moveToGoodPosition()
170
mysop.setDisplayFlag(True)
171
mysop.setRenderFlag(True)