Page MenuHome

screenshot.py

import os, sys
import bpy
#FIXME: Blender creates screenshot using python but not when running in background
#
# Same result from Ubuntu stock version (2.76) and from ppa (2.78)
#
# Within Blender : unable to fix this last warning but screenshot is generated !
#
#Traceback (most recent call last):
# File "/usr/share/blender/2.78/scripts/startup/bl_ui/space_text.py", line 32, in draw
# text = st.text
#AttributeError: 'SpaceView3D' object has no attribute 'text'
#
# From command line (crash) : blender --background --python screenshot.py
# Unable to determine what's wrong in the context
#
#Traceback (most recent call last):
#(...)
#File "/usr/share/blender/2.78/scripts/modules/bpy/ops.py", line 187, in __call__
# ret = op_call(self.idname_py(), C_dict, kw, C_exec, C_undo)
#RuntimeError: Operator bpy.ops.screen.screenshot.poll() failed, context is incorrect
def screenshot(P_filename, P_path = None):
#import bpy
print("Using Blender v" + bpy.app.version_string)
L_saveAs = P_filename
L_saveAs = os.path.join(P_path, L_saveAs) if P_path else os.path.join("/tmp", L_saveAs)
print("Scene saved in " + L_saveAs)
#XXX: switching to 3D full view so screenshot maximize scene in main window
#bpy.context.window.screen = bpy.data.screens['3D View Full']
for window in bpy.context.window_manager.windows:
screen = window.screen
print("DEBUG] Window : " + str(window.width) + "x" + str(window.height) + ":" + str(window.x) + "," + str(window.y))
print("DEBUG] Screen : " + str(screen.name) + ", Scene : " + str(screen.scene.name))
#for area in bpy.context.screen.areas:
for area in screen.areas:
print("DEBUG] Area : " + str(area.type))
if area.type == 'VIEW_3D':
for space in area.spaces:
print("DEBUG] Space : " + str(space.type))
if space.type == 'VIEW_3D':
#space.viewport_shade = 'RENDERED'
for region in area.regions:
print("DEBUG] Region : " + str(region.type))
if region.type == 'WINDOW':
L_altBpyCtx = { # defining alternative context (allowing modifications without altering main one)
'area' : area # our 3D View (first found)
, 'blend_data': bpy.context.blend_data # just to suppress PyContext warning, doesn't seem to have any effect
#, 'edit_text' : bpy.context.edit_text # just to suppress PyContext warning, doesn't seem to have any effect
, 'region' : None # just to suppress PyContext warning, doesn't seem to have any effect
#, 'scene' : bpy.context.scene
, 'scene' : screen.scene
, 'space' : space
, 'screen' : window.screen
#, 'window' : bpy.context.window # current window, could also copy context
, 'window' : window # current window, could also copy context
}
bpy.ops.screen.screenshot(L_altBpyCtx, filepath = L_saveAs, full = False)
#bpy.ops.screen.screenshot(L_altBpyCtx, filepath = L_saveAs, full = True)
break #XXX: limit to the window of the 3D View
break #XXX: limit to the corresponding space (3D View)
break #XXX: limit to the first 3D View (area)
screenshot("screenshot.png", ".")
'''
# Delaying script execution do nothing. No matter what, calling blender from command line
# with "--python" option seems to execute script before UI is ready !
#
# Trying to find a workaround playing with application handlers ... without success
from bpy.app.handlers import persistent
@persistent
def onSavePost(scene):
print("DEBUG] onSavePost: " + bpy.data.filepath)
screenshot("screenshot.png", ".")
@persistent
def onLoadPre(dummy):
print("DEBUG] onLoadPre: " + bpy.data.filepath)
screenshot("screenshot.png", ".")
@persistent
def onLoadPost(dummy):
print("DEBUG] onLoadPost: " + bpy.data.filepath)
screenshot("screenshot.png", ".")
def save():
L_filepath = bpy.data.filepath
if L_filepath == "":
print("[DEBUG] No current file ! ")
L_filepath = "/tmp/dummy.blend"
else: L_filepath = bpy.data.filepath
print("[DEBUG] Saving blend file in : " + L_filepath)
bpy.ops.wm.save_as_mainfile(filepath = L_filepath)
if onLoadPost not in bpy.app.handlers.load_post: bpy.app.handlers.load_post.append(onLoadPost)
if onLoadPre not in bpy.app.handlers.load_pre : bpy.app.handlers.load_pre.append(onLoadPre)
if onSavePost not in bpy.app.handlers.save_post: bpy.app.handlers.save_post.append(onSavePost)
# load_post and load_pre are not raised
# Saving the file raises bpy.app.handlers.save_post handler but the result is the same (not a valid screenshot)
save()
'''

File Metadata

Mime Type
text/x-python
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
37/03/9d8d839a7d6cd2d7254d48233e13

Event Timeline