
【Blender】Script:グランドにスナップ、原点を底面に移動する
海外フォーラムで見かけたBlenderのスクリプトを紹介。
・オブジェクトをグランドにスナップさせる
・オブジェクトの原点を底面に移動させる
個人的にこのような操作を良く行うが、標準機能で行うには、手順が面倒くさい。使用用途としては、こういう単純なものが良いのだが、Blender 2.8+で適合するAdd-onが見つからなかったので調査していた案件。
<関連記事>
■スクリプトの使い方
タブのScriptをクリックし、Scriptモードに切り替える。
新規テキスト作成。
実行したいスクリプトをコピペする。
三角の再生ボタンをクリックして実行。

■Snap To Ground
オブジェクトの底面に合わせて、グラウンドにスナップさせるスクリプト。
原点の位置は変更しない。

import bpy
context = bpy.context
for obj in context.selected_objects:
mx = obj.matrix_world
minz = min((mx @ v.co)[2] for v in obj.data.vertices)
mx.translation.z -= minz
■Origin To Bottom
オブジェクトの原点を底面に移動するスクリプト。
元スクリプトは、オブジェクトの原点を底面に移動し、オブジェクトをワールド座標の原点に移動するスクリプト。最後の方のbpy.ops.object.location_clear(clear_delta=False)をコメントアウトにして、原点移動だけするようにしてみた。

import bpy
def CenterOrigin():
#Get active object
act_obj = bpy.context.active_object
#Get cursor
cursor = bpy.context.scene.cursor
#Get original cursor location
original_cursor_location = (cursor.location[0], cursor.location[1], cursor.location[2])
#Make sure origin is set to geometry for cursor z move
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY', center='BOUNDS')
#Set cursor location to object location
cursor.location = act_obj.location
#Get cursor z move
half_act_obj_z_dim = act_obj.dimensions[2] / 2
cursor_z_move = cursor.location[2] - half_act_obj_z_dim
#Move cursor to bottom of object
cursor.location[2] = cursor_z_move
#Set origin to cursor
bpy.ops.object.origin_set(type='ORIGIN_CURSOR', center='MEDIAN')
#Reset cursor back to original location
cursor.location = original_cursor_location
#Assuming you're wanting object center to grid
#bpy.ops.object.location_clear(clear_delta=False)
CenterOrigin()
■あとがき
今回紹介したスクリプトは、複数オブジェクト適用には、対応していない。
このスクリプトをAdd-on化できると理想なんだが・・・。
自分にそこまでのスキルはない。
ちなみにこれらの機能は、標準搭載してもいいんじゃないか。
あとで調べてみると、「Drop It」という無料Add-onを見つけた。使用感としては、原点の位置に関わらずグランドやオブジェクトにスナップできるのでとても便利。
操作方法は、マウスっボタン右クリックメニュー>Drop It を実行するだけ。(ショートカットキー:V)。
以上。