3 import io_directx_bel.bel.fs
4 from io_directx_bel import bel
12 # create or retrieve a bdata image
14 def new(path, name=False, relative = True, premul = True) :
15 path = bel.fs.clean(path)
17 if bel.fs.isfile(path) == False :
18 dprint('Texture image not found')
23 path = bpy.path.relpath(path)
24 path = bel.fs.clean(path)
26 print('cant turn path into relative one (.blend and img path drive letters ?) ')
28 # retrieve paths to image file from existing image slot
29 # returns img if paths match
30 for img in bpy.data.images :
31 if img.filepath != '' :
32 if bpy.path.abspath(path) == bpy.path.abspath(bel.fs.clean(img.filepath)) :
35 # create a unique name in image slot
37 name = bpy.path.basename(path)
38 name = bel.bpyname(name,bpy.data.images.keys())
41 img = bpy.data.images.load(filepath=path)
43 img.use_premultiply = premul
47 def applyShader(mat,config) :
49 # matslot.link = 'DATA'
50 #mat = bpy.data.materials['Female_Body']
52 texslot = mat.texture_slots[0]
56 #config = shaders[shadername]
57 alpha = True if 'alpha' in config else False
63 mat.diffuse_color = Color((0.6, 0.6, 0.6))
64 mat.diffuse_intensity = 0.8
65 mat.diffuse_shader = 'LAMBERT'
66 mat.use_diffuse_ramp = False
69 mat.specular_color = Color((1.0, 1.0, 1.0))
70 mat.specular_intensity = 0.25
71 mat.specular_shader = 'COOKTORR'
72 mat.use_specular_ramp = False
73 mat.specular_hardness = 1.0
78 mat.translucency = 0.0
79 mat.use_shadeless = False
80 mat.use_tangent_shading = False
84 mat.use_transparency = alpha
85 mat.transparency_method = 'Z_TRANSPARENCY'
86 mat.alpha = not(alpha)
87 mat.specular_alpha = not(alpha)
88 mat.raytrace_transparency.fresnel = 0.0
89 mat.raytrace_transparency.fresnel_factor = 1.25
92 mat.raytrace_mirror.use = False
94 # subsurface_scattering
95 mat.subsurface_scattering.use
100 mat.use_shadows = True
101 mat.use_transparent_shadows = True
102 mat.use_cast_shadows_only = False
103 mat.shadow_cast_alpha = 1.0
104 mat.use_only_shadow = False
105 mat.shadow_only_type = 'SHADOW_ONLY_OLD'
106 mat.use_cast_buffer_shadows = True
107 mat.shadow_buffer_bias = 0.0
108 mat.use_ray_shadow_bias = True
109 mat.shadow_ray_bias = 0.0
110 mat.use_cast_approximate = True
115 texslot.diffuse_factor = 1.0
116 texslot.use_map_diffuse = True
117 texslot.diffuse_color_factor = 1.0
118 texslot.use_map_color_diffuse = True
119 texslot.alpha_factor = 1.0
120 texslot.use_map_alpha = alpha
121 texslot.translucency_factor = 0.0
122 texslot.use_map_translucency = False
125 texslot.specular_factor = 0.3
126 texslot.use_map_specular = True
127 texslot.specular_color_factor = 1.0
128 texslot.use_map_color_spec = True
129 texslot.hardness_factor = 0.1
130 texslot.use_map_hardness = True
133 texslot.ambient_factor = 0.0
134 texslot.use_map_ambient = False
135 texslot.emit_factor = 0.1
136 texslot.use_map_emit = True
137 texslot.mirror_factor = 0.0
138 texslot.use_map_mirror = False
139 texslot.raymir_factor = 0.0
140 texslot.use_map_raymir = False
143 texslot.normal_factor = 0.0
144 texslot.use_map_normal = False
145 texslot.warp_factor = 0.1
146 texslot.use_map_warp = False
147 texslot.displacement_factor = 0.0
148 texslot.use_map_displacement = False
150 texslot.blend_type = 'MIX'
151 texslot.invert = False
152 texslot.use_rgb_to_intensity = False
153 texslot.color = Color((1.0, 0.0, 1.0)) # default
154 texslot.use_stencil = False
155 texslot.default_value = 1.0
158 tex.use_alpha = alpha
159 tex.use_preview_alpha = alpha
162 if type(img) != type(None) :
163 img.use_premultiply = True
165 def BSshader(nodes,pointer) :
166 tkm = bpy.context.scene.tkm
167 typ, nodename = pointer.split(' ')
168 RenderShader = nodes[typ][nodename]
169 name = BSname(nodename,RenderShader['Object.Name'])
170 if name in bpy.data.materials :
171 mat = bpy.data.materials[name]
173 mat = bpy.data.materials.new(name=name)
175 DepthWriteEnable = RenderShader['DepthWriteEnable'] if 'DepthWriteEnable' in RenderShader else False # an integer
176 ShaderTransparency = RenderShader['MultiDrawLayer'] if 'MultiDrawLayer' in RenderShader else False # an integer
177 LightEnable = RenderShader['LightEnable'] if 'LightEnable' in RenderShader else False # an integer
179 ShaderPhong = BSnode(nodes,RenderShader['Surface'])
180 #print('mat : %s'%ShaderPhong['Material'])
181 RenderMaterial = BSnode(nodes,ShaderPhong['Material'])
182 DiffuseColor = RenderMaterial['DiffuseColor'] if 'DiffuseColor' in RenderMaterial else False
183 SpecularColor = RenderMaterial['SpecularColor'] if 'SpecularColor' in RenderMaterial else False
184 AmbientColor = RenderMaterial['AmbientColor'] if 'AmbientColor' in RenderMaterial else False
185 EmissionColor = RenderMaterial['Shininess'] if 'EmissionColor' in RenderMaterial else False
186 Shininess = RenderMaterial['Shininess'] if 'Shininess' in RenderMaterial else False
187 Transparency = RenderMaterial['Transparency'] if 'Transparency' in RenderMaterial else False
188 for key in RenderMaterial.keys() :
189 if key not in ['DiffuseColor','SpecularColor','AmbientColor','EmissionColor','Shininess','Transparency'] :
190 print('NEW RENDERMATERIAL PROP ! : %s'%key)
193 if DiffuseColor : mat.diffuse_color = Color(DiffuseColor) #[0][0],DiffuseColor[0][1],DiffuseColor[0][2])
194 if SpecularColor : mat.specular_color = Color(SpecularColor)#[0][0],SpecularColor[0][1],SpecularColor[0][2])
195 if AmbientColor : mat.ambient = AmbientColor[0] # source value is a vector3f with x=y=z
196 if EmissionColor : mat.emit = EmissionColor[0] # source value is a vector3f with x=y=z
198 #alpha is a boolean, whereas Transparency is a float or False
200 mat.use_transparency = True
201 mat.transparency_method = 'Z_TRANSPARENCY'
202 mat.alpha = Transparency
203 mat.specular_alpha = 0
207 if 'Color' in ShaderPhong :
208 ShaderTexture = BSnode(nodes,ShaderPhong['Color'])
209 texinfluence = 'Color'
210 if 'Reflection' in ShaderPhong :
211 ShaderTexture = BSnode(nodes,ShaderPhong['Reflection'])
212 texinfluence = 'Reflection'
213 if texinfluence == False :
214 print('neither color nor refl. in ShaderPhong %s'%RenderShader['Surface'])
215 print('other props are : %s'%ShaderPhong.keys())
218 ShaderTextureName = ShaderTexture['Object.Name']
220 Texture2D = BSnode(nodes,ShaderTexture['Texture'])
221 Texture2DName = Texture2D['Object.Name']
223 FileObject = BSnode(nodes,Texture2D['Texture.FileObject'])
224 imgpath = FileObject['FileName']
225 imgname = imgpath.split('/')[-1]
226 imgpath = tkm.path_archives+'/Images/Q=Tex032M/'+imgpath
228 if imgname not in bpy.data.images :
229 if os.path.isfile(imgpath+'.png') : ext = '.png'
230 elif os.path.isfile(imgpath+'.jp2') : ext = '.jp2'
232 print('Texture image not found ! %s'%Texture2D['Texture.FileObject'])
233 print('path : %s.png or .jp2 '%imgpath)
235 img = bpy.data.images.load(filepath=imgpath+ext)
237 img.use_premultiply = True
238 else : img = bpy.data.images[imgname]
241 texslot = mat.texture_slots[0]
243 tex = texslot.texture
248 #img = bpy.data.images.new(name='imgname',width=640, height=512)
250 if ShaderTextureName not in bpy.data.textures :
251 tex = bpy.data.textures.new(name=ShaderTextureName,type='IMAGE')
253 tex.use_alpha = alpha
254 tex.use_preview_alpha = alpha
255 else : tex = bpy.data.textures[ShaderTextureName]
257 texslot = mat.texture_slots.create(index=0)
258 texslot.texture = tex
259 texslot.texture_coords = 'UV'
260 texslot.uv_layer = 'UV0'
261 texslot.use_map_alpha = alpha
262 texslot.alpha_factor = 1.0