以前書いたPythonプログラムをBlender 2.8に対応させました。
シミュレーションパターンは3つあって、2つの剛体シミュレーションが動きます。柔体シミュレーションは設定がわからず動きませんでした。
環境
- macOS Mojave 10.14.3
- Blender 2.8 beta(2019年3月22日にダウンロードしたもの)
設定
エイリアスの設定
Terminalから簡単にBlenderが起動できるようエイリアスを設定します。
$ alias blender='/Applications/blender/blender.app/contents/MacOS/blender' $ source .bash_profile
Pythonの起動方法
ソースコードがあるディレクトリに移動してblenderコマンドを叩きます。
$ cd python $ blender --python hoge.py $ blender --background --python hoge.py (バックグラウンドレンダリング) オプションは短縮できて、以下のようにもできます。詳しくは「blender --help」で確認してください。 $ blender -b -P hogehoge.py
物理シミュレーション
ソースコード
"""
Blenderの物理シミュレーション 2.8対応版(2019.3.22 Build)
Eeveeによるレンダリングで、AVI RAW形式のファイルが生成される
"""
import bpy
import math
import os
def delete_all():
"""
デフォルトで存在しているCube/Camera/Lightを削除
"""
# すべてのシーン、オブジェクトを表示
print(bpy.data.scenes.keys())
for obj in bpy.data.objects:
print(obj.name)
# すべてのオブジェクトを削除
bpy.data.meshes.remove(bpy.data.meshes["Cube"])
bpy.data.cameras.remove(bpy.data.cameras["Camera"])
bpy.data.lights.remove(bpy.data.lights["Light"])
def create_scene():
"""
シーンを作成
"""
# Cubeを1000個作成
cubeCount = 10
for axis_x in range(cubeCount):
for axis_y in range(cubeCount):
for axis_z in range(cubeCount):
# 以下に3パターンの設定を用意
# 別パターンを試すときはコメントアウト
# その際、start_simulation内の "physical.avi" や "physical.blend" は任意で変更するとよし
# パターン1
bpy.ops.mesh.primitive_cube_add(
location=(axis_x*2, axis_y*2, axis_z*2))
bpy.ops.rigidbody.object_add()
# パターン2
# bpy.ops.mesh.primitive_cube_add(
# location=(axis_x*2, axis_y*2, axis_z*2), rotation=(10, 10, 0))
# bpy.ops.rigidbody.object_add()
# パターン3
# todo: 設定不備により正しく動作しない
# bpy.ops.mesh.primitive_cube_add(
# location=(axis_x*2, axis_y*2, axis_z*2))
# bpy.ops.rigidbody.object_add()
# bpy.ops.object.modifier_add(type='SOFT_BODY')
# 平面を2つ作成
bpy.ops.mesh.primitive_plane_add(
location=(cubeCount-2, cubeCount-2, -10))
bpy.ops.rigidbody.object_add(type="PASSIVE")
bpy.data.objects["Plane"].scale = (cubeCount+5, cubeCount+5, 1)
bpy.ops.mesh.primitive_plane_add(
location=(cubeCount-2, cubeCount-2, -30))
bpy.ops.rigidbody.object_add(type="PASSIVE")
bpy.data.objects["Plane.001"].scale = (cubeCount+10, cubeCount+10, 1)
# カメラを作成
bpy.ops.object.camera_add(view_align=True, location=(
cubeCount+30, cubeCount+30, cubeCount+10), rotation=(math.pi/3, 0, math.pi*3/4))
bpy.data.cameras["Camera"].lens = 24
bpy.data.scenes["Scene"].camera = bpy.data.objects["Camera"]
# 照明を作成
bpy.ops.object.light_add(type="SUN", location=(0, 0, cubeCount+20))
def start_simulation():
"""
物理シミュレーション(現状は250フレームまでBake可能)
"""
bpy.data.scenes["Scene"].rigidbody_world.time_scale = 2
bpy.ops.ptcache.bake_all()
# 動画を作成
bpy.context.scene.render.resolution_x = 800
bpy.context.scene.render.resolution_y = 600
bpy.context.scene.render.resolution_percentage = 100
bpy.context.scene.render.image_settings.file_format = "AVI_RAW"
bpy.data.scenes["Scene"].render.filepath = "physical1.avi"
bpy.context.scene.frame_start = 0
bpy.context.scene.frame_end = 250
bpy.ops.render.render(animation=True)
# blendファイルを保存
save_path = os.path.abspath(os.path.dirname(__file__))
bpy.path.relpath(save_path)
bpy.ops.wm.save_as_mainfile(
filepath="physical1.blend", relative_remap=True)
if __name__ == '__main__':
delete_all()
create_scene()
start_simulation()
delete_all関数には以前、「bpy.ops.object.delete()」と書くだけで全オブジェクトが削除されたのですが、2.8ではうまく動かなくてひとつひとつ削除するようにしました。
アニメーションフレーム数を250(デフォルト) → 300に増やたかったのに、BakeのSimulation Endにはプログラムからアクセスできないっぽい。

このままだと50フレームほどシミュレーションできないので、デフォルトの250のままとしました。
動画
Eeveeでレンダリングしているので速いです。
おわりに
Blender 2.8に対応させようと思ったのは、過去記事が他のブログでシェアされていたから。
その方はおそらくv2.79をお使いだったと思いますが、エラーが出て動かなかったご様子。謎。
僕はもうv2.8を使っていて、v2.79で動くようコードを直すテンションはないので(笑)、この際だからv2.8で動かしちゃえ、というわけです。
たまたまとはいえ、僕の記事を参考にしてくれていてびっくりですよ。嬉しいですね〜。

