Blender modal operator cannot insert keyframe and move object



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








2















In Blender I am using the modal operator template to move an object and record its position as a keyframe.



I am doing something like this:



import bpy
from bpy.props import IntProperty, FloatProperty

class ModalOperator(bpy.types.Operator):
"""Move an object with the mouse, example"""
bl_idname = "object.modal_operator"
bl_label = "Simple Modal Operator"

first_mouse_x = IntProperty()
first_value = FloatProperty()
current_frame = 1
endframe = bpy.data.scenes["Scene"].frame_end

def modal(self, context, event):
if event.type == 'MOUSEMOVE':
if self.current_frame < self.endframe:
delta = self.first_mouse_x - event.mouse_x
context.object.location.x = self.first_value + delta * 0.01
context.scene.frame_set(self.current_frame)
bpy.ops.anim.keyframe_insert_menu(type="Rotation")
bpy.ops.anim.keyframe_insert_menu(type="Location")
self.current_frame+=1

elif event.type == 'LEFTMOUSE':
return 'FINISHED'

elif event.type in 'RIGHTMOUSE', 'ESC':
context.object.location.x = self.first_value
return 'CANCELLED'

return 'RUNNING_MODAL'


Happens that before it inserts all keyframes and only after you can move the cube with the mouse, for instance. I would like to move the cube and "record" its movement at the same time. Is there a solution for that?










share|improve this question




























    2















    In Blender I am using the modal operator template to move an object and record its position as a keyframe.



    I am doing something like this:



    import bpy
    from bpy.props import IntProperty, FloatProperty

    class ModalOperator(bpy.types.Operator):
    """Move an object with the mouse, example"""
    bl_idname = "object.modal_operator"
    bl_label = "Simple Modal Operator"

    first_mouse_x = IntProperty()
    first_value = FloatProperty()
    current_frame = 1
    endframe = bpy.data.scenes["Scene"].frame_end

    def modal(self, context, event):
    if event.type == 'MOUSEMOVE':
    if self.current_frame < self.endframe:
    delta = self.first_mouse_x - event.mouse_x
    context.object.location.x = self.first_value + delta * 0.01
    context.scene.frame_set(self.current_frame)
    bpy.ops.anim.keyframe_insert_menu(type="Rotation")
    bpy.ops.anim.keyframe_insert_menu(type="Location")
    self.current_frame+=1

    elif event.type == 'LEFTMOUSE':
    return 'FINISHED'

    elif event.type in 'RIGHTMOUSE', 'ESC':
    context.object.location.x = self.first_value
    return 'CANCELLED'

    return 'RUNNING_MODAL'


    Happens that before it inserts all keyframes and only after you can move the cube with the mouse, for instance. I would like to move the cube and "record" its movement at the same time. Is there a solution for that?










    share|improve this question
























      2












      2








      2








      In Blender I am using the modal operator template to move an object and record its position as a keyframe.



      I am doing something like this:



      import bpy
      from bpy.props import IntProperty, FloatProperty

      class ModalOperator(bpy.types.Operator):
      """Move an object with the mouse, example"""
      bl_idname = "object.modal_operator"
      bl_label = "Simple Modal Operator"

      first_mouse_x = IntProperty()
      first_value = FloatProperty()
      current_frame = 1
      endframe = bpy.data.scenes["Scene"].frame_end

      def modal(self, context, event):
      if event.type == 'MOUSEMOVE':
      if self.current_frame < self.endframe:
      delta = self.first_mouse_x - event.mouse_x
      context.object.location.x = self.first_value + delta * 0.01
      context.scene.frame_set(self.current_frame)
      bpy.ops.anim.keyframe_insert_menu(type="Rotation")
      bpy.ops.anim.keyframe_insert_menu(type="Location")
      self.current_frame+=1

      elif event.type == 'LEFTMOUSE':
      return 'FINISHED'

      elif event.type in 'RIGHTMOUSE', 'ESC':
      context.object.location.x = self.first_value
      return 'CANCELLED'

      return 'RUNNING_MODAL'


      Happens that before it inserts all keyframes and only after you can move the cube with the mouse, for instance. I would like to move the cube and "record" its movement at the same time. Is there a solution for that?










      share|improve this question














      In Blender I am using the modal operator template to move an object and record its position as a keyframe.



      I am doing something like this:



      import bpy
      from bpy.props import IntProperty, FloatProperty

      class ModalOperator(bpy.types.Operator):
      """Move an object with the mouse, example"""
      bl_idname = "object.modal_operator"
      bl_label = "Simple Modal Operator"

      first_mouse_x = IntProperty()
      first_value = FloatProperty()
      current_frame = 1
      endframe = bpy.data.scenes["Scene"].frame_end

      def modal(self, context, event):
      if event.type == 'MOUSEMOVE':
      if self.current_frame < self.endframe:
      delta = self.first_mouse_x - event.mouse_x
      context.object.location.x = self.first_value + delta * 0.01
      context.scene.frame_set(self.current_frame)
      bpy.ops.anim.keyframe_insert_menu(type="Rotation")
      bpy.ops.anim.keyframe_insert_menu(type="Location")
      self.current_frame+=1

      elif event.type == 'LEFTMOUSE':
      return 'FINISHED'

      elif event.type in 'RIGHTMOUSE', 'ESC':
      context.object.location.x = self.first_value
      return 'CANCELLED'

      return 'RUNNING_MODAL'


      Happens that before it inserts all keyframes and only after you can move the cube with the mouse, for instance. I would like to move the cube and "record" its movement at the same time. Is there a solution for that?







      python modal-dialog operator-keyword blender






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 16:40









      Paolo GambardellaPaolo Gambardella

      112




      112






















          2 Answers
          2






          active

          oldest

          votes


















          1














          The easy solution would be to enable blenders Auto Keyframing.



          If you still want to get your operator working, I expect you will need to not call other operators and work with the data directly, particularly within a modal operator.



          context.object.keyframe_insert('location')
          context.object.keyframe_insert('rotation_euler')





          share|improve this answer























          • Hello, thank you very much for your answer. Right now I am using blender 2.79. If I enable auto keyframing, or also with the solution you offered, I have the exact same problem. In fact, auto-keyframing works if, for instance, I grap the object with G. It doesn't if I move it by script, don't know why.

            – Paolo Gambardella
            Nov 16 '18 at 13:47












          • @PaoloGambardella I don't think you can have two modal operators running at the same time and both reacting to user input. With auto keyframing you can start playback and edit your data while frames are rolling, no script required at all.

            – keltar
            Nov 16 '18 at 14:02











          • I understand what you are saying but if I play (ALT+A) with autokeyframing on and after I activate the script, it doesn't work. And it doesn't also if I first run the script and then play.

            – Paolo Gambardella
            Nov 16 '18 at 14:08











          • @PaoloGambardella that's two separate solutions that you're mixing together. Please don't. Auto keyframing presumably does what you initially intended, there is no need for extra operator here. If that's not the case - I see two solutions, either emulating other operators inside yours (i.e. reacting to standard hotkeys, performing similar actions, etc.) or setting redraw callback and attempting to intercept things here; both will require quite large code and presumably will interfere with 'usual' workflow.

            – keltar
            Nov 16 '18 at 16:06











          • @keltar thank you for your answers. I am not mixing the two approaches, in fact I tried also to remove the calls to keyframe_insert, etc and still I cannot record anything. Now I am simply moving my object and nothing happens, even with autokeyframe insertion activated. Instead if I move my object with the Grab functionality, it works. It's like the modal operator created by me blocking the other functionality.

            – Paolo Gambardella
            Nov 16 '18 at 16:10


















          0














          Ok I solved this one.



          I was calling the keyframe_insert for the wrong object (anim, instead of the cube itself)



          This is the proper way to insert it, for example when the object is a camera:



          camera = bpy.context.scene.camera
          camera.keyframe_insert(data_path='location', index=0)
          camera.keyframe_insert(data_path='location', index=1)
          camera.keyframe_insert(data_path='location', index=2)
          camera.keyframe_insert('rotation_quaternion')


          in this way the keyframes are inserted on play.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324076%2fblender-modal-operator-cannot-insert-keyframe-and-move-object%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            The easy solution would be to enable blenders Auto Keyframing.



            If you still want to get your operator working, I expect you will need to not call other operators and work with the data directly, particularly within a modal operator.



            context.object.keyframe_insert('location')
            context.object.keyframe_insert('rotation_euler')





            share|improve this answer























            • Hello, thank you very much for your answer. Right now I am using blender 2.79. If I enable auto keyframing, or also with the solution you offered, I have the exact same problem. In fact, auto-keyframing works if, for instance, I grap the object with G. It doesn't if I move it by script, don't know why.

              – Paolo Gambardella
              Nov 16 '18 at 13:47












            • @PaoloGambardella I don't think you can have two modal operators running at the same time and both reacting to user input. With auto keyframing you can start playback and edit your data while frames are rolling, no script required at all.

              – keltar
              Nov 16 '18 at 14:02











            • I understand what you are saying but if I play (ALT+A) with autokeyframing on and after I activate the script, it doesn't work. And it doesn't also if I first run the script and then play.

              – Paolo Gambardella
              Nov 16 '18 at 14:08











            • @PaoloGambardella that's two separate solutions that you're mixing together. Please don't. Auto keyframing presumably does what you initially intended, there is no need for extra operator here. If that's not the case - I see two solutions, either emulating other operators inside yours (i.e. reacting to standard hotkeys, performing similar actions, etc.) or setting redraw callback and attempting to intercept things here; both will require quite large code and presumably will interfere with 'usual' workflow.

              – keltar
              Nov 16 '18 at 16:06











            • @keltar thank you for your answers. I am not mixing the two approaches, in fact I tried also to remove the calls to keyframe_insert, etc and still I cannot record anything. Now I am simply moving my object and nothing happens, even with autokeyframe insertion activated. Instead if I move my object with the Grab functionality, it works. It's like the modal operator created by me blocking the other functionality.

              – Paolo Gambardella
              Nov 16 '18 at 16:10















            1














            The easy solution would be to enable blenders Auto Keyframing.



            If you still want to get your operator working, I expect you will need to not call other operators and work with the data directly, particularly within a modal operator.



            context.object.keyframe_insert('location')
            context.object.keyframe_insert('rotation_euler')





            share|improve this answer























            • Hello, thank you very much for your answer. Right now I am using blender 2.79. If I enable auto keyframing, or also with the solution you offered, I have the exact same problem. In fact, auto-keyframing works if, for instance, I grap the object with G. It doesn't if I move it by script, don't know why.

              – Paolo Gambardella
              Nov 16 '18 at 13:47












            • @PaoloGambardella I don't think you can have two modal operators running at the same time and both reacting to user input. With auto keyframing you can start playback and edit your data while frames are rolling, no script required at all.

              – keltar
              Nov 16 '18 at 14:02











            • I understand what you are saying but if I play (ALT+A) with autokeyframing on and after I activate the script, it doesn't work. And it doesn't also if I first run the script and then play.

              – Paolo Gambardella
              Nov 16 '18 at 14:08











            • @PaoloGambardella that's two separate solutions that you're mixing together. Please don't. Auto keyframing presumably does what you initially intended, there is no need for extra operator here. If that's not the case - I see two solutions, either emulating other operators inside yours (i.e. reacting to standard hotkeys, performing similar actions, etc.) or setting redraw callback and attempting to intercept things here; both will require quite large code and presumably will interfere with 'usual' workflow.

              – keltar
              Nov 16 '18 at 16:06











            • @keltar thank you for your answers. I am not mixing the two approaches, in fact I tried also to remove the calls to keyframe_insert, etc and still I cannot record anything. Now I am simply moving my object and nothing happens, even with autokeyframe insertion activated. Instead if I move my object with the Grab functionality, it works. It's like the modal operator created by me blocking the other functionality.

              – Paolo Gambardella
              Nov 16 '18 at 16:10













            1












            1








            1







            The easy solution would be to enable blenders Auto Keyframing.



            If you still want to get your operator working, I expect you will need to not call other operators and work with the data directly, particularly within a modal operator.



            context.object.keyframe_insert('location')
            context.object.keyframe_insert('rotation_euler')





            share|improve this answer













            The easy solution would be to enable blenders Auto Keyframing.



            If you still want to get your operator working, I expect you will need to not call other operators and work with the data directly, particularly within a modal operator.



            context.object.keyframe_insert('location')
            context.object.keyframe_insert('rotation_euler')






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 16 '18 at 9:30









            samblersambler

            4,8311816




            4,8311816












            • Hello, thank you very much for your answer. Right now I am using blender 2.79. If I enable auto keyframing, or also with the solution you offered, I have the exact same problem. In fact, auto-keyframing works if, for instance, I grap the object with G. It doesn't if I move it by script, don't know why.

              – Paolo Gambardella
              Nov 16 '18 at 13:47












            • @PaoloGambardella I don't think you can have two modal operators running at the same time and both reacting to user input. With auto keyframing you can start playback and edit your data while frames are rolling, no script required at all.

              – keltar
              Nov 16 '18 at 14:02











            • I understand what you are saying but if I play (ALT+A) with autokeyframing on and after I activate the script, it doesn't work. And it doesn't also if I first run the script and then play.

              – Paolo Gambardella
              Nov 16 '18 at 14:08











            • @PaoloGambardella that's two separate solutions that you're mixing together. Please don't. Auto keyframing presumably does what you initially intended, there is no need for extra operator here. If that's not the case - I see two solutions, either emulating other operators inside yours (i.e. reacting to standard hotkeys, performing similar actions, etc.) or setting redraw callback and attempting to intercept things here; both will require quite large code and presumably will interfere with 'usual' workflow.

              – keltar
              Nov 16 '18 at 16:06











            • @keltar thank you for your answers. I am not mixing the two approaches, in fact I tried also to remove the calls to keyframe_insert, etc and still I cannot record anything. Now I am simply moving my object and nothing happens, even with autokeyframe insertion activated. Instead if I move my object with the Grab functionality, it works. It's like the modal operator created by me blocking the other functionality.

              – Paolo Gambardella
              Nov 16 '18 at 16:10

















            • Hello, thank you very much for your answer. Right now I am using blender 2.79. If I enable auto keyframing, or also with the solution you offered, I have the exact same problem. In fact, auto-keyframing works if, for instance, I grap the object with G. It doesn't if I move it by script, don't know why.

              – Paolo Gambardella
              Nov 16 '18 at 13:47












            • @PaoloGambardella I don't think you can have two modal operators running at the same time and both reacting to user input. With auto keyframing you can start playback and edit your data while frames are rolling, no script required at all.

              – keltar
              Nov 16 '18 at 14:02











            • I understand what you are saying but if I play (ALT+A) with autokeyframing on and after I activate the script, it doesn't work. And it doesn't also if I first run the script and then play.

              – Paolo Gambardella
              Nov 16 '18 at 14:08











            • @PaoloGambardella that's two separate solutions that you're mixing together. Please don't. Auto keyframing presumably does what you initially intended, there is no need for extra operator here. If that's not the case - I see two solutions, either emulating other operators inside yours (i.e. reacting to standard hotkeys, performing similar actions, etc.) or setting redraw callback and attempting to intercept things here; both will require quite large code and presumably will interfere with 'usual' workflow.

              – keltar
              Nov 16 '18 at 16:06











            • @keltar thank you for your answers. I am not mixing the two approaches, in fact I tried also to remove the calls to keyframe_insert, etc and still I cannot record anything. Now I am simply moving my object and nothing happens, even with autokeyframe insertion activated. Instead if I move my object with the Grab functionality, it works. It's like the modal operator created by me blocking the other functionality.

              – Paolo Gambardella
              Nov 16 '18 at 16:10
















            Hello, thank you very much for your answer. Right now I am using blender 2.79. If I enable auto keyframing, or also with the solution you offered, I have the exact same problem. In fact, auto-keyframing works if, for instance, I grap the object with G. It doesn't if I move it by script, don't know why.

            – Paolo Gambardella
            Nov 16 '18 at 13:47






            Hello, thank you very much for your answer. Right now I am using blender 2.79. If I enable auto keyframing, or also with the solution you offered, I have the exact same problem. In fact, auto-keyframing works if, for instance, I grap the object with G. It doesn't if I move it by script, don't know why.

            – Paolo Gambardella
            Nov 16 '18 at 13:47














            @PaoloGambardella I don't think you can have two modal operators running at the same time and both reacting to user input. With auto keyframing you can start playback and edit your data while frames are rolling, no script required at all.

            – keltar
            Nov 16 '18 at 14:02





            @PaoloGambardella I don't think you can have two modal operators running at the same time and both reacting to user input. With auto keyframing you can start playback and edit your data while frames are rolling, no script required at all.

            – keltar
            Nov 16 '18 at 14:02













            I understand what you are saying but if I play (ALT+A) with autokeyframing on and after I activate the script, it doesn't work. And it doesn't also if I first run the script and then play.

            – Paolo Gambardella
            Nov 16 '18 at 14:08





            I understand what you are saying but if I play (ALT+A) with autokeyframing on and after I activate the script, it doesn't work. And it doesn't also if I first run the script and then play.

            – Paolo Gambardella
            Nov 16 '18 at 14:08













            @PaoloGambardella that's two separate solutions that you're mixing together. Please don't. Auto keyframing presumably does what you initially intended, there is no need for extra operator here. If that's not the case - I see two solutions, either emulating other operators inside yours (i.e. reacting to standard hotkeys, performing similar actions, etc.) or setting redraw callback and attempting to intercept things here; both will require quite large code and presumably will interfere with 'usual' workflow.

            – keltar
            Nov 16 '18 at 16:06





            @PaoloGambardella that's two separate solutions that you're mixing together. Please don't. Auto keyframing presumably does what you initially intended, there is no need for extra operator here. If that's not the case - I see two solutions, either emulating other operators inside yours (i.e. reacting to standard hotkeys, performing similar actions, etc.) or setting redraw callback and attempting to intercept things here; both will require quite large code and presumably will interfere with 'usual' workflow.

            – keltar
            Nov 16 '18 at 16:06













            @keltar thank you for your answers. I am not mixing the two approaches, in fact I tried also to remove the calls to keyframe_insert, etc and still I cannot record anything. Now I am simply moving my object and nothing happens, even with autokeyframe insertion activated. Instead if I move my object with the Grab functionality, it works. It's like the modal operator created by me blocking the other functionality.

            – Paolo Gambardella
            Nov 16 '18 at 16:10





            @keltar thank you for your answers. I am not mixing the two approaches, in fact I tried also to remove the calls to keyframe_insert, etc and still I cannot record anything. Now I am simply moving my object and nothing happens, even with autokeyframe insertion activated. Instead if I move my object with the Grab functionality, it works. It's like the modal operator created by me blocking the other functionality.

            – Paolo Gambardella
            Nov 16 '18 at 16:10













            0














            Ok I solved this one.



            I was calling the keyframe_insert for the wrong object (anim, instead of the cube itself)



            This is the proper way to insert it, for example when the object is a camera:



            camera = bpy.context.scene.camera
            camera.keyframe_insert(data_path='location', index=0)
            camera.keyframe_insert(data_path='location', index=1)
            camera.keyframe_insert(data_path='location', index=2)
            camera.keyframe_insert('rotation_quaternion')


            in this way the keyframes are inserted on play.






            share|improve this answer



























              0














              Ok I solved this one.



              I was calling the keyframe_insert for the wrong object (anim, instead of the cube itself)



              This is the proper way to insert it, for example when the object is a camera:



              camera = bpy.context.scene.camera
              camera.keyframe_insert(data_path='location', index=0)
              camera.keyframe_insert(data_path='location', index=1)
              camera.keyframe_insert(data_path='location', index=2)
              camera.keyframe_insert('rotation_quaternion')


              in this way the keyframes are inserted on play.






              share|improve this answer

























                0












                0








                0







                Ok I solved this one.



                I was calling the keyframe_insert for the wrong object (anim, instead of the cube itself)



                This is the proper way to insert it, for example when the object is a camera:



                camera = bpy.context.scene.camera
                camera.keyframe_insert(data_path='location', index=0)
                camera.keyframe_insert(data_path='location', index=1)
                camera.keyframe_insert(data_path='location', index=2)
                camera.keyframe_insert('rotation_quaternion')


                in this way the keyframes are inserted on play.






                share|improve this answer













                Ok I solved this one.



                I was calling the keyframe_insert for the wrong object (anim, instead of the cube itself)



                This is the proper way to insert it, for example when the object is a camera:



                camera = bpy.context.scene.camera
                camera.keyframe_insert(data_path='location', index=0)
                camera.keyframe_insert(data_path='location', index=1)
                camera.keyframe_insert(data_path='location', index=2)
                camera.keyframe_insert('rotation_quaternion')


                in this way the keyframes are inserted on play.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 19 '18 at 16:56









                Paolo GambardellaPaolo Gambardella

                112




                112



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324076%2fblender-modal-operator-cannot-insert-keyframe-and-move-object%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo