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()
原文:https://www.cnblogs.com/qimenlang/p/14464680.html