本日は Blender の技術調査枠です。
Blenderで利用可能なpythonスクリプトを作ります。
bluebirdofoz.hatenablog.com
blendファイルパスの取得
現在の blend ファイルのパスを取得できます。
未保存のプロジェクト上で実行すると、空文字が返ります。
get_blend_filepath.py
# osインポート import os # bpyインポート import bpy # 現在のblendファイルのディレクトリパスを取得する # 戻り値 blendファイルパス def get_blend_filepath(): # blendファイルのパスを取得 filepath = bpy.data.filepath return filepath # 関数実行例 blendpath = get_blend_filepath() print(type(blendpath)) if blendpath == '': print('Project not saved') else: # パスを表示する print(blendpath) # blendフォイルのディレクトリパスを取得 dir = os.path.dirname(blendpath) # ディレクトリを表示する print(dir)
オブジェクトの頂点を指定量だけ移動する
オブジェクトの頂点を指定量だけ移動します。
頂点を移動するため、メッシュオブジェクトの原点は変わりません。
bmesh をインポートしてメッシュ情報にアクセスできます。
move_position_vertex.py
# bpyインポート import bpy # bmeshインポート import bmesh # 指定オブジェクトの全頂点を指定量だけ移動する # 引数 arg_objectname:指定オブジェクト名 # arg_position:移動量 # 戻り値 def move_position_vertex(arg_objectname='Default', arg_position=(0,0,0)): # 指定オブジェクトを取得する selectob = bpy.context.scene.objects[arg_objectname] # 変更オブジェクトをアクティブに変更する bpy.context.scene.objects.active = selectob # 編集モードに移行する bpy.ops.object.mode_set(mode='EDIT', toggle=False) # 編集モードであることを確認する if selectob.mode == 'EDIT': # メッシュデータを取得する meshdata=bmesh.from_edit_mesh(selectob.data) # 頂点を走査する for v in meshdata.verts: # 移動量を加算する v.co.x += arg_position[0] v.co.y += arg_position[1] v.co.z += arg_position[2] # オブジェクトモードに戻す bpy.ops.object.mode_set(mode='OBJECT', toggle=False) return # 関数実行例 move_position_vertex(arg_objectname='Cube', arg_position=(0,0,3))