PreviewPass.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Uranium is released under the terms of the LGPLv3 or higher.
  3. from UM.Application import Application
  4. from UM.Resources import Resources
  5. from UM.View.RenderPass import RenderPass
  6. from UM.View.GL.OpenGL import OpenGL
  7. from UM.View.RenderBatch import RenderBatch
  8. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  9. from typing import Optional
  10. MYPY = False
  11. if MYPY:
  12. from UM.Scene.Camera import Camera
  13. ## A render pass subclass that renders slicable objects with default parameters.
  14. # It uses the active camera by default, but it can be overridden to use a different camera.
  15. #
  16. # This is useful to get a preview image of a scene taken from a different location as the active camera.
  17. class PreviewPass(RenderPass):
  18. def __init__(self, width: int, height: int):
  19. super().__init__("preview", width, height, 0)
  20. self._camera = None # type: Optional[Camera]
  21. self._renderer = Application.getInstance().getRenderer()
  22. self._shader = None
  23. self._scene = Application.getInstance().getController().getScene()
  24. # Set the camera to be used by this render pass
  25. # if it's None, the active camera is used
  26. def setCamera(self, camera: Optional["Camera"]):
  27. self._camera = camera
  28. def render(self) -> None:
  29. if not self._shader:
  30. self._shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "object.shader"))
  31. # Create a new batch to be rendered
  32. batch = RenderBatch(self._shader)
  33. # Fill up the batch with objects that can be sliced. `
  34. for node in DepthFirstIterator(self._scene.getRoot()):
  35. if node.callDecoration("isSliceable") and node.getMeshData() and node.isVisible():
  36. batch.addItem(node.getWorldTransformation(), node.getMeshData())
  37. self.bind()
  38. if self._camera is None:
  39. batch.render(Application.getInstance().getController().getScene().getActiveCamera())
  40. else:
  41. batch.render(self._camera)
  42. self.release()