Category: Blender

How to get material with Python API in Blender

GOAL

To get material and its properties with Python API in Blender

Environment

Blender 2.83
Windows 10

Methods

How to get material

Method1. Get active material

Active material is the material currently selected in the material slot. If you select a face, the active material changes to the material assigned the selected face.

The following is the script to get active material of an object named “Cube”.

import bpy
obj = bpy.data.objects["Cube"]
material = obj.active_material
print(material)
# output => <bpy_struct, Material("Material3")>

Method2. Get material slots and select one of them

import bpy
obj = bpy.data.objects["Cube"]
material_slots = obj.material_slots
for m in material_slots:
    material = m.material
    print("slot", m, "material", material)

# output is material slot and material
# slot <bpy_struct, MaterialSlot("Material")> material <bpy_struct, Material("Material")>
# slot <bpy_struct, MaterialSlot("Material2")> material <bpy_struct, Material("Material2")>
# slot <bpy_struct, MaterialSlot("Material3")> material <bpy_struct, Material("Material3")>

You can get a slot with index or key (material name). The order of slots follows the order on material slot UI.

import bpy
obj = bpy.data.objects["Cube"]
material_slots = obj.material_slots

material1 = material_slots[0].material
print(material1)
# output => <bpy_struct, Material("Material")>
material2 = material_slots["Material2"].material
print(material2)
# output => <bpy_struct, Material("Materia2")>

Method3. Get from bpy.data.materials

You can get existing material by its key (Material name).

import bpy
material = bpy.data.materials["Material2"]

How to get material properties

You can see the material properties in the material properties tab. The way to get the value of properties is different according to the renderer.

Renderer: workbench

You can get the value of properties by using attributes of Material class as below.

import bpy
material = bpy.data.materials["Material.001"]

color = material.diffuse_color
print(color[0], color[1], color[2], color[3])
# output => 0.20000000298023224 0.800000011920929 0.5 1.0

metallic = material.metallic
print(metallic)
# output => 0.30000001192092896

Other renderers

For example, Principled BSDF is one of the node based materials in Eevee.

Access the node_tree of the Material object. And get the node “Principled BSDF” and get inputs of the node.

You can see the list of inputs by material.node_tree.nodes[“Principled BSDF”].inputs

import bpy
#obj = bpy.data.objects["Cube"]
#material = obj.active_material
material = bpy.data.materials["Material3"]

inputs = material.node_tree.nodes["Principled BSDF"].inputs
for i in inputs:
    print(i)
# output
# <bpy_struct, NodeSocketColor("Base Color")>
# <bpy_struct, NodeSocketFloatFactor("Subsurface")>
# <bpy_struct, NodeSocketVector("Subsurface Radius")>
# <bpy_struct, NodeSocketColor("Subsurface Color")>
# <bpy_struct, NodeSocketFloatFactor("Metallic")>

You can get node sockets with index or key(input name). And the value of unconnected input can be get with attribute “default_value

import bpy
material = bpy.data.materials["Material3"]

inputs = material.node_tree.nodes["Principled BSDF"].inputs
color = inputs["Base Color"].default_value
print(color[0], color[1], color[2], color[3])
# output => 0.15000000596046448 0.800000011920929 0.10000000149011612 1.0

subsurface = inputs["Subsurface"].default_value
print(subsurface)
# output => 0.0

How to change material properties

Substitute value for the attribute.

import bpy
material = bpy.data.materials["Material3"]

inputs = material.node_tree.nodes["Principled BSDF"].inputs
color = inputs["Base Color"].default_value
color[0] = 1.0

The color will change.

You can use the python array as the value of color.

import bpy
material = bpy.data.materials["Material.001"]
material.diffuse_color = [1.0, 0.2, 0.2 ,1.0]

How To Assign Material To The Object in Blender

GOAL

To understand how to handle materials in Blender.

Environment

Blender 2.83
Windows 10

How to create new material

Select object and add material in the material properties tab.

The new material is added to the material slot

How to assign materials

Assign one material to the object

If you’d like to use one material for one object, select and remove other materials from the material slot.

The removed material can be added again.

Assign material to the faces

Select faces. Select material and press the “Assign” button.

The result

What is “Color” in Object Properties in Blender

GOAL

To understand how to use object color.

Environment

Blender 2.83
Windows 10

What is Object Properties?

The Properties shows and allows editing of many active data, including the active scene and object.

from Properties page in Blender2.91 Manual

You can change the attributes of the object from “Object Properties” tab. The color can be set in the “Visibility” menu.

How to display the object color

1. Change the object color

2. Change the renderer into “Workbench”

The Workbench Engine is a render engine optimized for fast rendering during modeling and animation preview.

from Introduction of workbench in Blender2.91 Manual

3. Set the color “Object”

You can select colors that the Workbench uses to render objects from single, Object, Material, Random, Vertex and Texture. See “Color” page of workbench in Blender2.91 Manual for details.

4. Rendering

Change the viewport shading “rendered”.

Rendered result

“python script failed check the message in the system console” in Blender

This is for beginners.

Error Detail

When I ran python script below in blender, the error message “python script failed check the message in the system console” is displayed.

import bpy.data
print(bpy.data.objects)
from Blender

Solution

Open the system console and check the message but where is the system console?

How to open the system console

Click Window > Toggle System Console

You can see the message on the generated window.

How to set keymap in Blender

GOAL

To add some functions into keymap in Blender. I added “Toggle Quad View” into the “Industry Compatible” keymap.

There is no shortcut key of “Toggle Quad View” in Industry Compatible keymap in contrast to Blender keymap.

Environment

Blender 2.83
Windows 10

Method

Open and right-click the menu to which you’d like to add shortcut key.

Click “Assign Shortcut” and press the key to assign.

Press the key and the shortcut key is registered.

What is “ActionZone” in Blender keymap

GOAL

To understand Blender keymap related to “ActionZone”.

Environment

Blender2.83
Windows10

How to see the current keymap settings

Edit > Preferences > Keymap

You can see actions and assigned keymaps. There are “ActionZone” in short cut keys.

What is ActionZone?

I couldn’t find any official documentation, but I think ActionZone means the each windows of editor.

Move the cursor to the upper right corner then the cross cursor will appear. When the cross cursor is displayed, you can handle “ActionZone”.

Example of usages

ActionZone (dragging cross cursor): split and join the area
Shift + ActionZone (dragging cross cursor) : Duplicate area into new window
Ctrl + ActionZone (dragging cross cursor) : Swap area

[Tips]How to Select a Face Loop in Blender -Element select-

I tried to select thin side of cube, but I couldn’t select by double-click.

What I want to do

GOAL

To summarize some ways to select elements in Blender.

Environment

Blender 2.8a

Keymap

Selection methods are different depending on keymap.

How to check and change keymap in Blender

Edit > Preference > Keymap

When Keymap is “Blender”, alt + click(left) is the key to select loop element.

When Keymap is “Industry Compatible”, double-click is the key to select loop element. I usually use this keymap.

The method to select loop elements

Vertices

Change the select mode into “vertex select” and double-click the edge on the vertex loop you’d like to select.

Example

Edges

Change the select mode into “edge select” and double-click the edge on the edge loop you’d like to select.

Example

Select another side.

Faces

Change the select mode into “face select” and double-click the side that is near the on the face loop you’d like to select.

Example

Select another side.

Other selection methods

Select one element

Click face, edge or vertex.

Multiple selection

Shift + click

Select continuous elements

Ctrl + click

Select All

Press A key

[Tips]How to use Image for Normal map in Blender

GOAL

To create normal map from RGB image.

Environment

Windows10
Blender 2.8a

Method

Create new Principled BSDF material.

Set “NormalMap” to the parameter “Normal”. And set “Image Texture” to the parameter “Calor”.

Open and select normal map to apply.

Open UV editor and adjust the UV Mapping.

You can use Nodes to change the scale of input image.

How to delete unnecessary materials in Blender.

GOAL

To delete unnecessary materials in Blender.

Environment

  • Windows 10
  • Blender 2.80

Method

In this scene there are 3 materials that has 3 colors, red, green and blue. The red material and green material are set to objects.

The blue material is not used now, but how to delete unnecessary material, the blue one?

Method 1. Find the material from “Blender File” and delete it.

Change the Display Mode of the Outliner into “Blender File”.

Open Material tab and right-click the material to delete.

Method 2. delete materials in “Orphan Data”.

Change the Display Mode of the Outliner into “Orphan Data”. The items displayed in the display mode “Orphan Data” are items that are not used or connected to any other items.

Open Material tab and right-click the material to delete.

Method 3. Reload the blender file.

Save the file and reopen it. All orphan data are automatically deleted when the file is reloaded.

[Tips]Use monochrome images as alpha map in Blender.

GOAL

To use monochrome images as alpha map in Blender, for example hair or eyelash.

hair texture for base color
hair texture for alpha map
rendered result

Method

Create new material for hair object. Select Principled BSDF as surface.

Set Image Texture to Base Color socket.

Open hair texture for base color.

Set Image Texture to Alpha socket.

Open hair texture for alpha map.

This procedure is insufficient because the alpha channel of the image is used for alpha map. Open Node Editor and reconnect nodes.

Use Color socket of the image.