Blender Bone - Python

 

{{DZ_TITLE}}

最初の操作

  • BoneとPoseの取得
import bpy
bone = bpy.context.scene.objects['Armature'].data.bones['Bone']
pose = bpy.context.scene.objects['Armature'].pose.bones['Bone']
  • Bone位置、サイズの変更
import bpy
bone = bpy.context.scene.objects['Armature'].data.bones['Bone']
pose.location
pose.location = [1,2,3]
pose.scale = [2,2,2]
  • Boneの回転、X,Y,Z軸周り (相対)
import bpy
import math
bpy.ops.object.mode_set(mode='POSE')
pose = bpy.context.scene.objects['Armature'].pose.bones['Bone']
pose.rotation_mode='XYZ'
pose.rotation_euler.rotate_axis('X', math.radians(10))
pose.rotation_euler.rotate_axis('Y', math.radians(20))
pose.rotation_euler.rotate_axis('Z', math.radians(1))
pose.rotation_mode='QUATERNION'
bpy.ops.object.mode_set(mode='OBJECT')
  • Boneの回転、X,Y,Z軸周り (絶対)
import bpy
import math
bpy.ops.object.mode_set(mode='POSE')
pose = bpy.context.scene.objects['Armature'].pose.bones['Bone']
pose.rotation_mode='XYZ'
pose.rotation_euler[0] = math.radians(x)
pose.rotation_euler[1] = math.radians(y)
pose.rotation_euler[2] = math.radians(z)
pose.rotation_mode='QUATERNION'
bpy.ops.object.mode_set(mode='OBJECT')
  • Armatureの作成
import bpy
bpy.ops.object.armature_add(enter_editmode=False, location=(0, 0, 0))
  • Boneの作成
import bpy
def add_bone(armature_name):
    for obj in bpy.context.scene.objects:
        obj.select_set(False)
    obj=bpy.context.scene.objects[armature_name]
    bpy.context.view_layer.objects.active = obj
    obj.select_set(True)
    bpy.ops.object.mode_set(mode='EDIT')
    bpy.ops.armature.bone_primitive_add()
    bpy.ops.object.mode_set(mode='OBJECT')
  • Armatureの一覧を取得
    get_armature_name_lst()
    [‘Armature’]
import bpy
def get_armature_name_lst():
    armature_name_lst = []
    for obj in bpy.context.scene.objects:
        if obj.type == 'ARMATURE':
            armature_name_lst.append(obj.name)
    return armature_name_lst
  • Boneの一覧を取得
    get_bone_name_lst(‘Armature’)
    [‘Bone’]
import bpy
def get_bone_name_lst(armature_name):
    bone_name_lst = []
    obj = bpy.context.scene.objects[armature_name]
    if obj.type == 'ARMATURE':
        for bone in obj.data.bones:
            bone_name_lst.append(bone.name)
    return bone_name_lst
  • Bone位置とObjectを関連図ける connect_bone_and_obj_type_bone(‘Armature’,‘Bone.001’,‘Cube’)
import bpy
def connect_bone_and_obj_type_bone(armature_name,bone_name,objct_name):
    arma = bpy.data.objects[armature_name]
    obj  = bpy.data.objects[objct_name]

    bpy.ops.object.mode_set(mode='OBJECT')
    bpy.ops.object.select_all(action='DESELECT')
    arma.select_set(True)
    bpy.context.view_layer.objects.active = arma

    bpy.ops.object.mode_set(mode='EDIT')
    parent_bone = bone_name
    arma.data.edit_bones.active = arma.data.edit_bones[parent_bone]
    bpy.ops.object.mode_set(mode='OBJECT')

    bpy.ops.object.select_all(action='DESELECT')
    obj.select_set(True)
    arma.select_set(True)
    bpy.context.view_layer.objects.active = arma

    bpy.ops.object.parent_set(type='BONE', keep_transform=True)
  • Bone位置どうしをOffsetで固定する make_bone_offset(‘Armature’,‘Bone’,‘Bone.001’)
    make_bone_offset(‘Armature’,‘Bone’,[‘Bone.001’,‘Bone.002’,‘Bone.003’])
import bpy
def make_bone_offset(armature_name,parent_bone_name,child_bone_name):
    armature = bpy.data.objects[armature_name]

    bpy.ops.object.mode_set(mode='OBJECT')
    bpy.ops.object.select_all(action='DESELECT')
       
    bpy.context.view_layer.objects.active = armature
    bpy.ops.object.mode_set(mode='EDIT')

    if type(child_bone_name) is list or type(child_bone_name) is tuple :
        for child_bone_name1 in child_bone_name:
            armature.data.edit_bones.active = armature.data.edit_bones[child_bone_name1]
    else:
        armature.data.edit_bones.active = armature.data.edit_bones[child_bone_name]
    armature.data.edit_bones.active = armature.data.edit_bones[parent_bone_name]

    bpy.ops.armature.parent_set(type='OFFSET')

おすすめ記事

geojsonをfoliumで綺麗な地図を作る
geojsonをfoliumで綺麗な地図を作る
OBJファイルにPythonでテクスチャ貼りたいですか?
OBJファイルにPythonでテクスチャ貼りたいですか?
JavaScript
JavaScript
JQuery チート
JQuery チート
地理情報
地理情報
Raspberry pi Liteの非X Window環境でJackdを動かす
Raspberry pi Liteの非X Window環境でJackdを動かす
Supponsered

外部サイト
↓プログラムを学んでみたい場合、学習コースなどもおすすめです!

Comments

comments powered by Disqus