import bpy from mathutils import Vector from . import utils class GPTB_OT_create_follow_path_curve(bpy.types.Operator): bl_idname = "object.create_follow_path_curve" bl_label = "Create Follow Path Curve" bl_description = "Create curve and add follow path constraint\ \n(remove location offset from object if any)" bl_options = {"REGISTER", "UNDO"} @classmethod def poll(cls, context): return context.object def execute(self, context): ob = context.object # settings = context.scene.anim_cycle_settings bpy.ops.object.mode_set(mode='OBJECT', toggle=False) ## For bones # root_name = fn.get_root_name(context=context) # root = ob.pose.bones.get(root_name) # if not root: # self.report({'ERROR'}, f'posebone {root_name} not found in armature {ob.name} check addon preferences to change name') # return {"CANCELLED"} ## create curve at bone position # loc = ob.matrix_world @ root.matrix.to_translation() # root_axis_vec = fn.get_direction_vector_from_enum(settings.forward_axis) ## get real world direction of the root # world_forward = (root.matrix @ root_axis_vec) - root.matrix.to_translation() loc = ob.matrix_world.to_translation() ## X global # TODO: Set direction orientation in view space (UP, LEFT, RIGHT, DOWN) direction = Vector((1,0,0)) curve = utils.create_curve(location=loc, direction=direction.normalized() * 2, name='curve_path', context=context) utils.create_follow_path_constraint(ob, curve) ## reset location to remove offset ob.location = (0,0,0) # ob.keyframe_insert('location') ob.rotation_euler = (0,0,0) # ob.keyframe_insert('rotation_euler') # refresh evaluation so constraint shows up correctly bpy.context.scene.frame_set(bpy.context.scene.frame_current) return {"FINISHED"} classes = ( GPTB_OT_create_follow_path_curve, ) def register(): for cls in classes: bpy.utils.register_class(cls) def unregister(): for cls in reversed(classes): bpy.utils.unregister_class(cls)