MRが楽しい

MRやVRについて学習したことを書き残す

Blender2.7とBlender2.8でのPythonAPIの変更点

本日は Blender2.8 の技術調査枠です。
Blender2.7とBlender2.8でのPythonAPIの違いについて記事にします。
f:id:bluebirdofoz:20191128085709j:plain

Blender2.7とBlender2.8で異なるPythonAPI

Blender2.8ではBlender2.7から一部のPythonAPIが変更・削除されています。
APIに関する大きな変更点を以下に列挙します。

アクティブオブジェクトのアクセス

Blender2.8ではアクティブオブジェクトのアクセスにsceneではなくview_layerを利用します。

# 2.7
bpy.context.scene.objects
bpy.context.scene.objects.active

# 2.8
bpy.context.view_layer.objects
bpy.context.view_layer.objects.active

f:id:bluebirdofoz:20191128085719j:plain

オブジェクトリンクにはcollectionからアクセスします。

# 2.7
bpy.context.scene.objects.link(bpy.data.objects["Cube"])

# 2.8
bpy.context.scene.collection.objects.link(bpy.data.objects["Cube"])

f:id:bluebirdofoz:20191128085727j:plain

なお、各コレクションにアクセスする際はcollection.childrenを利用します。

# 2.7
# no exists

# 2.8
bpy.context.scene.collection.children.keys()
bpy.context.scene.collection.children["Collection"].objects

f:id:bluebirdofoz:20191128085736j:plain

選択状態の取得と変更

Blender2.8では選択状態はselect_get()から取得します。
選択状態を変更する際も、直接booleanを代入せず、select_set()で設定します。

# 2.7
bpy.context.object.select
bpy.context.object.select = True
bpy.context.object.select = False

# 2.8
bpy.context.object.select_get()
bpy.context.object.select_set(True)
bpy.context.object.select_set(False)

f:id:bluebirdofoz:20191128085745j:plain

表示状態の取得と変更

Blender2.8では表示状態はhide_get()から取得します。
表示状態を変更するは直接booleanを代入せず、hide_set()で設定します。
ただしオブジェクトが表示状態でもコレクションが非表示だとオブジェクトは見えません。

# 2.7
bpy.context.object.hide
bpy.context.object.hide = True

# 2.8
bpy.context.object.hide_get()
bpy.context.object.hide_set(True)

f:id:bluebirdofoz:20191128085754j:plain

また、Blender2.8では表示状態をvisible_get()からも取得できます。
これを使うとコレクションを考慮した表示状態の取得が行えます。
表示時 True となり、hideと正否が逆転する点に留意が必要です。

# 2.7
bpy.context.object.hide

# 2.8
not bpy.context.object.visible_get()

f:id:bluebirdofoz:20191128085803j:plain

レイヤー機能

Blender2.8ではレイヤーは削除されました。
レイヤーに相当する役割はコレクションが担います。

# 2.7
bpy.context.scene.layers

# 2.8
# no exists

f:id:bluebirdofoz:20191128090433j:plain

ユーザー設定

Blender2.8ではユーザー設定はpreferencesでアクセスします。

# 2.7
bpy.context.user_preferences

# 2.8
bpy.context.preferences

f:id:bluebirdofoz:20191128090443j:plain

APIでキーワード引数の必須化

Blender2.8 では一部のAPIでキーワード引数が必須になっています。

# 2.7
bpy.context.scene.frame_set(10, 0.25)

# 2.8
bpy.context.scene.frame_set(10, subframe=0.25)

f:id:bluebirdofoz:20191128090452j:plain