How to smoothly rotate an object


(spod1) #1

Hi, I am trying to rotate an object from one direction to other direction. How can we make it smooth rotation? Right now it is making a jumpy rotation with the following code. Thanks in advance

import bpy
import mathutils 


context = bpy.context
scene = context.scene


obA = scene.objects.get("Cone")
obA["speed"] = 0.1


def setloc(scene):
    


    a = bpy.data.scenes["Scene"].frame_current
    if a < 60:
        loc = mathutils.Vector((8.0, -0.05, 0.0))
        dv = loc - obA.location
    else:
        loc = mathutils.Vector((-10.0, 14.05, 7.0))   
        dv = loc - obA.location
    
    #rotation
    rot_quat = dv.to_track_quat('Z', 'Y')
    obA.rotation_euler = rot_quat.to_euler()
    speed = obA["speed"]
  
    if speed > 0.000001 and dv.length > 0.000001:
        obA.location += speed * dv.normalized()




bpy.app.handlers.frame_change_post.clear()
bpy.app.handlers.frame_change_post.append(setloc)


(Microno) #2

You’re currently using an if-else statement to do the rotations hence why it is jumpy.

If you want the rotations to be smooth then you will have to change your code so that it uses spherical interpolation between the two rotation states over a small range of frames.

For example, if you want the object to rotate smoothly around frame 60 then you’ll have to interpolate the rotation from, say frame 55, to, say frame 65 depending on how fast you want the rotation to be.