Newer
Older
import bpy
import os
import filecmp
def objname(path):
path2 = os.path.dirname(path) + os.sep
pituus = len(path2)
nimi = path[pituus:]
return nimi
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):
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'):
Kalle-Samuli Riihikoski
committed
if tex_slot.texture.image is not None:
tex_slot.texture.image.reload()
if(coa.texturefolder):
osoite = os.path.dirname(coa.texturefolder) + os.sep
else:
osoite = os.path.dirname(coa.objectdir) + os.sep
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
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):
Kalle-Samuli Riihikoski
committed
index = 0
tuli = False
lasku = False
while(lasku == False):
tuli = False
new_mat = ("Material.%03d"%(index))
for i in bpy.data.materials:
if(i.name == new_mat):
tuli = True
break
if(tuli):
index += 1
else:
lasku = True
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
Kalle-Samuli Riihikoski
committed
indexx = 0
tuli = False
lasku = False
while(lasku == False):
tuli = False
name_tex = ("Color_%s"%(indexx))
for i in bpy.data.textures:
if(i.name == name_tex):
tuli = True
break
if(tuli):
indexx += 1
else:
lasku = True
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 == ''):
Kalle-Samuli Riihikoski
committed
indexx = 0
tuli = False
lasku = False
while(lasku == False):
tuli = False
name_tex = ("Normal_%s"%(indexx))
for i in bpy.data.textures:
if(i.name == name_tex):
tuli = True
break
if(tuli):
indexx += 1
else:
lasku = True
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
objekti.active_material.texture_slots[index].normal_map_space = 'TANGENT'
Kalle-Samuli Riihikoski
committed
objekti.active_material.texture_slots[index].normal_factor = 1
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
Kalle-Samuli Riihikoski
committed
objekti.active_material.texture_slots[index].normal_factor = 1
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 == ''):
Kalle-Samuli Riihikoski
committed
indexx = 0
tuli = False
lasku = False
while(lasku == False):
tuli = False
name_tex = ("Specular_%s"%(indexx))
for i in bpy.data.textures:
if(i.name == name_tex):
tuli = True
break
if(tuli):
indexx += 1
else:
lasku = True
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
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 == ''):
Kalle-Samuli Riihikoski
committed
indexx = 0
tuli = False
lasku = False
while(lasku == False):
tuli = False
name_tex = ("Displacement_%s"%(indexx))
for i in bpy.data.textures:
if(i.name == name_tex):
tuli = True
break
if(tuli):
indexx += 1
else:
lasku = True
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
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')