1
$\begingroup$

I have the code:

import bpy
import math

scn = bpy.context.scene

class AutoReverse(bpy.types.Operator):
    bl_idname = "object.move_operator"
    bl_label = "Auto Reverse"

    def execute(self, context):
        # get keyframes of object list
        def get_keyframes(obj_list):
            keyframes = []
            for obj in obj_list:
                anim = obj.animation_data
                if anim is not None and anim.action is not None:
                    for fcu in anim.action.fcurves:
                        for keyframe in fcu.keyframe_points:
                            x, y = keyframe.co
                            if x not in keyframes:
                                keyframes.append((math.ceil(x)))
            return keyframes

        # get all selected objects
        selection = bpy.context.selected_objects

        # check if it has animation data
        if bpy.context.active_object.animation_data.action is not None:

            # get all frames with assigned keyframes
            keys = get_keyframes(selection)

            # print all keyframes
            print (keys)   

            # print first and last keyframe
            firstFrame = keys[0]
            lastFrame = keys[-1]

            # checking if it is baked
            if len(keys) < lastFrame:
                #baking the animation
                bpy.ops.nla.bake(frame_start=firstFrame, frame_end=lastFrame, step=1, bake_types={'OBJECT'})


            #scale it by -1 to reverse it
            old_type = bpy.context.area.type
            bpy.context.area.type = 'GRAPH_EDITOR'
            bpy.ops.graph.interpolation_type(type='CONSTANT')
            bpy.context.scene.frame_current = 1
            bpy.ops.transform.resize(value=(-1, 1, 1))
            bpy.context.area.type = old_type
            return {'FINISHED'}

        else:
             self.report({'ERROR'}, 'There is no animation')


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

if __name__ == "__main__":
    register()

I get the error

RuntimeError: class OBJECT_OT_add, function execute: incompatible return value , str(, Function.result expected a set, not a NoneType)

From testing I think it's from this line:

if bpy.context.active_object.animation_data.action is not None:

(BTW I know there is an almost identical question, but he fixes it himself and I can't find a similar error in mine)

I would like to know why it occurs (beyond just purely what the error tells me), how to fix it, or whether there is a suitable replacement to the line above to avoid the error alltogether, thank you.

$\endgroup$
1
$\begingroup$

Okay, I forgot that if you use self.report({'ERROR'}, 'There is no animation'), you must add return {'CANCELLED'}.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.