Blender使用python脚本创建屏幕截图,但在后台

时间:2017-07-05 22:57:42

标签: python screenshot blender

我是疯了还是有这个功能的错误(使用python脚本和后台运行的blender)?

1 /在Blender中复制/粘贴以下代码(来自screenshot.py)(切换到Scripting视图),它将截取屏幕截图但是......

2 /从命令行调用此脚本:(因此,当您使用脚本修改场景时,可以自动化屏幕截图...用于您的博客,教程等)。

blender --background --python screenshot.py

如果没有提供有关"错误"的信息,它就会崩溃。在上下文中

screenshot.py:

import os, sys

#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

  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 = 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("Window : " + str(window.width) + "x" + str(window.height) + ":" + str(window.x) + "," + str(window.y))
    print("Screen : " + str(screen.name) + ", Scene : " + str(screen.scene.name))
    #for area in bpy.context.screen.areas:
    for area in screen.areas:
      print("Area   : " + str(area.type))
      if area.type == 'VIEW_3D':
        for space in area.spaces:
          print("Space  : " + str(space.type))
          if space.type == 'VIEW_3D':
            #space.viewport_shade = 'RENDERED'
            for region in area.regions:
              print("Region  : " + str(region.type))
              if region.type == 'WINDOW':
                L_altBpyCtx = {                        # defining alternative context (allowing modifications without altering real 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)
                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", ".")

3顺便说一下,没有" - 背景"参数,它将生成一个空的截图(窗口的大小,充满灰色)。在此期间,取消注释

#bpy.context.window.screen = bpy.data.screens['3D View Full']

和/或

#space.viewport_shade = 'RENDERED'

并且您会看到您的Blender界面受到影响(将视图的布局从'默认'切换到' 3D全视图'和/或您的视口阴影来自&# 39;固定'默认情况下为'渲染')...因此脚本实际上已被解释,但它不会生成屏幕截图!

无论如何,问题似乎来自于调用Blender" - python somescript.py"直接来自命令行

我可能错过了什么?!谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

得到了Blender家伙的答案(感谢sergey)。底线:这是一个限制,因为通过命令行参数传递的所有脚本都是在命令行处理时执行的,在任何OpenGL绘制完成之前

  

警告是由脚本试图欺骗系统引起的   覆盖上下文,该上下文可能处于不一致状态。   从界面运行屏幕截图时,您不需要覆盖   任何背景。

     

通过命令行参数传递的所有脚本都在执行   命令行处理时间,在任何OpenGL绘制完成之前。该   截图运算符本身只是读取OpenGL缓冲区,m没有   做任何实际的绘图(它不能绘制任何东西,因为那是   不安全的)。

     

在后台模式下运行blender时,没有创建窗口,   这也意味着没有OpenGL上下文。你可以使用截图   在这种配置中。

     

非常感谢报告,但这只是限制而不是   容易被绕过。

精确度 :(因为提供的代码没有显示尝试“欺骗”Blender的所有尝试)

可悲的是,如果你还/仍然对这个功能感兴趣,你必须在外面思考(Blender)盒子!不要浪费你的时间试图“欺骗”Blender,比如延迟脚本执行,玩应用程序处理程序,在文本编辑器中伪造脚本导入等等......我已经为你完成了它:它不起作用,可能是出于类似的原因。