Skip to main content
more pythonic handling
Source Link
Leander
  • 27.8k
  • 2
  • 46
  • 109

How about the following function? You would just need to get your ascii data to match the XYZ/RXRYRZ.

import bpy 
import bpy.types
import time
from mathutils import *

# positions and rotates an object (globally) and create a keyframe
# at the given frame#
# Change the rotation mode to your liking.
def animate_object( obj, x, y, z, rx, ry, rz, frame_number ):
    obj.location = Vector((x,y,z))
    obj.rotation_mode = 'XYZ'
    obj.rotation_euler = Euler((rx,ry,rz))
    obj.keyframe_insert(data_path="location", frame=frame_number)
    obj.keyframe_insert(data_path="rotation_euler", frame=frframe=frame_number)

ex:

cam_positions = (
    ( 1.56,3.13,-1.83, -4.20,-2.58,-3.48 ),
    ( 1.74,3.19,-1.63, -4.40,-1.95,-3.67 ),
    #...
    ( 2.01,3.03,-1.57, -4.32,-1.61,-3.64 )
)

cam=bpycam = bpy.data.objects["Camera"]

frame_number = 1
for frame_number, cp in enumerate(cam_positions):
    animate_object( cam, cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]*cp, frame_number )
  frame_number = frame_number + 1)

How about the following function? You would just need to get your ascii data to match the XYZ/RXRYRZ.

import bpy 
import bpy.types
import time
from mathutils import *

# positions and rotates an object (globally) and create a keyframe
# at the given frame#
# Change the rotation mode to your liking.
def animate_object( obj, x, y, z, rx, ry, rz, frame_number ):
  obj.location = Vector((x,y,z))
  obj.rotation_mode = 'XYZ'
  obj.rotation_euler = Euler((rx,ry,rz))
  obj.keyframe_insert(data_path="location", frame=frame_number)
  obj.keyframe_insert(data_path="rotation_euler", frame=fr

ex:

cam_positions = (
  ( 1.56,3.13,-1.83, -4.20,-2.58,-3.48 ),
  ( 1.74,3.19,-1.63, -4.40,-1.95,-3.67 ),
  ...
  ( 2.01,3.03,-1.57, -4.32,-1.61,-3.64 )
)

cam=bpy.data.objects["Camera"]

frame_number = 1
for cp in cam_positions:
  animate_object( cam, cp[0], cp[1], cp[2], cp[3], cp[4], cp[5], frame_number )
  frame_number = frame_number + 1

How about the following function? You would just need to get your ascii data to match the XYZ/RXRYRZ.

import bpy 
import bpy.types
import time
from mathutils import *

# positions and rotates an object (globally) and create a keyframe
# at the given frame#
# Change the rotation mode to your liking.
def animate_object( obj, x, y, z, rx, ry, rz, frame_number ):
    obj.location = Vector((x,y,z))
    obj.rotation_mode = 'XYZ'
    obj.rotation_euler = Euler((rx,ry,rz))
    obj.keyframe_insert(data_path="location", frame=frame_number)
    obj.keyframe_insert(data_path="rotation_euler", frame=frame_number)

ex:

cam_positions = (
    ( 1.56,3.13,-1.83, -4.20,-2.58,-3.48 ),
    ( 1.74,3.19,-1.63, -4.40,-1.95,-3.67 ),
    #...
    ( 2.01,3.03,-1.57, -4.32,-1.61,-3.64 )
)

cam = bpy.data.objects["Camera"]

frame_number = 1
for frame_number, cp in enumerate(cam_positions):
    animate_object(cam, *cp, frame_number + 1)
Source Link
Bruno
  • 4.2k
  • 8
  • 21

How about the following function? You would just need to get your ascii data to match the XYZ/RXRYRZ.

import bpy 
import bpy.types
import time
from mathutils import *

# positions and rotates an object (globally) and create a keyframe
# at the given frame#
# Change the rotation mode to your liking.
def animate_object( obj, x, y, z, rx, ry, rz, frame_number ):
  obj.location = Vector((x,y,z))
  obj.rotation_mode = 'XYZ'
  obj.rotation_euler = Euler((rx,ry,rz))
  obj.keyframe_insert(data_path="location", frame=frame_number)
  obj.keyframe_insert(data_path="rotation_euler", frame=fr

ex:

cam_positions = (
  ( 1.56,3.13,-1.83, -4.20,-2.58,-3.48 ),
  ( 1.74,3.19,-1.63, -4.40,-1.95,-3.67 ),
  ...
  ( 2.01,3.03,-1.57, -4.32,-1.61,-3.64 )
)

cam=bpy.data.objects["Camera"]

frame_number = 1
for cp in cam_positions:
  animate_object( cam, cp[0], cp[1], cp[2], cp[3], cp[4], cp[5], frame_number )
  frame_number = frame_number + 1