forked from niacdoial/blemd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
materialhelper.py
144 lines (122 loc) · 5.83 KB
/
materialhelper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
135
136
137
138
139
140
141
142
143
144
import bpy
import mathutils
import os
from .texhelper import newtex_tslot, getTexImage, showTextureMap, getTexSlot
from mathutils import Color
from . import matpipeline as MPL
def add_material(obj, mat):
is_in = False
for m2 in obj.data.materials:
if mat == m2:
is_in = True
break
if not is_in:
obj.data.materials.append(mat)
for num, com in enumerate(obj.material_slots):
if com.material == mat:
return num
raise ValueError("material not successfully added")
def add_err_material(obj):
mat = bpy.data.materials.new("ERROR MATERIAL")
mat.diffuse_color = (0,0,1,1) # WHY are those different types of objects?
mat.specular_color = mathutils.Color((1,1,1))
add_material(obj, mat)
return mat
def build_material_legacy(mIndex, mat1, tex, texpath, ext):
material = mat1.materialbases[mIndex]
currMaterial = None # materials may not be defined
stage = material.texStages[0] # simplified material: ONE texture
if stage != 0xffff: # TODO better reproduce the original behavior when that is not the case
currMaterial = bpy.data.materials.new("dummy_temp_name-function_v1")
v2 = mat1.texStageIndexToTextureIndex[stage]
textureName = tex.stringtable[v2]
fileName = os.path.join(texpath, textureName + ext)
bmpFound = os.path.isfile(fileName)
newtex_tslot(fileName, 'DIFFUSE', currMaterial)
img = getTexImage(currMaterial, fileName)
bmp = None
hasAlpha = False
if bmpFound:
# for p in range(3, len(img.pixels), 4): # pixels stored as individual channels
# if img.pixels[p] != 1: # only get alpha
# hasAlpha = True
# break
# else:
# hasAlpha = False
hasAlpha=True
else:
# make it easier to see invalid/missing textures
currMaterial.diffuse_color = (1,0,0,1)
if hasAlpha:
# self._currMaterial.twoSided = True # -- anything with alpha is always two sided?
newtex_tslot(fileName, 'ALPHA', currMaterial)
showTextureMap(currMaterial) # -- display texture in view
# NOTE: check ash.bmd for case when wrapS=2 and wrap=2. u_offset = 0.5 and V_offset = -0.5 [note negative on v]
if bmpFound:
if tex.texHeaders[v2].wrapS == 0:
# clamp to edge? Needs testing. Cannot use .U_Mirror = False and .U_Tile = False. If WrapS == 0 then has Alpha?
pass
elif tex.texHeaders[v2].wrapS == 1: # - repeat (default)
pass
elif tex.texHeaders[v2].wrapS == 2:
# add suffix to let the modeler know where mirror should be used
currMaterial.name += "_U"
getTexSlot(currMaterial, fileName).scale[0] = -1
# self._currMaterial.diffusemap.coords.U_Tile = False
# self._currMaterial.diffusemap.coords.U_offset = 0.5
# self._currMaterial.diffusemap.coords.U_Tiling = 0.5
else:
raise ValueError("Unknown wrapS " + str(tex.texHeaders[v2].wrapS))
if tex.texHeaders[v2].wrapT == 0:
# clamp to edge? Needs testing
pass
elif tex.texHeaders[v2].wrapT == 1:
# repeat (default)
pass
elif tex.texHeaders[v2].wrapT == 2:
# add suffix to let the modeler know where mirror should be used
currMaterial.name += "_V"
getTexSlot(currMaterial, fileName).scale[1] = -1
# self._currMaterial.diffusemap.coords.V_Tile = False
# self._currMaterial.diffusemap.coords.V_offset = 0.5
# self._currMaterial.diffusemap.coords.V_Tiling = 0.5
else:
raise ValueError("Unknown wrapT " + str(tex.texHeaders[v2].wrapS))
return currMaterial
def build_material_v3(mIndex, mat1, tex1, texpath, ext, params):
mbase = mat1.materialbases[mIndex]
material = bpy.data.materials.new('dummy_temp_name-function_v3')
material.use_nodes = True
MPL.createMaterialSystem(mbase, mat1, tex1, texpath, ext, material.node_tree, params)
return material
# mat1.materials[mIndex] = msys
def build_material_simple(mIndex, mat1, tex1, texpath, ext, params):
mbase = mat1.materialbases[mIndex]
material = bpy.data.materials.new('dummy_temp_name-function_simple')
material.use_nodes = True
MPL.create_simple_material_system(mbase, mat1, tex1, texpath, ext, material.node_tree, params)
return material
# mat1.materials[mIndex] = msys
def add_vcolor(mesh, representation, layerID):
"""copies vertex colors (layer `layerID`) from the representation to the blender mesh"""
vx_layer = mesh.vertex_colors.new(name="v_color_"+str(layerID))
# some really recent, unstable versions of blender 2.79 have alpha support in vertex colors.
# detect this and react accordingly (or an exception will be raised)
try:
vx_layer.data[0].color = (1,0,0,0)
alpha_support = True
except:
alpha_support = False
vx_layer_a = mesh.vertex_colors.new(name="v_color_alpha_"+str(layerID))
# alpimg = bpy.data.images.new(mesh.name+'_vcol_alpha_'+str(layerID), 256, 256)
# XCX image method buggy -> disabled
if alpha_support:
for num, com in enumerate(representation.loops):
if com.VColors[layerID] is not None:
vx_layer.data[num].color = com.VColors[layerID]
else:
for num, com in enumerate(representation.loops):
if com.VColors[layerID] is not None:
vx_layer.data[num].color = mathutils.Color(com.VColors[layerID][:3])
vx_layer_a.data[num].color = mathutils.Color((com.VColors[layerID][3],)*3)
# else, will be white