Month: August 2020

How to show/hide header and sidebar in Blender

This is just a Tips.

GOAL

To hide/show headers and side bars in Blender.

Environment

Blender 2.83
Windows 10

How to hide headers

Right-mouse-click the header and check buttons off.

How to display header

Click dropdown button at the boundary of the editor.

You can show Sidebar in the same way.

How to hide sidebar

Method1. Use View menu and check click View > Sidebar off.

You can hide Toolbar, Sidebar, Toon Settings in the same way.

Method2. drag and drop the edge of sidebar to the right

Where is user preference file of Blender

GOAL

To find preference file of Blender and view the contents of the file.

Environment

Blender 2.83 (I’m using 2.83 because it is LTS)
Windows 10

What is saved in preference files?

There are two areas where Blender’s defaults are stored:

The Preferences file stores keymap, add-ons theme and other options.

The Startup File stores the scene, Workspaces and interface which is displayed at startup and when loading a new file (File ‣ New).

from Blender2.91 manual
(more…)

How To Embed HTML Code In WordPress Editor

This is just a tips to embed HTML as a code, not as text.

GOAL

To embed HTML code in WordPress as below.

Example 1

<abbr> displays an abbreviation or acronym on mouse over. The tag is <abbr title=”HyperText Markup Language”>HTML</abbr>

This is an example of embedded HTML in WordPress.

(more…)

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

How To Get Attributes Of Object In Python

This is just a tips.

GOAL

To get the list of attributes of an object in Python

Environment

Python 3.7.4

Method

Use built-in function dir([object]) method as below.

dict1 = {"a":1, "b":2}
print(dir(dict1))
# output => ['__class__', '__contains__', '__delattr__', ... , 'setdefault', 'update', 'values']

dir() can takes not only variable but also any objects such as ‘type’ itself.

print(dir(dict))
# output is the same as print(dir(dict1)) above 

dir() can be used for user-defined class, too.

class User:
    number = 0
    def __init__(self, name, age=0):
        self.name = name
        self.age = 0
        self.addNum()
    def profile(self):
        print("Name:"+self.name, " Age:"+self.age)
    @classmethod
    def addNum(cls):
        cls.number += 1g

user1 = User("John", 20)
print(dir(user1))
# output => ['__class__', '__delattr__', '__dict__', ... , 'addNum', 'age', 'name', 'number', 'profile']

dir([object]) function return the list of only the object’s attributes. So the list returned by dir(User) includes class variables “number” but don’t include instance variables “name” and “age”.

 class User:
    number = 0
    def __init__(self, name, age=0):
        self.name = name
        self.age = age
        self.addNum()
    def isAdult(self):
        if 18 <= self.age:
            return True
        else:
            return False
    def profile(self):
        print("Name:"+self.name, " Age:"+self.age)
    @classmethod
    def addNum(cls):
        cls.number += 1

print(dir(User))
# output => ['__class__', '__delattr__', '__dict__', ... , 'addNum', 'number', 'profile']

If dir() takes no argument, it returns the list of names in the current local scope.

user1 = User("John", 20)
num1 = 10
dict1 = {"a":1, "b":2}
print(dir())
# output => ['User', '__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'dict1', 'num1', 'user1']

Appendix

Module “inspect” is also used for getting information of objects as below.

import inspect
user1 = User("John", 20)
print(inspect.getmembers(user1))
# getmembers returns the pairs of (attribute, its value)
# output => [('__class__', <class '__main__.User'>), ('__delattr__', <method-wrapper '__delattr__' of User object at 0x000002B8A1BBAEB0>), ('__dict__', {'name': 'John', 'age': 20}), ... , ('name', 'John'), ('number', 1), ('profile', <bound method User.profile of <__main__.User object at 0x000002B8A1BBAEB0>>)]

Function vars([object]) only return __dict__ attributes of the object that is the dict of (variables, value).

user1 = User("John", 20)
print(vars(user1))
# output => {'name': 'John', 'age': 20}

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