本日は Blender2.8 の技術調査枠です。
Blender2.7とBlender2.8でのPythonAPIの違いについて記事にします。
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
オブジェクトリンクにはcollectionからアクセスします。
# 2.7 bpy.context.scene.objects.link(bpy.data.objects["Cube"]) # 2.8 bpy.context.scene.collection.objects.link(bpy.data.objects["Cube"])
なお、各コレクションにアクセスする際はcollection.childrenを利用します。
# 2.7 # no exists # 2.8 bpy.context.scene.collection.children.keys() bpy.context.scene.collection.children["Collection"].objects
選択状態の取得と変更
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)
表示状態の取得と変更
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)
また、Blender2.8では表示状態をvisible_get()からも取得できます。
これを使うとコレクションを考慮した表示状態の取得が行えます。
表示時 True となり、hideと正否が逆転する点に留意が必要です。
# 2.7 bpy.context.object.hide # 2.8 not bpy.context.object.visible_get()
レイヤー機能
Blender2.8ではレイヤーは削除されました。
レイヤーに相当する役割はコレクションが担います。
# 2.7 bpy.context.scene.layers # 2.8 # no exists
ユーザー設定
Blender2.8ではユーザー設定はpreferencesでアクセスします。
# 2.7 bpy.context.user_preferences # 2.8 bpy.context.preferences