offscreen rendering does not support as background process on windows scheduler
I'm trying to run a following vtk-python code through windows scheduler,
it runs in forground and failed in background.
import vtk
import logging
from vtkmodules.util import numpy_support
try:
logging.basicConfig(filename='app.log', filemode='w', format='%(name)s - %(levelname)s - %(message)s')
logging.warning('This will get logged to a file')
# Create a sphere
sphere = vtk.vtkSphereSource()
sphere.SetThetaResolution(20)
sphere.SetPhiResolution(20)
logging.warning('1')
# Create a mapper
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(sphere.GetOutputPort())
logging.warning('2')
# Create an actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetColor(1, 0, 0) # Set color to red
# Create a renderer
renderer = vtk.vtkRenderer()
renderer.AddActor(actor)
renderer.SetBackground(1, 1, 1) # Set background to white
logging.warning('3')
# Create a render window
renderWindow = vtk.vtkRenderWindow()
# Enable off-screen rendering (headless rendering)
renderWindow.SetOffScreenRendering(1)
# Set desired size (optional)
renderWindow.SetSize(400, 400)
renderWindow.AddRenderer(renderer)
logging.warning('4')
# Create a render window interactor
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
logging.warning('5')
# Render the scene
renderWindow.Render()
logging.warning('6')
# Grab a screenshot (offscreen rendering)
windowToImageFilter = vtk.vtkWindowToImageFilter()
windowToImageFilter.SetInput(renderWindow)
windowToImageFilter.Update()
logging.warning('7')
# Convert VTK image to NumPy array
vtk_image = windowToImageFilter.GetOutput()
height, width, _ = vtk_image.GetDimensions()
vtk_array = vtk_image.GetPointData().GetScalars()
components = vtk_array.GetNumberOfComponents()
vtk_data = numpy_support.vtk_to_numpy(vtk_array)
image_array = vtk_data.reshape(height, width, components)
logging.warning('8')
# Save the screenshot using vtkPNGWriter
writer = vtk.vtkPNGWriter()
writer.SetFileName("D:\\offscreen_rendering\\screenshot.png")
writer.SetInputConnection(windowToImageFilter.GetOutputPort())
writer.Write()
logging.warning('9')
except Exception as e:
with open('D:\\offscreen_rendering\\error.txt', 'w') as fw:
fw.write(str(e))
It run with following settings,
And failed with the following setting
The Only difference is Run whether user is logged in or not
for reference here i'm attaching the logs of the both scenario.