Blenderのスクリプト
過去の関連エントリ
・2012-12-28 Blenderでレンダリング
・2012-12-30 Blenderで物理シミュレーション
Freeの3DCGソフトウェア「Blender」で Python スクリプトを使う練習をしてみた。
Python 自体、これまで一度も使ったことがなかったので、まさにゼロからの挑戦。
次のWebページが非常に参考になった。
■ 情報処理 I - 第15回 : Python で Blender を制御する
http://www.wakayama-u.ac.jp/~tokoi/lecture/shori1/15.html
上記のWebページに記載の通りの手順で、スクリプトによるモデリングからレンダリング、ファイル出力まで一通りを学ぶことができる。
そのまま同じのでは面白くないので、少しだけ手を加えて、次のようなものを作ってみた。
手作業ではとてもやってられないような形状をループ処理と数値計算で、パッと形にできてしまうのは楽しい。
あまり参考にならないと思うけど、上記のスクリプト。
import bpy import random import math import mathutils for item in bpy.context.scene.objects: if item.type == 'MESH': bpy.context.scene.objects.unlink(item) for item in bpy.data.objects: if item.type == 'MESH': bpy.data.objects.remove(item) for item in bpy.data.meshes: bpy.data.meshes.remove(item) for item in bpy.data.materials: bpy.data.materials.remove(item) random.seed() materials = [] for i in range(1, 7): materials.append(bpy.data.materials.new('Color' + str(i))) materials[-1].diffuse_color = ((i >> 2) & 1, (i >> 1) & 1, i & 1) for i in range(1000): bpy.ops.mesh.primitive_ico_sphere_add(subdivisions = 3, size = 0.5) bpy.context.object.data.materials.append(materials[i % 6]) obj = bpy.context.scene.objects.active obj.location = (0,0, 1) obj.scale = (0.05 , 0.05, 0.3) ax = random.uniform(0, 2*math.pi) ay = random.uniform(0, 2*math.pi) az = random.uniform(0, 2*math.pi) dd = random.uniform(0.5, 1) rotEuler = mathutils.Euler((ax, ay, az), 'XYZ') z_vec = mathutils.Vector((0.0, 0.0, dd)) z_vec.rotate(rotEuler) obj.rotation_euler = (ax, ay, az) obj.location = (z_vec.x, z_vec.y, z_vec.z) lamp = bpy.data.objects['Lamp'] lamp.location = (20, 10, 10) lamp.rotation_euler = (-0.2, -0.4, 0) lamp.data.falloff_type = 'INVERSE_LINEAR' lamp.data.shadow_method = 'NOSHADOW' world = bpy.data.worlds['World'] world.horizon_color = (0.8, 0.8, 0.8) world.zenith_color = (0.1, 0.1, 0.1) world.use_sky_blend = True camera = bpy.data.objects['Camera'] camera.location = (5, 0, 2.5) camera.rotation_euler = (0, 1, 0) camera.data.type = 'PERSP' camera.data.angle = 1 scene = bpy.context.scene scene.render.resolution_x = 1640 scene.render.resolution_y = 1360 scene.render.resolution_percentage = 100
以下、備忘録
・実行時のスクリプトのエラーはコンソールに出力される。コンソールの表示はメニューの[Window]-[Toggle System Console]で行う。
・Blender のAPI仕様書: http://www.blender.org/documentation/blender_python_api_2_66_6/contents.html
・Python入門: http://www.pythonweb.jp/tutorial/
・Python ライブラリリファレンス:数値と数学モジュール http://docs.python.jp/2.5/lib/numeric.html
Blender Master Class: A Hands-On Guide to Modeling, Sculpting, Materials, and Rendering
- 作者: Ben Simonds
- 出版社/メーカー: No Starch Press
- 発売日: 2013/02/15
- メディア: ペーパーバック
- この商品を含むブログ (1件) を見る
- 作者: Mark Lutz,夏目大
- 出版社/メーカー: オライリージャパン
- 発売日: 2009/02/26
- メディア: 大型本
- 購入: 12人 クリック: 423回
- この商品を含むブログ (133件) を見る
- 作者: 桑井博之
- 出版社/メーカー: カットシステム
- 発売日: 2012/07
- メディア: 単行本
- この商品を含むブログ (2件) を見る