Skip to content

Instantly share code, notes, and snippets.

@mio3io
Last active June 5, 2024 08:19
Show Gist options
  • Save mio3io/94caffe4235959c9a9372134a79ea67b to your computer and use it in GitHub Desktop.
Save mio3io/94caffe4235959c9a9372134a79ea67b to your computer and use it in GitHub Desktop.
Blenderでマルチレゾとか使ってて左右対称が崩れたときリカバリーするスクリプト デフォルトは向かって右側が実態でサフィックス'.R', '.L'
import bpy
def select_vertices(obj, op):
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode='OBJECT')
if op == '+X':
for v in obj.data.vertices:
if v.co.x <= 0:
v.select = True
else:
for v in obj.data.vertices:
if v.co.x >= 0:
v.select = True
bpy.ops.object.mode_set(mode='EDIT')
def mirror_weights(obj, t1_suffix, t2_suffix):
vgroups = obj.vertex_groups
name_pairs = [(name, name[:-len(t1_suffix)] + t2_suffix) for name in vgroups.keys() if name.endswith(t1_suffix)]
for from_name, to_name in name_pairs:
from_group = vgroups.get(from_name)
to_group = vgroups.get(to_name)
if from_group and to_group:
bpy.ops.object.vertex_group_set_active(group=from_group.name)
bpy.ops.object.vertex_group_mirror(use_topology=False)
def symmetrize(obj, op):
bpy.context.object.active_shape_key_index = 0
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
if op == '+X':
bpy.ops.mesh.symmetrize(direction='POSITIVE_X')
else:
bpy.ops.mesh.symmetrize(direction='NEGATIVE_X')
active_obj = bpy.context.active_object
symmetrize(active_obj, '+X')
select_vertices(active_obj, '+X')
mirror_weights(active_obj, '.R', '.L')
# hidarigawa
#symmetrize(active_obj, '-X')
#select_vertices(active_obj, '-X')
#mirror_weights(active_obj, '.L', '.R')
bpy.ops.object.mode_set(mode='OBJECT')
@mio3io
Copy link
Author

mio3io commented Jan 16, 2024

自分用に使ってたものでオプション用のUIがありません。
blendファイルごとにスクリプトを作る必要があります(一度作成すればそのblendファイルでは使えます)
新規スクリプトを作ってコピペして▶の実行ボタンで使用できます

なお、動画はミラー適用していますがメッシュを対称生成するため事前のミラー適用は不要です。
X対称用のミラーモディファイアがある場合は削除してください。

@mio3io
Copy link
Author

mio3io commented Jan 16, 2024

py.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment