def onLayoutSelectButton(self): layoutManager = slicer.app.layoutManager() currentLayout = layoutManager.layout # place skeleton model in first 3d view skeleton_volume = slicer.util.getFirstNodeByName("SkeletonModel") viewNode = layoutManager.threeDWidget(0).mrmlViewNode() if skeleton_volume is not None: displayNode = skeleton_volume.GetDisplayNode() displayNode.SetViewNodeIDs([viewNode.GetID()]) # place reconstructed volume in second 3d view # hacky but necessary way to ensure that we grab the correct browser node i = 1 found = False browser = slicer.util.getFirstNodeByClassByName( 'vtkMRMLSequenceBrowserNode', 'LandmarkingScan') while not found and i < 6: if browser == None: browser = slicer.util.getFirstNodeByClassByName( 'vtkMRMLSequenceBrowserNode', 'LandmarkingScan_{}'.format(str(i))) if browser != None: found = True else: found = True i += 1 if browser is not None: spine_volume = slicer.util.getFirstNodeByName( browser.GetName() + 'ReconstructionResults') if layoutManager.threeDWidget(1) is not None: viewNode = layoutManager.threeDWidget(1).mrmlViewNode() else: newView = slicer.vtkMRMLViewNode() newView = slicer.mrmlScene.AddNode(newView) newWidget = slicer.qMRMLThreeDWidget() newWidget.setMRMLScene(slicer.mrmlScene) newWidget.setMRMLViewNode(newView) if spine_volume is not None: displayNode = slicer.modules.volumerendering.logic( ).GetFirstVolumeRenderingDisplayNode(spine_volume) displayNode.SetViewNodeIDs([viewNode.GetID()]) spine_volume.SetDisplayVisibility(1) if currentLayout == 501: layoutManager.setLayout( 502) # switch to dual 3d + red slice layout elif currentLayout == 502: layoutManager.setLayout(6) # switch to red slice only layout else: layoutManager.setLayout( 501) # switch to custom layout with 3d viewer
def setupQuadBufferMode(self): # Create a new layout to get another 3D view layoutName = "QuadBuffered window" layoutLabel = "QB" layoutColor = [0.5, 0.5, 1.0] # This node will manage the view instead of the layout node, so this view can mode freely and have it's own parameters. self.viewOwnerNode = slicer.mrmlScene.AddNewNodeByClass( "vtkMRMLScriptedModuleNode") # Create the node and connect it to the scene/layout/owner node self.viewLogic = slicer.vtkMRMLViewLogic() self.viewLogic.SetMRMLScene(slicer.mrmlScene) self.viewNode = self.viewLogic.AddViewNode(layoutName) self.viewNode.SetLayoutLabel(layoutLabel) self.viewNode.SetLayoutColor(layoutColor) self.viewNode.SetAndObserveParentLayoutNodeID( self.viewOwnerNode.GetID()) # Create a new threeDWidget self.viewWidget = slicer.qMRMLThreeDWidget() ## The surface needs to be stereo enabled to render the object in QB mode, if disabled, the window will accept stereo but won't render correctly #self.format = qt.QSurfaceFormat().setStereo(True) #self.viewWidget.setFormat(format) ## Not only the widget and its surface must be stereo enabled, we also need to state which stereo we want and that the render window is capable of render in QB mode. self.renderWindowQuadBuffer = self.viewWidget.threeDView( ).renderWindow() #self.renderWindowQuadBuffer.SetStereoCapableWindow(1) #Needs the window to be created in stereo capable mode #self.renderWindowQuadBuffer.SetStereoRender(1) #Turned ON enables stereo rendering #self.renderWindowQuadBuffer.SetStereoType(2) #1=crystaleyes, 2=red blue, works on VTK but seems to be uneffective by itself on Slicer, may need to use line 106 """If this turns to 3=quadbuffer, activate QB just as if one clicked on the button on the view controller (means QB is activated AND the correct mode is selected on the view controller) May not be needed if the renderWindowQuadBuffer does the work by itself line 101, to test out when Slicer accepts StereoWidget""" #self.viewNode.SetStereoType(3) # Connect everything to the widget, make it a bit bigger than by default self.viewWidget.setMRMLScene(slicer.mrmlScene) self.viewWidget.setMRMLViewNode(self.viewNode) self.viewWidget.resize(800, 800) # Connects the methods of view the widget to the button, so whenever someone opens the widget and closes it using the classical cross, the widget can be re-opened self.ui.EnableQuadBufferButton.connect('clicked(bool)', self.showQuadBufferWidget)
def TestSection_InitializeTest(self): # # Define variables # # A certain model to test that is both in the brain and the midbrain folders self.testModelNodeName = 'Model_79_left_red_nucleus' self.testModelNode = None self.overrideColor = [255, 255, 0] # Get subject hierarchy node self.shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode( slicer.mrmlScene) self.assertIsNotNone(self.shNode) # Get folder plugin pluginHandler = slicer.qSlicerSubjectHierarchyPluginHandler().instance( ) self.assertIsNotNone(pluginHandler) self.folderPlugin = pluginHandler.pluginByName('Folder') self.assertIsNotNone(self.folderPlugin) # # Initialize test # # Create 3D view self.layoutName = "Test3DView" # ownerNode manages this view instead of the layout manager (it can be any node in the scene) self.viewOwnerNode = slicer.mrmlScene.AddNewNodeByClass( "vtkMRMLScriptedModuleNode") self.viewNode = slicer.vtkMRMLViewNode() self.viewNode.SetName(self.layoutName) self.viewNode.SetLayoutName(self.layoutName) self.viewNode.SetLayoutColor(1, 1, 0) self.viewNode.SetAndObserveParentLayoutNodeID( self.viewOwnerNode.GetID()) self.viewNode = slicer.mrmlScene.AddNode(self.viewNode) self.viewWidget = slicer.qMRMLThreeDWidget() self.viewWidget.setMRMLScene(slicer.mrmlScene) self.viewWidget.setMRMLViewNode(self.viewNode) self.viewWidget.show() # Get model displayable manager for the 3D view self.modelDisplayableManager = self.getModelDisplayableManager() self.assertIsNotNone(self.modelDisplayableManager)
def generatePixmapForPrinting(self, filePath): if not self.getOutputFlattenedModelNode(): raise ValueError("Failed to access flattened baffle model") viewOwnerNode = self.getOutputFlattenedModelNode() if not self.printThreeDViewNode: self.printThreeDViewNode = slicer.mrmlScene.CreateNodeByClass( "vtkMRMLViewNode") self.printThreeDViewNode.UnRegister(None) self.printThreeDViewNode.SetSingletonTag("FlattenedBafflePrinter") self.printThreeDViewNode.SetName("FlattenedBafflePrinter") self.printThreeDViewNode.SetLayoutName("FlattenedBafflePrinter") self.printThreeDViewNode.SetLayoutLabel("FlattenedBafflePrinter") self.printThreeDViewNode.SetLayoutColor(1, 1, 0) self.printThreeDViewNode.SetAndObserveParentLayoutNodeID( viewOwnerNode.GetID()) self.printThreeDViewNode.SetRulerType( slicer.vtkMRMLAbstractViewNode.RulerTypeThin) self.printThreeDViewNode.SetRulerColor( slicer.vtkMRMLAbstractViewNode.RulerColorBlack) self.printThreeDViewNode = slicer.mrmlScene.AddNode( self.printThreeDViewNode) if not self.printThreeDWidget: self.printThreeDWidget = slicer.qMRMLThreeDWidget() self.printThreeDWidget.setObjectName( "self.printThreeDWidget" + self.printThreeDViewNode.GetLayoutLabel()) self.printThreeDWidget.viewLabel = self.printThreeDViewNode.GetLayoutLabel( ) self.printThreeDWidget.viewColor = qt.QColor.fromRgbF( *self.printThreeDViewNode.GetLayoutColor()) self.printThreeDWidget.setMRMLScene(slicer.mrmlScene) self.printThreeDWidget.setMRMLViewNode(self.printThreeDViewNode) self.printThreeDViewNode.SetAndObserveParentLayoutNodeID( viewOwnerNode.GetID()) self.printThreeDWidget.setMRMLViewNode(self.printThreeDViewNode) # Configure view and widget self.printThreeDViewNode.SetBoxVisible(0) self.printThreeDViewNode.SetAxisLabelsVisible(0) self.printThreeDViewNode.SetRenderMode( slicer.vtkMRMLViewNode.Orthographic) self.printThreeDViewNode.SetBackgroundColor((1, 1, 1)) self.printThreeDViewNode.SetBackgroundColor2((1, 1, 1)) # Set color and shading of flattened baffle and fixed points flattenedModelDisplayNode = self.getOutputFlattenedModelNode( ).GetDisplayNode() flattenedModelOriginalColor = flattenedModelDisplayNode.GetColor() flattenedModelDisplayNode.SetColor(0.0, 0.0, 0.0) flattenedFixedPointsNode = self.getOutputFlattenedFixedPointsNode() flattenedFixedPointsDisplayNode = flattenedFixedPointsNode.GetDisplayNode( ) flattenedFixedPointsOriginalColor = flattenedFixedPointsDisplayNode.GetColor( ) flattenedFixedPointsDisplayNode.SetColor(1.0, 0.5, 0.5) # Make sure nothing is visible in the print view other than the baffle and the fixed landmarks hiddenDisplayNodes = [] layoutThreeDViewNode = slicer.app.layoutManager().threeDWidget( 0).viewLogic().GetViewNode() allDisplayNodes = slicer.util.getNodesByClass('vtkMRMLDisplayNode') for displayNode in allDisplayNodes: if displayNode is not flattenedModelDisplayNode and displayNode is not flattenedFixedPointsDisplayNode: displayNode.AddViewNodeID(layoutThreeDViewNode.GetID()) hiddenDisplayNodes.append(displayNode) # Show 3D view self.printThreeDWidget.resize(self.printViewWidth, self.printViewHeight) self.printThreeDWidget.show() # Determine ROI for flattened baffle flattenedBaffePolyData = self.getOutputFlattenedModelNode( ).GetPolyData() bounds = [0] * 6 flattenedBaffePolyData.GetBounds(bounds) center = [(bounds[0] + bounds[1]) / 2.0, (bounds[2] + bounds[3]) / 2.0, (bounds[4] + bounds[5]) / 2.0] # Setup camera cameraPositionOffset = 100.0 cameraNode = slicer.modules.cameras.logic().GetViewActiveCameraNode( self.printThreeDViewNode) cameraNode.SetFocalPoint(center) cameraNode.SetPosition( center[0], center[1], center[2] + bounds[5] - bounds[4] + cameraPositionOffset) cameraNode.SetViewUp(0, 1, 0) cameraNode.GetCamera().SetClippingRange(cameraPositionOffset / 2.0, (bounds[5] - bounds[4]) * 2 + cameraPositionOffset * 2.0) windowSizeInPixels = self.printThreeDWidget.threeDView().renderWindow( ).GetSize() pixelSizeInMm = 25.4 / self.printYResolutionDpi printViewHeightOfViewportInMm = windowSizeInPixels[ 1] * pixelSizeInMm / self.printScale cameraNode.SetParallelScale(printViewHeightOfViewportInMm) threeDView = self.printThreeDWidget.threeDView() renderWindow = threeDView.renderWindow() renderer = renderWindow.GetRenderers().GetFirstRenderer() originalCameraUserTransform = cameraNode.GetCamera().GetUserTransform() originalPixelAspect = renderer.GetPixelAspect() cameraUserTransform = vtk.vtkTransform() cameraUserTransform.Scale( self.printXResolutionDpi / self.printYResolutionDpi, 1.0, 1.0) cameraNode.GetCamera().SetUserTransform(cameraUserTransform) if self.printTransparentBackground: originalAlphaBitPlanes = renderWindow.GetAlphaBitPlanes() renderWindow.SetAlphaBitPlanes(1) originalGradientBackground = renderer.GetGradientBackground() renderer.SetGradientBackground(False) # Render threeDView.forceRender() windowToImage = vtk.vtkWindowToImageFilter() if self.printTransparentBackground: windowToImage.SetInputBufferTypeToRGBA() renderWindow.Render() windowToImage.SetInput(renderWindow) # Write to file with custom DPI # (use Qt file writer to allow saving DPI values) windowToImage.Update() vtkImage = windowToImage.GetOutput() qImage = qt.QImage() slicer.qMRMLUtils().vtkImageDataToQImage(vtkImage, qImage) inchesPerMeter = 1000 / 25.4 qImage.setDotsPerMeterX(self.printXResolutionDpi * inchesPerMeter) qImage.setDotsPerMeterY(self.printYResolutionDpi * inchesPerMeter) imagePixmap = qt.QPixmap.fromImage(qImage) imagePixmap.save(filePath) self.printThreeDWidget.hide() # Restore settings cameraNode.GetCamera().SetUserTransform(originalCameraUserTransform) flattenedModelDisplayNode.SetColor(flattenedModelOriginalColor) flattenedFixedPointsDisplayNode.SetColor( flattenedFixedPointsOriginalColor) for displayNode in hiddenDisplayNodes: displayNode.RemoveAllViewNodeIDs() if self.printTransparentBackground: renderWindow.SetAlphaBitPlanes(originalAlphaBitPlanes) renderer.SetGradientBackground(originalGradientBackground)
# Create a Widget for Right Window # # layout_name_right = "Right_Window" layout_label = "T3" # Create MRML node view_node_right = slicer.vtkMRMLViewNode() view_node_right.SetName(layout_name_right) view_node_right.SetLayoutName(layout_name_right) view_node_right.SetLayoutLabel(layout_label) view_node_right = slicer.mrmlScene.AddNode(view_node_right) view_node_right.SetName(layout_name_right) # Create widget view_widget_right = slicer.qMRMLThreeDWidget() view_widget_right.setMRMLScene(slicer.mrmlScene) view_widget_right.setMRMLViewNode(view_node_right) view_widget_right.setWindowTitle( layout_name_right) # Make sure to name the window view_widget_right.show() # Create a Widget for Right Window # # layoutNameLeft = "Left_Window" # Create MRML node view_node_left = slicer.vtkMRMLViewNode() view_node_left.SetName(layoutNameLeft) view_node_left.SetLayoutName(layoutNameLeft) view_node_left.SetLayoutLabel(layout_label)