機械学習基礎理論独習

誤りがあればご指摘いただけると幸いです。数式が整うまで少し時間かかります。リンクフリーです。

勉強ログです。リンクフリーです
目次へ戻る

【Blender】Weight Paint 作業時に便利な Script 集

はじめに

Vertex Weight の編集中の時に Weight Paint と Pose Mode を遷移させますが
Weight Paint へ遷移するときは、先にArmature を選択したうえで Mesh を選択しなければなりません。
Pose Mode へ遷移するときは、Armature を選択しなければなりません。
しかも選択の切り替えは Object Mode でやる必要があります。
これだけでもかなり面倒ですよね。
これらの面倒を解消するための Script を置いておきます。

Script 実行に便利な Add-on

Script の実行は Script To Button という Add-on を使えば Sidebar に表示される Button から実行可能です。

Object Mode へ遷移する Script

import bpy

# to object mode
bpy.ops.object.mode_set(mode='OBJECT')

print('to object mode')

Edit Mode (Armature) へ遷移する Script

import bpy
# cache
armature_object = bpy.data.objects["Armature"]
mesh_object = bpy.data.objects["GEO-body_male_realistic"]

# to edit mode (armature)
bpy.ops.object.mode_set(mode='OBJECT')  # Switch to Object Mode temporarily
mesh_object.select_set(False)
bpy.context.view_layer.objects.active = armature_object
armature_object.select_set(True)
bpy.ops.object.mode_set(mode='EDIT')

print('to edit mode (armature)')

Edit Mode (Mesh) へ遷移する Script

import bpy
# cache
armature_object = bpy.data.objects["Armature"]
mesh_object = bpy.data.objects["GEO-body_male_realistic"]

# to edit mode (mesh)
bpy.ops.object.mode_set(mode='OBJECT')  # Switch to Object Mode temporarily
armature_object.select_set(True)
bpy.context.view_layer.objects.active = mesh_object
mesh_object.select_set(True)
bpy.ops.object.mode_set(mode='EDIT')

print('to edit mode (mesh)')

Weight Paint へ遷移する Script

import bpy
# cache
armature_object = bpy.data.objects["Armature"]
mesh_object = bpy.data.objects["GEO-body_male_realistic"]

# to weight paint
bpy.ops.object.mode_set(mode='OBJECT')  # Switch to Object Mode temporarily
armature_object.select_set(True)
bpy.context.view_layer.objects.active = mesh_object
mesh_object.select_set(True)
bpy.ops.object.mode_set(mode='WEIGHT_PAINT')

print('to weight paint')

Pose Mode へ遷移する Script

Pose Mode へ遷移後、Sidebar で Item を選択しています。

import bpy
# cache
armature_object = bpy.data.objects["Armature"]
mesh_object = bpy.data.objects["GEO-body_male_realistic"]

# to pose mode
bpy.ops.object.mode_set(mode='OBJECT')  # Switch to Object Mode temporarily
bpy.context.view_layer.objects.active = armature_object
armature_object.select_set(True)
mesh_object.select_set(False)
bpy.ops.object.mode_set(mode='POSE')

# select 'Item'
# https://blenderartists.org/t/is-it-impossible-to-select-a-tab-in-a-side-panel-through-script/1522596/2
areas = bpy.context.screen.areas
area = [a for a in areas if a.type == 'VIEW_3D'][0]
with bpy.context.temp_override(area=area):
    area.regions[5].active_panel_category = 'Item' # 'Item', 'Tool', 'View' or any addon name
    bpy.context.object.hide_render = bpy.context.object.hide_render

print('to pose mode')

Weight Paint で Paint の Weight の倍率を変更する Script

ratio の値を変更してお使いください。

import bpy

if bpy.context.mode == "PAINT_WEIGHT":
    ratio = 1.0
    bpy.context.scene.tool_settings.unified_paint_settings.weight *= ratio
    bpy.ops.wm.tool_set_by_id(name="builtin.brush")
else:
    print('not PAINT_WEIGHT')

Pose Mode で 全ての Bone の回転角度をクリアする Script

import bpy
armature_object = bpy.data.objects['Armature']
bone_name = 'Clavicle'
if bone_name in armature_object.pose.bones and bpy.context.mode == "POSE":
    
    # deselect all 
    bpy.ops.pose.select_all(action='DESELECT')

    # the desired Bone
    target_bone = armature_object.pose.bones[bone_name].bone
    # set as active 
    bpy.context.object.data.bones.active = target_bone
    # select in viewport
    target_bone.select = True

    # select 'Item'
    # https://blenderartists.org/t/is-it-impossible-to-select-a-tab-in-a-side-panel-through-script/1522596/2
    areas = bpy.context.screen.areas
    area = [a for a in areas if a.type == 'VIEW_3D'][0]
    with bpy.context.temp_override(area=area):
        area.regions[5].active_panel_category = 'Item' # 'Item', 'Tool', 'View' or any addon name
        bpy.context.object.hide_render = bpy.context.object.hide_render
else:
    print('bone_name is not exist or not POSE')

Pose Mode で 特定の Bone を選択する Script

import bpy
armature_object = bpy.data.objects['Armature']
bone_name = 'Clavicle'
if bone_name in armature_object.pose.bones and bpy.context.mode == "POSE":
    
    # deselect all 
    bpy.ops.pose.select_all(action='DESELECT')

    # the desired Bone
    target_bone = armature_object.pose.bones[bone_name].bone
    # set as active 
    bpy.context.object.data.bones.active = target_bone
    # select in viewport
    target_bone.select = True

    # select 'Item'
    # https://blenderartists.org/t/is-it-impossible-to-select-a-tab-in-a-side-panel-through-script/1522596/2
    areas = bpy.context.screen.areas
    area = [a for a in areas if a.type == 'VIEW_3D'][0]
    with bpy.context.temp_override(area=area):
        area.regions[5].active_panel_category = 'Item' # 'Item', 'Tool', 'View' or any addon name
        bpy.context.object.hide_render = bpy.context.object.hide_render
else:
    print('bone_name is not exist or not POSE')
目次へ戻る