XRayView.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import os.path
  4. from PyQt5.QtGui import QOpenGLContext
  5. from UM.Application import Application
  6. from UM.Logger import Logger
  7. from UM.Math.Color import Color
  8. from UM.PluginRegistry import PluginRegistry
  9. from UM.Platform import Platform
  10. from UM.Event import Event
  11. from UM.View.View import View
  12. from UM.Scene.Iterator.BreadthFirstIterator import BreadthFirstIterator
  13. from UM.View.RenderBatch import RenderBatch
  14. from UM.View.GL.OpenGL import OpenGL
  15. from cura.CuraApplication import CuraApplication
  16. from . import XRayPass
  17. ## View used to display a see-through version of objects with errors highlighted.
  18. class XRayView(View):
  19. def __init__(self):
  20. super().__init__()
  21. self._xray_shader = None
  22. self._xray_pass = None
  23. self._xray_composite_shader = None
  24. self._composite_pass = None
  25. self._old_composite_shader = None
  26. self._old_layer_bindings = None
  27. def beginRendering(self):
  28. scene = self.getController().getScene()
  29. renderer = self.getRenderer()
  30. if not self._xray_shader:
  31. self._xray_shader = OpenGL.getInstance().createShaderProgram(os.path.join(PluginRegistry.getInstance().getPluginPath("XRayView"), "xray.shader"))
  32. self._xray_shader.setUniformValue("u_color", Color(*Application.getInstance().getTheme().getColor("xray").getRgb()))
  33. for node in BreadthFirstIterator(scene.getRoot()):
  34. if not node.render(renderer):
  35. if node.getMeshData() and node.isVisible():
  36. renderer.queueNode(node,
  37. shader = self._xray_shader,
  38. type = RenderBatch.RenderType.Solid,
  39. blend_mode = RenderBatch.BlendMode.Additive,
  40. sort = -10,
  41. state_setup_callback = lambda gl: gl.glDepthFunc(gl.GL_ALWAYS),
  42. state_teardown_callback = lambda gl: gl.glDepthFunc(gl.GL_LESS)
  43. )
  44. def endRendering(self):
  45. pass
  46. def event(self, event):
  47. if event.type == Event.ViewActivateEvent:
  48. # FIX: on Max OS X, somehow QOpenGLContext.currentContext() can become None during View switching.
  49. # This can happen when you do the following steps:
  50. # 1. Start Cura
  51. # 2. Load a model
  52. # 3. Switch to Custom mode
  53. # 4. Select the model and click on the per-object tool icon
  54. # 5. Switch view to Layer view or X-Ray
  55. # 6. Cura will very likely crash
  56. # It seems to be a timing issue that the currentContext can somehow be empty, but I have no clue why.
  57. # This fix tries to reschedule the view changing event call on the Qt thread again if the current OpenGL
  58. # context is None.
  59. if Platform.isOSX():
  60. if QOpenGLContext.currentContext() is None:
  61. Logger.log("d", "current context of OpenGL is empty on Mac OS X, will try to create shaders later")
  62. CuraApplication.getInstance().callLater(lambda e = event: self.event(e))
  63. return
  64. if not self._xray_pass:
  65. # Currently the RenderPass constructor requires a size > 0
  66. # This should be fixed in RenderPass's constructor.
  67. self._xray_pass = XRayPass.XRayPass(1, 1)
  68. self.getRenderer().addRenderPass(self._xray_pass)
  69. if not self._xray_composite_shader:
  70. self._xray_composite_shader = OpenGL.getInstance().createShaderProgram(os.path.join(PluginRegistry.getInstance().getPluginPath("XRayView"), "xray_composite.shader"))
  71. theme = Application.getInstance().getTheme()
  72. self._xray_composite_shader.setUniformValue("u_background_color", Color(*theme.getColor("viewport_background").getRgb()))
  73. self._xray_composite_shader.setUniformValue("u_error_color", Color(*theme.getColor("xray_error").getRgb()))
  74. self._xray_composite_shader.setUniformValue("u_outline_color", Color(*theme.getColor("model_selection_outline").getRgb()))
  75. if not self._composite_pass:
  76. self._composite_pass = self.getRenderer().getRenderPass("composite")
  77. self._old_layer_bindings = self._composite_pass.getLayerBindings()
  78. self._composite_pass.setLayerBindings(["default", "selection", "xray"])
  79. self._old_composite_shader = self._composite_pass.getCompositeShader()
  80. self._composite_pass.setCompositeShader(self._xray_composite_shader)
  81. if event.type == Event.ViewDeactivateEvent:
  82. self.getRenderer().removeRenderPass(self._xray_pass)
  83. self._composite_pass.setLayerBindings(self._old_layer_bindings)
  84. self._composite_pass.setCompositeShader(self._old_composite_shader)