Author: Nako

[Error]pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool

Error Detail

When I install PySide2 with pip command, the error below occurred. There are 2 exceptions “socket.timeout: The read operation timed out” and “pip._vendor.urllib3.exceptions.ReadTimeoutError“.

<!–more–>

>pip install pyside2
Collecting pyside2
  Downloading PySide2-5.15.2-5.15.2-cp35.cp36.cp37.cp38.cp39-none-win_amd64.whl (136.3 MB)
     |██████████████████              | 75.3 MB 25 kB/s eta 0:40:29ERROR: Exception:
Traceback (most recent call last):
  File "c:\users\<user_name>\appdata\local\programs\python\python38\lib\site-packages\pip\_vendor\urllib3\response.py", line 437, in _error_catcher
    yield
.
.
. Omitted
.
.
  File "c:\users\<user_name>\appdata\local\programs\python\python38\lib\ssl.py", line 1099, in read
    return self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\users\<user_name>\appdata\local\programs\python\python38\lib\site-packages\pip\_internal\cli\base_command.py", line 228, in _main
    status = self.run(options, args)
.
.
. Omitted
.
.
  File "c:\users\<user_name>\appdata\local\programs\python\python38\lib\site-packages\pip\_vendor\urllib3\response.py", line 442, in _error_catcher
    raise ReadTimeoutError(self._pool, None, "Read timed out.")
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.

Environment

Windows 10
Pyhon 3.8.7
pip 20.2.3

Cause

When response couldn’t gotten in time, both of exceptions are occurred. The time is defined by environment variable “PIP_DEFAULT_TIMEOUT”.

The cause is just a poor connection in my case.

Solution

Change the value of PIP_DEFAULT_TIMEOUT (the value is an integer in seconds).
Reference : “Environment Variables” from pip documentation

Method 1. Pass the option to pip directly

pip --default-timeout=1200 install pyside2

Method 2. Set the default value

set PIP_DEFAULT_TIMEOUT=1200

How to see revision and restore previous article in WordPress

I mistakenly overwrote my article in WordPress. How can I recover them?

GOAL

To see revisions of the article and recover previous version in WordPress.

Environment

WordPress 5.6
Windows 10

Methods

Open the post and click “Revisions” in right sidebar.

Select the revision you’d like to recover and click “Restore This Revision” button.

What to do if “Restore This Revision” button is disable

“Restore This Revision” button couldn’t be pressed for some reasons.

You can copy the elements of previous version and paste it into the editor.

How To Run Python Script When Blender Starts

GOAL

To run Python script when Blender starts only once or every time.

Environment

Blender 2.83
Windows 10

Method

I created a simple Python script “Documents\blenderPython\test\spheres.py” to add spheres as below

import bpy
for i in range(3):
    bpy.ops.mesh.primitive_uv_sphere_add(radius=1, location=((i-1)*3, 0, 3))

Only once

Start Blender with command

Start blender in command prompt with an argument -P or –python.
Reference: Python Options in Blender 2.91 Manual

(more…)

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}