7
$\begingroup$

I know python well, but I'm not familiar with blender.

Is it possible to use the blender API to do this?

Setup: Create a scene in which the the lights and cameras are pre-arranged.

Run a python script which:

  1. Imports a model (fbx or obj).
  2. Renders from each camera and saves output to a .png file.
  3. Returns to step 1 until all models have been rendered.
$\endgroup$
10
$\begingroup$

Here's an outline with obj import, creates one camera in a new scene, loops thru a model list, makes a new scene with links to cam in created scene, loops thru the cams in the new scene (add more) and renders. You will need to edit the paths to suit your needs:

import bpy
import os
from math import radians

context = bpy.context

models_path = "//"
render_path = "//"

models = ["bretling.obj", "bretling.obj"]

#create a scene
scene = bpy.data.scenes.new("Scene")
camera_data = bpy.data.cameras.new("Camera")

camera = bpy.data.objects.new("Camera", camera_data)
camera.location = (-2.0, 3.0, 3.0)
camera.rotation_euler = ([radians(a) for a in (422.0, 0.0, 149)])
scene.objects.link(camera)

# do the same for lights etc
scene.update()

for model_path in models:
    scene.camera = camera
    path = os.path.join(models_path, model_path)
    # make a new scene with cam and lights linked
    context.screen.scene = scene
    bpy.ops.scene.new(type='LINK_OBJECTS')
    context.scene.name = model_path
    cams = [c for c in context.scene.objects if c.type == 'CAMERA']
    #import model
    bpy.ops.import_scene.obj(filepath=path, axis_forward='-Z', axis_up='Y', filter_glob="*.obj;*.mtl")
    for c in cams:
        context.scene.camera = c                                    
        print("Render ", model_path, context.scene.name, c.name)
        context.scene.render.filepath = "somepathmadeupfrommodelname"
        bpy.ops.render.render(write_still=True)

The import operator code can be gleaned from the Python Console by typing in bpy.ops.import and Ctrl+Space which will auto complete, for FBX:

>>> bpy.ops.import_scene.fbx(
fbx()
bpy.ops.import_scene.fbx(filepath="", axis_forward='-Z', axis_up='Y', directory="", filter_glob="*.fbx", ui_tab='MAIN', use_manual_orientation=False, global_scale=1, bake_space_transform=False, use_custom_normals=True, use_image_search=True, use_alpha_decals=False, decal_offset=0, use_anim=True, anim_offset=1, use_custom_props=True, use_custom_props_enum_as_string=True, ignore_leaf_bones=False, force_connect_children=False, automatic_bone_orientation=False, primary_bone_axis='Y', secondary_bone_axis='X', use_prepost_rot=True)

As you mention you are o fay with python, this should be well enough to make you dangerous.

Ideally I would probably set this up as an import operator with multi file select from a folder.

$\endgroup$
  • $\begingroup$ Somehow irrelevant but would appreciate if you can give me some help on this: I am importing many .obj files sequentially. Very quickly, the import time increases significantly to the point that it takes minutes for each object (even the ones with low-complexity) to get imported. Could you please take a look at my question here and see if you can offer a solution? $\endgroup$ – Amir Mar 4 '18 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.