1
$\begingroup$

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)
$\endgroup$

1 Answer 1

0
$\begingroup$

You can use an auxiliary vector n which indicate the direction of the cone, and make n to be a mix between the direction to the target and another vector n0. Then the coefficients of the mix should vary smoothly in time.

import bpy
import mathutils 

context = bpy.context
scene = context.scene

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

df = 30
n0 = mathutils.Vector((1., 0., 0.))

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
    n = dv.normalized()
    rot_quat = n.to_track_quat('Z', 'Y')
  elif a < 60 + df :
    loc = mathutils.Vector((-10.0, 14.05, 7.0))   
    dv = loc - obA.location
    n = (df-abs(2*(a-60)-df))*n0 + (2*(a-60)-df)*dv.normalized()
    rot_quat = n.to_track_quat('Z', 'Y')
  else : 
    loc = mathutils.Vector((-10.0, 14.05, 7.0))   
    dv = loc - obA.location
    n = dv.normalized()
    rot_quat = n.to_track_quat('Z', 'Y')

  obA.rotation_euler = rot_quat.to_euler()

#rotation
  speed = obA["speed"]

  if speed > 0.000001 and n.length > 0.000001:
    obA.location += speed * n.normalized()


bpy.app.handlers.frame_change_post.clear()
bpy.app.handlers.frame_change_post.append(setloc)
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .