Creating Materials using python is easy and it has a lot of answers on StackExchange.
Now what I want to ask is -
If I want that the material should only be added if the object (on which material to be added) should be mesh or text,
and material should not be added in case of curves or anything else and it should give a custom error message. Whenever the add test material button is pressed operator should only be executed if the object is mesh. Actually I want to specify in the script that this material to be added only if the object type is mesh. (Please have a look at the script)
import bpy
class TEST_MATERIAL_OT_add_material(bpy.types.Operator):
bl_idname = "test_material.add_material"
bl_label = "Add Test Material"
bl_description = "This button will add a material to your object"
@classmethod
def poll(cls, context):
ob = context.active_object
return ob is not None and ob.type=='MESH'
def execute(self, context):
ob = context.active_object
if ob is None:
self.report({'ERROR'}, "No active object")
return {"CANCELLED"}
if ob.type != 'MESH':
self.report({'ERROR'}, "Object is not a mesh")
return {"CANCELLED"}
self.report({'INFO'}, "Object is a mesh, operator can run")
self.create_material()
return {'FINISHED'}
def create_material(self):
# removes unwanted nodes
for node in tree.nodes:
tree.nodes.remove(node)
test_shader_mat = bpy.data.materials.new("TestMat")
mesh = bpy.context.object.data
mesh.materials.clear()
mesh.materials.append(test_shader_mat)
bpy.context.object.active_material.use_nodes = True
for mat in bpy.data.materials:
if "TestMat" in mat.name:
nodes = mat.node_tree.nodes
for node in nodes:
if node.type != 'OUTPUT_MATERIAL': # skip the material output node as we'll need it later
nodes.remove(node)
# Creating Node Group Test_Material
group = bpy.data.node_groups.new(type="ShaderNodeTree", name="Test_Material")
# Creating Group Input
group.inputs.new("NodeSocketColor", "Diffuse Color")
group.inputs.new("NodeSocketColor", "Glossy Color")
group.inputs.new("NodeSocketFloat", "Mix Factor")
group.inputs.new("NodeSocketFloat", "Glossyness")
input_node = group.nodes.new("NodeGroupInput")
input_node.location = (-800, 0)
# Creating Group Output Node
group.outputs.new("NodeSocketShader", "Diffuse Color")
group.outputs.new("NodeSocketShader", "Glossy Color")
group.outputs.new("NodeSocketShader", "Mix Output")
output_node = group.nodes.new("NodeGroupOutput")
output_node.location = (1500, 0)
# Creating Diffuse Node
diffuse_node = group.nodes.new(type='ShaderNodeBsdfDiffuse')
diffuse_node.location = (150, 100)
# Creating Glossy Node
glossy_node = group.nodes.new(type='ShaderNodeBsdfGlossy')
glossy_node.location = (300, 250)
# Creating Mix Shader Node
mix_shader_node = group.nodes.new(type='ShaderNodeMixShader')
mix_shader_node.location = (450, 100)
# Creating Links Between Nodes
group.links.new(diffuse_node.outputs["BSDF"], mix_shader_node.inputs[1])
group.links.new(glossy_node.outputs["BSDF"], mix_shader_node.inputs[2])
group.links.new(input_node.outputs["Diffuse Color"], diffuse_node.inputs[0])
group.links.new(input_node.outputs["Glossy Color"], glossy_node.inputs[0])
group.links.new(input_node.outputs["Mix Factor"], mix_shader_node.inputs[0])
group.links.new(input_node.outputs["Glossyness"], glossy_node.inputs[1])
group.links.new(output_node.inputs["Diffuse Color"], diffuse_node.outputs[0])
group.links.new(output_node.inputs["Glossy Color"], glossy_node.outputs[0])
group.links.new(output_node.inputs["Mix Output"], mix_shader_node.outputs[0])
# Putting Node Group to the node editor
tree = bpy.context.object.active_material.node_tree
group_node = tree.nodes.new("ShaderNodeGroup")
group_node.node_tree = group
group_node.location = (-40, 300)
group_node.use_custom_color = True
group_node.color = (1, 0.341, 0.034)
group_node.width = 250
shader_node_output_material_node = tree.nodes["Material Output"]
links = tree.links
links.new(group_node.outputs[0], shader_node_output_material_node.inputs[0])
class TEST_MATERIAL_PT_layout_panel(bpy.types.Panel):
bl_label = "Test Material Node"
bl_category = "Test Material"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
def draw(self, context):
layout = self.layout
layout.operator("test_material.add_material", icon='IMPORT')
classes = (TEST_MATERIAL_OT_add_material, TEST_MATERIAL_PT_layout_panel)
def register():
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)
if __name__ == "__main__":
register()
return context.object.type == 'MESH'
the operator will only be available if the active object is of 'MESH' type. I am not sure if that's what you want to do or you want to go through some objects and then only act on the ones that are meshes. $\endgroup$