Created
August 9, 2021 03:31
-
-
Save green224/5137db82c9ad349aebcfd5b4daaaffef to your computer and use it in GitHub Desktop.
Blenderで、選択ボーンの位置にちょうど頂点を持つメッシュを生成するコマンド
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
import math | |
from mathutils import Vector, Matrix | |
# 選択中のボーンから、対応する位置の頂点をもつメッシュを生成する | |
def execute(): | |
if bpy.context.mode != 'EDIT_ARMATURE': return | |
vertices = [] | |
vGroupNames = [] | |
obj = bpy.context.active_object | |
# エディットモードの状態で情報を更新 | |
obj.update_from_editmode() | |
poseBones = obj.pose.bones | |
for b in poseBones: | |
if b.bone.select: | |
vertices.append(b.tail) | |
vGroupNames.append(b.name) | |
# メッシュの作成 | |
mesh_obj = add_mesh_from_data('GEN_BoneMesh',vertices,[]) | |
mesh_obj.location = obj.location | |
mesh_obj.rotation_euler = obj.rotation_euler | |
mesh_obj.scale = obj.scale | |
# 作成元のボーンのウエイトを付加 | |
for i in range(len(vGroupNames)): | |
v_group = mesh_obj.vertex_groups.new(name=vGroupNames[i]) | |
v_index = [i] | |
v_group.add(v_index, 1.0, 'REPLACE') | |
# モデファイアの付加 | |
mesh_obj.modifiers.new("Armature", 'ARMATURE') | |
mesh_obj.modifiers["Armature"].object = obj | |
# データからメッシュを作成 | |
def add_mesh_from_data(name,vertices,faces): | |
mesh = bpy.data.meshes.new(name) | |
obj = bpy.data.objects.new(name, mesh) | |
bpy.context.collection.objects.link(obj) | |
mesh.from_pydata(vertices, [], faces) | |
mesh.update() | |
return( obj ) | |
execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment