Blender使用python脚本创建屏幕截图,但在后台运行时不会



我疯了吗,或者这个功能有什么错误(使用python脚本和blender在后台运行)?

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

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/顺便说一下,在没有"--background"参数的情况下调用它,它将生成一个空的屏幕截图(窗口大小,充满灰色)。同时,取消评论

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

和/或

#space.viewport_shade = 'RENDERED'

你会看到你的Blender界面受到影响(将视图的布局从"默认"切换到"3D全视图"和/或你的视口阴影从"固体",默认情况下,到"渲染")......所以脚本实际上是被解释的,但它不会生成屏幕截图!

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

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

从一个搅拌机的人那里得到了答案(谢谢谢尔盖)。底线:这是一个限制,因为通过命令行参数传递的所有脚本都在命令行处理时执行,在任何OpenGL绘制完成之前!

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

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

在后台模式下运行搅拌机时,没有创建窗口, 这也意味着没有OpenGL上下文。您可以;t 使用屏幕截图 在这样的配置中。

所以感谢您的报告,但这只是限制,不能 很容易绕过。

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

可悲的是,如果您也/仍然对此类功能感兴趣,则必须跳出(搅拌机)框框思考!不要浪费时间试图"欺骗"Blender,比如延迟脚本执行、使用应用程序处理程序、在文本编辑器中伪造脚本导入等......我已经为你做了:它不起作用,可能出于非常相似的原因。

相关内容

最新更新