def initMaterial(self): """Create the empty material at the core Principled Node""" self.material = bpy.data.materials.new(name=self.name) self.material.use_nodes = True principled_mat = PrincipledBSDFWrapper(self.material, is_readonly=False) principled_mat.roughness = 1.0 self.principled_node = principled_mat.node_principled_bsdf self.mat_output = principled_mat.node_out
def createMaterial(self): mat = bpy.data.materials.new(name=self.name) mat.use_nodes = True nodes = mat.node_tree.nodes links = mat.node_tree.links principled_mat = PrincipledBSDFWrapper(mat, is_readonly=False) principled = principled_mat.node_principled_bsdf back_principled = None mat_output = principled_mat.node_out principled_mat.roughness = 1.0 for map_name, img in self.maps.items(): if img is None or map_name.split("_")[0] not in __class__.input_tr: continue current_principled = principled if map_name.endswith("_back"): if back_principled is None: # Create backface principled and connect it back_principled = nodes.new(type="ShaderNodeBsdfPrincipled") geometry_node = nodes.new(type="ShaderNodeNewGeometry") mix_node = nodes.new(type="ShaderNodeMixShader") back_principled.inputs["Roughness"].default_value = 1.0 links.new(geometry_node.outputs["Backfacing"], mix_node.inputs[0]) links.new(principled.outputs[0], mix_node.inputs[1]) links.new(back_principled.outputs[0], mix_node.inputs[2]) links.new(mix_node.outputs[0], mat_output.inputs[0]) current_principled = back_principled map_name = map_name[:-5] # remove "_back" texture_node = nodes.new(type="ShaderNodeTexImage") texture_node.image = getCyclesImage(img) texture_node.image.colorspace_settings.name = "sRGB" if map_name == "baseColor" else "Non-Color" if hasattr(texture_node, "color_space"): texture_node.color_space = "COLOR" if map_name == "baseColor" else "NONE" elif map_name == "normal": normal_node = nodes.new(type="ShaderNodeNormalMap") links.new(texture_node.outputs["Color"], normal_node.inputs["Color"]) links.new(normal_node.outputs["Normal"], current_principled.inputs["Normal"]) else: links.new(texture_node.outputs["Color"], current_principled.inputs[__class__.input_tr[map_name]]) if map_name == "opacity": mat.blend_method = 'BLEND' autoAlignNodes(mat_output) return mat
def createBubbleMaterial(name, baseColor): mat = bpy.data.materials.new(name) mat.use_nodes = True mat.blend_method = 'BLEND' principled = PrincipledBSDFWrapper(mat, is_readonly=False) principled.base_color = baseColor principled.metallic = 0.5 principled.specular = 0.2 principled.roughness = 0.05 #principled.IOR = 1.1 principled.alpha = 0.3 # principled.specular_texture.image = load_image("/path/to/image.png") # Export principled = PrincipledBSDFWrapper(mat, is_readonly=True) base_color = principled.base_color specular_texture = principled.specular_texture if specular_texture and specular_texture.image: specular_texture_filepath = principled.specular_texture.image.filepath
def handle_materials(context, model, materials, update): """ """ for m in model.Materials: matname = material_name(m) if matname not in materials: blmat = utils.get_iddata(context.blend_data.materials, None, m.Name, None) if update: blmat.use_nodes = True refl = m.Reflectivity transp = m.Transparency ior = m.IndexOfRefraction roughness = m.ReflectionGlossiness transrough = m.RefractionGlossiness spec = m.Shine / 255.0 if m.DiffuseColor == _black and m.Reflectivity > 0.0 and m.Transparency == 0.0: r, g, b, _ = m.ReflectionColor elif m.DiffuseColor == _black and m.Reflectivity == 0.0 and m.Transparency > 0.0: r, g, b, _ = m.TransparentColor refl = 0.0 elif m.DiffuseColor == _black and m.Reflectivity > 0.0 and m.Transparency > 0.0: r, g, b, _ = m.TransparentColor refl = 0.0 else: r, g, b, a = m.DiffuseColor if refl > 0.0 and transp > 0.0: refl = 0.0 principled = PrincipledBSDFWrapper(blmat, is_readonly=False) principled.base_color = (r / 255.0, g / 255.0, b / 255.0) principled.metallic = refl principled.transmission = transp principled.ior = ior principled.roughness = roughness principled.specular = spec principled.node_principled_bsdf.inputs[ 16].default_value = transrough materials[matname] = blmat
def createMaterial(self): mat = bpy.data.materials.new(name=self.name) mat.use_nodes = True nodes = mat.node_tree.nodes links = mat.node_tree.links principled_mat = PrincipledBSDFWrapper(mat, is_readonly=False) principled = principled_mat.node_principled_bsdf mat_output = principled_mat.node_out principled_mat.roughness = 1.0 front = {} back = {} # Create all of the texture nodes for map_name, img in self.maps.items(): if img is None or map_name.split("_")[0] not in __class__.input_tr: continue texture_node = nodes.new(type="ShaderNodeTexImage") if map_name.endswith("_back"): map_name = map_name[:-5] # remove "_back" back[map_name] = texture_node else: front[map_name] = texture_node texture_node.image = getCyclesImage(img) texture_node.image.colorspace_settings.name = "sRGB" if map_name == "baseColor" or map_name == "diffuse" else "Non-Color" if hasattr(texture_node, "color_space"): texture_node.color_space = "COLOR" if map_name == "baseColor" or map_name == "diffuse" else "NONE" if map_name == "opacity": mat.blend_method = 'BLEND' if map_name == "height": mat.cycles.displacement_method = 'BOTH' if not front: # In case just the backside texture was chosen front = back back = {} def setup(name, node): if __class__.input_tr.get(name): links.new(node.outputs["Color"], principled.inputs[__class__.input_tr[name]]) else: if name == "glossiness": invert_node = nodes.new(type="ShaderNodeInvert") links.new(node.outputs["Color"], invert_node.inputs["Color"]) links.new(invert_node.outputs["Color"], principled.inputs["Roughness"]) if name == "diffuse": if not principled.inputs["Base Color"].is_linked: links.new(node.outputs["Color"], principled.inputs["Base Color"]) elif name == "height": displacement_node = nodes.new(type="ShaderNodeDisplacement") displacement_node.inputs[2].default_value = .2 links.new(node.outputs["Color"], displacement_node.inputs["Height"]) links.new(displacement_node.outputs["Displacement"], mat_output.inputs[2]) elif name == "normal": normal_node = nodes.new(type="ShaderNodeNormalMap") links.new(node.outputs["Color"], normal_node.inputs["Color"]) links.new(normal_node.outputs["Normal"], principled.inputs["Normal"]) elif name == "normalInvertedY": normal_node = nodes.new(type="ShaderNodeNormalMap") separate_node = nodes.new(type="ShaderNodeSeparateRGB") combine_node = nodes.new(type="ShaderNodeCombineRGB") math_node = nodes.new(type="ShaderNodeMath") math_node.operation = "MULTIPLY_ADD" math_node.inputs[1].default_value = -1 math_node.inputs[2].default_value = 1 links.new(node.outputs["Color"], separate_node.inputs["Image"]) links.new(separate_node.outputs["R"], combine_node.inputs[0]) links.new(separate_node.outputs["G"], math_node.inputs[0]) links.new(math_node.outputs["Value"], combine_node.inputs[1]) links.new(separate_node.outputs["B"], combine_node.inputs[2]) links.new(combine_node.outputs["Image"], normal_node.inputs["Color"]) links.new(normal_node.outputs["Normal"], principled.inputs["Normal"]) if not back: # If there is no item in the back dictionary [setup(name, node) for name, node in front.items()] else: geometry_node = nodes.new("ShaderNodeNewGeometry") def pre_setup(name, front, back, mix): links.new(geometry_node.outputs["Backfacing"], mix.inputs[0]) links.new(front.outputs["Color"], mix.inputs[1]) links.new(back.outputs["Color"], mix.inputs[2]) setup(name, mix) for name, node in front.items(): if back.get(name): pre_setup(name, node, back[name], nodes.new(type="ShaderNodeMixRGB")) autoAlignNodes(mat_output) return mat