4
$\begingroup$

How to make a video of a PLY mesh from python script? I need to place the model in space and rotate it of 360 degrees on an axis.

Here is a ply example: http://156.54.99.175/3d/sodark.zip

$\endgroup$
7
  • $\begingroup$ Do you mean a turntable? Is there any difference between ply and other import formats in this case? What have you tried so far? Related: blender.stackexchange.com/questions/1459/… $\endgroup$
    – p2or
    Commented Oct 9, 2015 at 8:12
  • $\begingroup$ Thanks for your reply. I want to get a video file of my object rotated by 360 degrees. I would like to automate the process with a python script. $\endgroup$
    – sborfedor
    Commented Oct 9, 2015 at 8:40
  • $\begingroup$ sborfedor, what render engine? Internal or Cycles? $\endgroup$
    – zeffii
    Commented Oct 9, 2015 at 10:24
  • 1
    $\begingroup$ Something like this: blender.org/manual/_images/multiview_workflow_6.gif, right? I think this question needs to be reworded/clarified. For a solid answer more information is required: Rendering in OpenGL, Blender-Internal, Cycles? Background color? Camera properties? Object size? etc. Perhaps it's a good idea to upload an example scene. $\endgroup$
    – p2or
    Commented Oct 9, 2015 at 10:24
  • 1
    $\begingroup$ I think in this question it doesn't make any difference whether the model was a .ply or not. If it's inside blender it is an Object like Suzanne. $\endgroup$
    – zeffii
    Commented Oct 9, 2015 at 10:37

2 Answers 2

7
$\begingroup$

You could use the add-ons linked to in the comments, but if you already have some form of script it's easy enough to plug in a small bit of code to get the same effect. Something like this. This adds a Camera (child) and an Empty (parent), then rotates the Empty on the Z-axis and keyframes each rotation, for frame: (1..num_frames+1).

import math
import bpy

def add_cam(location, rotation):
    bpy.ops.object.camera_add(location=location, rotation=rotation)
    return bpy.context.active_object

def add_empty(location):
    bpy.ops.object.empty_add(location=location)
    return bpy.context.active_object    

cam = add_cam(location=(0, -5, 0), rotation=(math.pi/2, 0, 0))
empty = add_empty(location=(0, 0, 0))
cam.parent = empty

num_frames = 90
gamma = math.pi * 2 / num_frames
for i in range(1, num_frames+1):
    empty.rotation_euler[2] = gamma * i
    empty.keyframe_insert(data_path='rotation_euler', frame=i, index=-1)     

You might perform some calculations to figure out based on the size and orientation of the Object, where to place the Empty and how far away to place the Camera and which Euler axis to rotate around.

animation example

Then you set your render resolution x, y and quality and output type, and scene.frame_start, scene.frame_end. A smart approach is to render the sequence as separate frames first, using the .png format. Then decide on the kind of final video format. A popular webformat is GIF or MP4. Blender can output a couple of video formats, but GIF is not one of them.

MP4
Blender can load a directory full of .png in the Video Sequence Editor (VSE) and render directly to a video video format/container (mp4, avi...etc). See Save rendered images in different formats

GIF (animated)
Unfortunately there's no feature that lets you convert a sequence of .png straight to GIF built in. But there are add-ons that use tools like ImageMagick and ffmpeg to do the conversion.

$\endgroup$
2
  • $\begingroup$ That's great! But how to get out the animated gif or video or a sequence of rendered images? $\endgroup$
    – sborfedor
    Commented Oct 9, 2015 at 11:56
  • $\begingroup$ Can you explain how exactly use scene.frame_end and scene.frame_start (should we add them in a code, or use somehow in a different way)? $\endgroup$
    – Serob_b
    Commented Feb 7, 2019 at 15:17
0
$\begingroup$

For zeffii's answer to this, I want to point out that there actually isn't a need to load up on the keyframes. You really only need to keyframe the initial and final rotation values, so a minimum of two keyframes. Plus, with two keyframes, you can easily play around with the interpolation between them instead of for example having to hard-code a sinusoidal interpolation function. I also find that camera movements which contain easing effects usually look much nicer. If you really want linear interpolation with two keyframes, you can set it as the default in preferences, manually do it, or you can code it into the python script, which is what I did here:

import math
import bpy

def add_cam(location, rotation):
    bpy.ops.object.camera_add(location=location, rotation=rotation)
    return bpy.context.active_object

def add_empty(location):
    bpy.ops.object.empty_add(location=location)
    return bpy.context.active_object    

cam = add_cam(location=(0, -5, 0), rotation=(math.pi/2, 0, 0))
empty = add_empty(location=(0, 0, 0))
cam.parent = empty


num_frames = 90

empty.rotation_euler.z = 0
empty.keyframe_insert(data_path='rotation_euler', frame=1, index = -1)     

empty.rotation_euler.z = 2 * math.pi
empty.keyframe_insert(data_path='rotation_euler', frame=num_frames, index = -1) 

       
# set interpolation to linear:      
bpy.ops.object.select_all(action='DESELECT')  
empty.select_set(True)
bpy.context.area.ui_type = 'FCURVES'
bpy.ops.graph.interpolation_type(type='LINEAR')
bpy.context.area.ui_type = 'TEXT_EDITOR'
$\endgroup$

You must log in to answer this question.

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