Newer
Older
import bpy
import os
import filecmp
def objname(path):
path2 = os.path.dirname(path) + os.sep
pituus = len(path2)
nimi = path[pituus:]
monesko = name.rfind('.')
justname = name[:monesko]
return justname
newname =''
tex_name =[]
index_tex = 0
for tt in bpy.data.textures:
tex_name.append(tt.name)
return tex_name
def find_index(objekti):
luku = 0
for tex in objekti.active_material.texture_slots:
if(not(hasattr(tex,'texture'))):
break
luku = luku +1
return luku
def gettex(mat_list, objekti, scene,export):
coat3D = bpy.context.scene.coat3D
if(bpy.context.scene.render.engine == 'VRAY_RENDER' or bpy.context.scene.render.engine == 'VRAY_RENDER_PREVIEW'):
vray = True
else:
vray = False
take_color = 0;
take_spec = 0;
take_normal = 0;
take_disp = 0;
bring_color = 1;
bring_spec = 1;
bring_normal = 1;
bring_disp = 1;
texcoat = {}
texcoat['color'] = []
texcoat['specular'] = []
texcoat['nmap'] = []
texcoat['disp'] = []
texu = []
if(export):
objekti.coat3D.objpath = export
nimi = objname(export)
osoite = os.path.dirname(export) + os.sep
for mate in objekti.material_slots:
for tex_slot in mate.material.texture_slots:
if(hasattr(tex_slot,'texture')):
if(tex_slot.texture.type == 'IMAGE'):
tex_slot.texture.image.reload()
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
else:
nimi = objname(coat3D.objectdir)
osoite = os.path.dirname(coat3D.objectdir) + os.sep
just_nimi = justname(nimi)
just_nimi += '_'
just_nimi_len = len(just_nimi)
if(len(objekti.material_slots) != 0):
for obj_tex in objekti.active_material.texture_slots:
if(hasattr(obj_tex,'texture')):
if(obj_tex.texture):
if(obj_tex.use_map_color_diffuse):
bring_color = 0;
if(obj_tex.use_map_specular):
bring_spec = 0;
if(obj_tex.use_map_normal):
bring_normal = 0;
if(obj_tex.use_map_displacement):
bring_disp = 0;
files = os.listdir(osoite)
for i in files:
tui = i[:just_nimi_len]
if(tui == just_nimi):
texu.append(i)
for yy in texu:
minimi = (yy.rfind('_'))+1
maksimi = (yy.rfind('.'))
tex_name = yy[minimi:maksimi]
koko = ''
koko += osoite
koko += yy
texcoat[tex_name].append(koko)
#date = os.path.getmtime(texcoat[tex_name][0])
if((texcoat['color'] or texcoat['nmap'] or texcoat['disp'] or texcoat['specular']) and (len(objekti.material_slots)) == 0):
new_mat = ("%s_Material"%(objekti.name))
bpy.data.materials.new(new_mat)
ki = bpy.data.materials[new_mat]
objekti.data.materials.append(ki)
if(bring_color == 1 and texcoat['color']):
name_tex ='Color_'
num = []
index = find_index(objekti)
tex = bpy.ops.Texture
objekti.active_material.texture_slots.create(index)
total_mat = len(objekti.active_material.texture_slots.items())
useold = ''
for seekco in bpy.data.textures:
if((seekco.name[:6] == 'Color_') and (seekco.users_material == ())):
useold = seekco
for num_tex in tex_name:
if(num_tex[:6] == 'Color_'):
num.append(num_tex)
luku_tex = len(num)
name_tex = ('Color_%s'%(luku_tex))
bpy.ops.image.new(name=name_tex)
bpy.data.images[name_tex].filepath = texcoat['color'][0]
bpy.data.images[name_tex].source = 'FILE'
bpy.data.textures.new(name_tex,type='IMAGE')
objekti.active_material.texture_slots[index].texture = bpy.data.textures[name_tex]
objekti.active_material.texture_slots[index].texture.image = bpy.data.images[name_tex]
if(objekti.data.uv_textures.active):
objekti.active_material.texture_slots[index].texture_coords = 'UV'
objekti.active_material.texture_slots[index].uv_layer = objekti.data.uv_textures.active.name
objekti.active_material.texture_slots[index].texture.image.reload()
elif(useold != ''):
objekti.active_material.texture_slots[index].texture = useold
objekti.active_material.texture_slots[index].texture.image.filepath = texcoat['color'][0]
if(objekti.data.uv_textures.active):
objekti.active_material.texture_slots[index].texture_coords = 'UV'
objekti.active_material.texture_slots[index].uv_layer = objekti.data.uv_textures.active.name
if(bring_normal == 1 and texcoat['nmap']):
name_tex ='Normal_'
num = []
index = find_index(objekti)
tex = bpy.ops.Texture
objekti.active_material.texture_slots.create(index)
total_mat = len(objekti.active_material.texture_slots.items())
useold = ''
for seekco in bpy.data.textures:
if((seekco.name[:7] == 'Normal_') and (seekco.users_material == ())):
useold = seekco
if(useold == ''):
tex_name = setgallery()
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
for num_tex in tex_name:
if(num_tex[:7] == 'Normal_'):
num.append(num_tex)
luku_tex = len(num)
name_tex = ('Normal_%s'%(luku_tex))
bpy.ops.image.new(name=name_tex)
bpy.data.images[name_tex].filepath = texcoat['nmap'][0]
bpy.data.images[name_tex].source = 'FILE'
bpy.data.textures.new(name_tex,type='IMAGE')
objekti.active_material.texture_slots[index].texture = bpy.data.textures[name_tex]
objekti.active_material.texture_slots[index].texture.image = bpy.data.images[name_tex]
if(objekti.data.uv_textures.active):
objekti.active_material.texture_slots[index].texture_coords = 'UV'
objekti.active_material.texture_slots[index].uv_layer = objekti.data.uv_textures.active.name
objekti.active_material.texture_slots[index].use_map_color_diffuse = False
objekti.active_material.texture_slots[index].use_map_normal = True
objekti.active_material.texture_slots[index].texture.image.reload()
if(vray):
bpy.data.textures[name_tex].vray_slot.BRDFBump.map_type = 'TANGENT'
else:
bpy.data.textures[name_tex].use_normal_map = True
bpy.data.textures[name_tex].normal_space = 'TANGENT'
elif(useold != ''):
objekti.active_material.texture_slots[index].texture = useold
objekti.active_material.texture_slots[index].texture.image.filepath = texcoat['nmap'][0]
if(objekti.data.uv_textures.active):
objekti.active_material.texture_slots[index].texture_coords = 'UV'
objekti.active_material.texture_slots[index].uv_layer = objekti.data.uv_textures.active.name
objekti.active_material.texture_slots[index].use_map_color_diffuse = False
objekti.active_material.texture_slots[index].use_map_normal = True
if(bring_spec == 1 and texcoat['specular']):
name_tex ='Specular_'
num = []
index = find_index(objekti)
tex = bpy.ops.Texture
objekti.active_material.texture_slots.create(index)
total_mat = len(objekti.active_material.texture_slots.items())
useold = ''
for seekco in bpy.data.textures:
if((seekco.name[:9] == 'Specular_') and (seekco.users_material == ())):
useold = seekco
if(useold == ''):
tex_name = setgallery()
for num_tex in tex_name:
if(num_tex[:9] == 'Specular_'):
num.append(num_tex)
luku_tex = len(num)
name_tex = ('Specular_%s'%(luku_tex))
bpy.ops.image.new(name=name_tex)
bpy.data.images[name_tex].filepath = texcoat['specular'][0]
bpy.data.images[name_tex].source = 'FILE'
bpy.data.textures.new(name_tex,type='IMAGE')
objekti.active_material.texture_slots[index].texture = bpy.data.textures[name_tex]
objekti.active_material.texture_slots[index].texture.image = bpy.data.images[name_tex]
if(objekti.data.uv_textures.active):
objekti.active_material.texture_slots[index].texture_coords = 'UV'
objekti.active_material.texture_slots[index].uv_layer = objekti.data.uv_textures.active.name
objekti.active_material.texture_slots[index].use_map_color_diffuse = False
objekti.active_material.texture_slots[index].use_map_specular = True
objekti.active_material.texture_slots[index].texture.image.reload()
elif(useold != ''):
objekti.active_material.texture_slots[index].texture = useold
objekti.active_material.texture_slots[index].texture.image.filepath = texcoat['specular'][0]
if(objekti.data.uv_textures.active):
objekti.active_material.texture_slots[index].texture_coords = 'UV'
objekti.active_material.texture_slots[index].uv_layer = objekti.data.uv_textures.active.name
objekti.active_material.texture_slots[index].use_map_color_diffuse = False
objekti.active_material.texture_slots[index].use_map_specular = True
if(bring_disp == 1 and texcoat['disp']):
name_tex ='Displacement_'
num = []
index = find_index(objekti)
tex = bpy.ops.Texture
objekti.active_material.texture_slots.create(index)
total_mat = len(objekti.active_material.texture_slots.items())
useold = ''
for seekco in bpy.data.textures:
if((seekco.name[:13] == 'Displacement_') and (seekco.users_material == ())):
useold = seekco
if(useold == ''):
tex_name = setgallery()
for num_tex in tex_name:
if(num_tex[:13] == 'Displacement_'):
num.append(num_tex)
luku_tex = len(num)
name_tex = ('Displacement_%s'%(luku_tex))
bpy.ops.image.new(name=name_tex)
bpy.data.images[name_tex].filepath = texcoat['disp'][0]
bpy.data.images[name_tex].source = 'FILE'
bpy.data.textures.new(name_tex,type='IMAGE')
objekti.active_material.texture_slots[index].texture = bpy.data.textures[name_tex]
objekti.active_material.texture_slots[index].texture.image = bpy.data.images[name_tex]
if(objekti.data.uv_textures.active):
objekti.active_material.texture_slots[index].texture_coords = 'UV'
objekti.active_material.texture_slots[index].uv_layer = objekti.data.uv_textures.active.name
objekti.active_material.texture_slots[index].use_map_color_diffuse = False
objekti.active_material.texture_slots[index].use_map_displacement = True
objekti.active_material.texture_slots[index].texture.image.reload()
elif(useold != ''):
objekti.active_material.texture_slots[index].texture = useold
objekti.active_material.texture_slots[index].texture.image.filepath = texcoat['disp'][0]
if(objekti.data.uv_textures.active):
objekti.active_material.texture_slots[index].texture_coords = 'UV'
objekti.active_material.texture_slots[index].uv_layer = objekti.data.uv_textures.active.name
objekti.active_material.texture_slots[index].use_map_color_diffuse = False
objekti.active_material.texture_slots[index].use_map_displacement = True
if(vray):
objekti.active_material.texture_slots[index].texture.use_interpolation = False
objekti.active_material.texture_slots[index].displacement_factor = 0.05
else:
disp_modi = ''
for seek_modi in objekti.modifiers:
if(seek_modi.type == 'DISPLACE'):
disp_modi = seek_modi
break
if(disp_modi):
disp_modi.texture = objekti.active_material.texture_slots[index].texture
if(objekti.data.uv_textures.active):
disp_modi.texture_coords = 'UV'
disp_modi.uv_layer = objekti.data.uv_textures.active.name
else:
objekti.modifiers.new('Displace',type='DISPLACE')
objekti.modifiers['Displace'].texture = objekti.active_material.texture_slots[index].texture
if(objekti.data.uv_textures.active):
objekti.modifiers['Displace'].texture_coords = 'UV'
objekti.modifiers['Displace'].uv_layer = objekti.data.uv_textures.active.name
return('FINISHED')