2
$\begingroup$

Hi I am a complete beginner to blender and I am trying to accomplish the following,

  1. Make a trajectory
  2. Move the camera along the trajectory by a small offset
  3. Call a function after every small offset

I have accomplished 1, for 2, I have a follow path constraint setup for the camera. Now I want to be able to move the camera along the trajectory using a python script, which would allow me to call another function.

def follow_path(): 
  camera = bpy.data.objects['Camera']
  path = bpy.data.objects['BezierCircle']

  camera.select = True
  path.select = True

  bpy.context.scene.objects.active = camera #select camera

  ##TO DO MOVE THE CAMERA####

  segment()
 
def segment():
   #do something

I am not able to find/understand any code that did this online. Any help is greatly appreciated, my scene setup and the constraints are Blender Scene with camera and path

follow path constraint on camera

$\endgroup$

1 Answer 1

1
$\begingroup$

TL;DNR: You need to find the follow path constraint modifier and set its offset value in segment.

Since you have a follow path constraint, all segment needs to do is set the Offset value of the constraint to a value between 0 and 100. Think of offset as the percentage of the path that the camera will have traversed.

Here's a very simple function that uses the current frame and calculate the percentage. If you run the script it will position the camera at the position it would take on frame 30.

import bpy

def segment(myConstraint):
    scene = bpy.context.scene.
    frames = scene.frame_end - bscene.frame_start + 1.
    current = scene.frame_current - scene.frame_start
    new_offset = current / frames
    myConstraint.offset = new_offset * 100.0
    
camera = bpy.data.objects['Camera']
path = bpy.data.objects['BezierCircle']
follow_path_constraint = camera.constraints['Follow Path']

bpy.context.scene.frame_current = 30
segment(follow_path_constraint)

This script needs a lot of warnings. It assumes that there is a camera, and a Bezier circle; and that the camera has a follow path constraint. It assumes that the all of these things have their default names.

$\endgroup$

You must log in to answer this question.

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