2
$\begingroup$

I want to know If I can use blender as a photo and video viewer? Like If I have a photo on my computer I just have to press "open with" blender and blender automatically open on the UV image editor if it is a photo or on the video editor if it is a video. If not can this be done by a script or something?

$\endgroup$
  • $\begingroup$ a suggestion : associate these types with a shell script which launch blender with the appropriate python script based on the file type $\endgroup$ – Chebhou Jul 24 '15 at 14:31
  • $\begingroup$ I dont know what is the shell script is,can you explain? $\endgroup$ – Omar Emara Jul 25 '15 at 11:08
3
$\begingroup$

Blender can be a useful image viewer for some formats like multilayer OpenEXR. I just expanded a bit on Chebhou's answer by creating a python script that calls blender, I expect this can be used on any *nix platform included OSX and possibly even windows.

The script that gets called or set as the application to open image files is -

#!/usr/bin/env python3

from subprocess import call
from sys import argv
import os

sdir = os.path.abspath(os.path.dirname(argv[0]))

calllist = [
    'blender',
    '--factory-startup',
    os.path.join(sdir, 'blend_image_view.blend'),
    '--python',
    os.path.join(sdir, 'blend_open_image_file.py'),
    '--',
    ]

# add the args we get as filenames
calllist.extend(argv)

call(calllist)

The factory-startup and blend file can be removed from the list of options sent to blender. I have blender setup to start with a window that fills two monitors, that doesn't work as well when used as a simple image viewer. I also setup a start file that has two simple layouts for image and movie viewing, leaving a window clear of any excess.

The main working script I adjusted a bit to handle multiple files. When multiple movies are opened I have them in separate channels, with all but the first video muted. I really don't think blender is a good choice for video viewing, this could be improved by adding a way to extract frame rates and resolutions to setup blender to match, which doesn't work well if you open multiple videos with different frame rates and sizes.

import bpy
from sys import argv
import os

# change these two values to match screens names available
# UV Editing
blenderImageScreenName = 'ImageViewer'
# Video Editing
blenderMovieScreenName = 'MovieViewer'

acceptedImgFiles = ['.bmp', '.cin', '.dpx',  '.exr',
                    '.hdr', '.jpg', '.jpeg', '.jp2',
                    '.j2k', '.png', '.rgb',  '.tga',
                    '.tif'
                    ]
acceptedMovFiles = ['.avi',  '.flv', '.h264', '.mov',
                    '.mpeg', '.mpg', '.mp2',  '.mp3',
                    '.mp4',  '.mkv', '.ogg',  '.xvid'
                    ]

fileStartIdx = argv.index('--') + 2
numFilesToOpen = len(argv) - fileStartIdx
showAreaType = 'image'
curChann = 1

for i in range(numFilesToOpen):
    filename = os.path.abspath(argv[fileStartIdx + i])
    ext = os.path.splitext(filename)[1].lower()

    if ext in acceptedImgFiles:
        bpy.ops.image.open(filepath=argv[fileStartIdx + i])
    elif ext in acceptedMovFiles:
        # while we will open video files, blender isn't the best option here
        # When opening multiple videos, each video is imported into a new track
        # tracks after the first will be muted to prevent a terrifying
        # mess of multiple audio tracks
        # end frame is set to the longest video track imported
        # frame rate is left default and will need to be adjusted for each video
        nc = bpy.context.scene.sequence_editor.sequences.new_movie(name=argv[fileStartIdx + i],
                filepath=os.path.abspath(argv[fileStartIdx + i]), frame_start=1, channel=curChann)
        if bpy.context.scene.frame_end < nc.frame_duration:
            bpy.context.scene.frame_end = nc.frame_duration
        # I expected new_movie() to import video and sound
        # but that doesn't happen so we also add sound from the same file
        nca = bpy.context.scene.sequence_editor.sequences.new_sound(name=argv[fileStartIdx + i],
                filepath=os.path.abspath(argv[fileStartIdx + i]), frame_start=1, channel=curChann+1)
        if curChann > 1:
            nc.mute = True
            nca.mute = True
        curChann += 2
        showAreaType = 'video'
    else:
        print("Don't know what type of file this is -",filename)

if showAreaType == 'image':
    bpy.context.window.screen = bpy.data.screens[blenderImageScreenName]
    # make first image active
    for area in bpy.data.screens[blenderImageScreenName].areas:
        if area.type == 'IMAGE_EDITOR':
            area.spaces.active.image = bpy.data.images[0]
else:
    bpy.context.window.screen = bpy.data.screens[blenderMovieScreenName]

Save this as blend_open_image_file.py to match the filename set in the first script.

The list of extensions can easily be extended.

I thought I would keep a copy of this on github which also has the blend file mentioned above if anyone thinks it can be improved.

$\endgroup$
  • $\begingroup$ When I open the image he give me a cmd screen that write some line and close again,what am I doing wrong. $\endgroup$ – Omar Emara Jul 27 '15 at 11:14
  • $\begingroup$ most likely you didn't add the shebang line that I left out - the first line of the script should be #!/usr/bin/env python3 $\endgroup$ – sambler Jul 27 '15 at 13:13
  • $\begingroup$ Still the same,what should the call script format be?and should I set a path or something before doing anything? $\endgroup$ – Omar Emara Jul 27 '15 at 14:11
  • $\begingroup$ You probably have to enter the full path to blender in the first calllist item, on osx that would be something like /Applications/Blender/blender.app/Contents/MacOS/blender $\endgroup$ – sambler Jul 27 '15 at 14:27
  • $\begingroup$ I am using windows 10,I put blender path instead of "blender",but it is still not working.what should the file format be for the call script? $\endgroup$ – Omar Emara Jul 27 '15 at 14:36
1
$\begingroup$

This is not for OSX ( i don't have access to one ) but the concept is the same as on Windows OS:

  • create a folder that contains two files myscript.bat and import_file_script.py
  • assign the file types ( that you want to open with blender ) to be opened by this script ( RMB->Open_with->choose_default_program and select the .bat file )
  • myscript.bat is a batch script ( on OSX shell/apple-script ) and in this file put the following lines :

@ECHO OFF

SET script=%~dp0import_file_script.py   
SET ext=%~x1                            
SET filename=%~f1                       
SET blender="C:/Program Files/Blender Foundation/Blender/blender.exe"

%blender% --python %script% -- %ext% %filename%

and in the python script file ( named : import_file_script.py )

import bpy
import sys

bpy.ops.wm.console_toggle()

argv = sys.argv
ext = argv[argv.index("--") + 1]  
myfile = argv[argv.index("--") + 2]

if ext in ['.png','.jpg','.jpeg']:
    bpy.context.window.screen = bpy.data.screens['Image Viewer'] # or just ['UV Editing']
    bpy.ops.image.open(filepath=myfile)
    for area in bpy.data.screens['Image Viewer'].areas : 
        if area.type == 'IMAGE_EDITOR' :
                area.spaces.active.image = bpy.data.images[0]

#you can add elif/switch for multi-extention handling
  • in your startup blend file you can create a screen layout and call it Image Viewer that is simply one UV image area

Now when you double click an image it will be handled by the batch script which will call blender that will run the python script responsible for importing ( or just opening ) the file in blender

$\endgroup$

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.