3
$\begingroup$

In my file explorer I can see a small rendering of my scene as the icon for the .blend file. I wonder if I could retrieve this internally via libload..? This is just what I need: A small rendering of the first scene.

My question is: How can I get a render of the first scene of a separate .blend file?

$\endgroup$
4
  • 1
    $\begingroup$ see this and this answer $\endgroup$
    – Chebhou
    Mar 16 '15 at 19:17
  • $\begingroup$ But they both rely on external python script, so this is not what I need: An internal way of getting the render of a .blend file.. $\endgroup$
    – yPhil
    Mar 16 '15 at 19:23
  • $\begingroup$ would a low res opengl render be good for this ? $\endgroup$
    – Chebhou
    Mar 16 '15 at 19:27
  • $\begingroup$ Yes, I'm sure it would do just fine. What I need is very close to an icon, it's for the cars menu in a cartoonish racing game. $\endgroup$
    – yPhil
    Mar 16 '15 at 19:47
4
$\begingroup$

As has been pointed out blender has a python script to perform that task. While the *nix variations install this along with blender, the OSX and Windows distributions do not include it.

As with the rest of blender this script is available under the GPL license so you can copy part or all of the script and include it in the script you are making. A call to blend_extract_thumb(path) with the path to the file you want a thumbnail of will return an image buffer and resolution for you to use however you want.

$\endgroup$
1
$\begingroup$

To create the file preview i had two options


Option 1: externally

  • using a batch file and a script loop through the files open, render,save image to a file on this dir and pack it inside the .blend file.
  • in your main .bled file append the 'preview' image from each file or get files names and look for images with the same name

the batch file :

for %%f in (*.blend) do (
      blender %%~nf.blend -P initiate.py
)

the script :

import bpy  
import os

# path to the folder
file_path = bpy.data.filepath
file_name = bpy.path.display_name_from_filepath(file_path)
file_ext = '.blend'
file_dir = file_path.replace(file_name+file_ext, '')

#set render settings
bpy.data.scenes[0].render.resolution_x = 128
bpy.data.scenes[0].render.resolution_y = 128
bpy.data.scenes[0].render.resolution_percentage = 100
#render
bpy.ops.render.opengl(view_context = False)
#save image
img_name = file_name+".png"
bpy.data.images['Render Result'].save_render(file_dir+img_name)
bpy.ops.image.open(filepath = file_dir+img_name)
bpy.data.images[img_name].pack()
#close blender
bpy.ops.wm.quit_blender()

note: you can't render while blender in the background ( -b ) so you'll see blender open and close as many as there are files

workaround : you do this process to the files before distributing them so each file will contain its preview ( which will be appended to the main file in run-time )


Option 2: internally

  • append the scenes from each file
  • render the preview for each file
  • save the preview in the current .blend file and its dir
  • delete the scene if not needed

note : the only problem is that importing the scene and deleting it will leave textures and materials behind.

  • the result is images with the file names inside the current .bend file

import bpy  
import os


# path to the folder
file_path = bpy.data.filepath
file_name = bpy.path.display_name_from_filepath(file_path)
file_ext = '.blend'
file_dir = file_path.replace(file_name+file_ext, '')


# collect all .blend files
files_list = [f for f in os.listdir(file_dir) if f.endswith('.blend')] 

# iterate through files and import scenes

for i in files_list:

    filepath = os.path.join(file_dir,i)
    # load scenes with the name 'Scene'
    with bpy.data.libraries.load(filepath) as (data_from, data_to):
        data_to.scenes = ['Scene']#data_from.scenes[0]
        bpy.data.scenes[-1].name = i.replace('.blend','')       


# scenes list except the original
tracks = [t for t in bpy.data.scenes if t.name != 'Scene']
#tracks = [t for t in bpy.data.scenes if t.name.startswith("track")]

#loop through scenes and create a preview
mainScreen = bpy.context.screen
for scene in tracks:
    #set active scene
    mainScreen.scene = scene
    #set render settings
    scene.render.resolution_x = 128
    scene.render.resolution_y = 128
    scene.render.resolution_percentage = 100
    #render from camera (set view_context = True for the view render)
    bpy.ops.render.opengl(view_context = False)


    #save and load the render (you can't keep the render result)
    img_name = scene.name+".png"

    bpy.data.images['Render Result'].save_render(file_dir+img_name)
    bpy.ops.image.open(filepath = file_dir+img_name)
    #delete the scene
    bpy.ops.scene.delete()
$\endgroup$
2
  • 1
    $\begingroup$ Please word this more clearly. Honestly I couldn't tell if you were writing an answer or just putting an extended comment in the answers section until I re-read it about 3 times :) $\endgroup$ Mar 17 '15 at 15:32
  • 1
    $\begingroup$ @AWildRolandiXor will edit, it looks like a follow up question :P $\endgroup$
    – Chebhou
    Mar 17 '15 at 15:36

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.