首页 > 编程语言 > 详细

Blender Python Scripts Tips

时间:2021-03-01 18:10:14      阅读:34      评论:0      收藏:0      [点我收藏+]

1.选中对象:bpy.data.objects[‘obj_name‘].select_set(True)

2.激活对象:bpy.context.view_layer.objects.active = object

 

Demo:实现 LookAt 功能

bl_info = {
    "name": "Create Look At",
    "author": "LJX",
    "version": (1, 0),
    "blender": (2, 80, 0),
    "location": "F3",
    "description": "create look at rigs",
    "warning": "",
    "doc_url": "",
    "category": "Object",
}


import bpy
from mathutils import Vector
class CreateLookAtController(bpy.types.Operator):
    bl_idname = "object.create_look_at"
    bl_label = "Create LookAt Controller"

    def execute(self, context):
        # create cube
        bpy.ops.mesh.primitive_cube_add()
        bl_target_name=bpy.context.object.name
        
        # create first skinning bone
        bl_first_bone_location=bpy.context.object.location
        bpy.ops.object.armature_add(location=bl_first_bone_location)
        bpy.context.object.rotation_euler=(-1.5708, 0.0, 0.0) # why 90 is 1.5708
        bpy.context.object.show_in_front=True
        bl_first_bone_obj=bpy.context.object
        bl_first_bone_name=bl_first_bone_obj.name
        
        # auto create weight
        bpy.data.objects[bl_target_name].select_set(True)
        bpy.data.objects[bl_first_bone_name].select_set(True) 
        bpy.ops.object.parent_set(type=ARMATURE_AUTO)
        
        # create second constraint bone
        bl_second_bone_location=bl_first_bone_location+Vector((0,5,0))
        bpy.ops.object.armature_add(location=bl_second_bone_location) # how to set relative location
        bpy.context.object.rotation_euler=(-1.5708, 0.0, 0.0) # why 90 is 1.5708
        bpy.context.object.show_in_front=True
        bl_second_bone_obj=bpy.context.object        
        
        # create lookat constraint
        bpy.context.view_layer.objects.active = bl_first_bone_obj  #activate first bone
        bpy.ops.object.posemode_toggle()
        bpy.ops.pose.constraint_add(type=DAMPED_TRACK)
        bpy.context.object.pose.bones["Bone"].constraints["Damped Track"].target = bl_second_bone_obj
        bpy.context.object.pose.bones["Bone"].constraints["Damped Track"].subtarget = "Bone"
        bpy.ops.object.posemode_toggle()

        return {FINISHED}        

def register():
    bpy.utils.register_class(CreateLookAtController)

def unregister():
    bpy.utils.unregister_class(CreateLookAtController)

if __name__ == "__main__":
    register()

 

Blender Python Scripts Tips

原文:https://www.cnblogs.com/qimenlang/p/14464680.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!