def onCentrar(self): volumenCentrar = self.imagenSelector.currentNode() lm = slicer.app.layoutManager() origenVolumen = volumenCentrar.GetOrigin() volumeLogic = slicer.vtkSlicerVolumesLogic() origenCentro = [0, 0, 0] volumeLogic.GetVolumeCenteredOrigin(volumenCentrar, origenCentro) traslacion = [0, 0, 0] volumenCentrar.SetOrigin(origenCentro) lm.resetSliceViews() for i in range(3): traslacion[i] = origenCentro[i] - origenVolumen[i] T = slicer.vtkMRMLTransformNode() I = slicer.vtkMRMLTransformNode() transmatrix = vtk.vtkMatrix4x4() transform = vtk.vtkTransform() T.SetAndObserveTransformToParent(transform) T.SetName('centrarTransformacionSeeg') I.SetName('CentrarTInversaSeeg') transmatrix.DeepCopy((1, 0, 0, traslacion[0], 0, 1, 0, traslacion[1], 0, 0, 1, traslacion[2], 0, 0, 0, 1)) transform.SetMatrix(transmatrix) inv = transform.GetInverse() I.SetAndObserveTransformToParent(inv) slicer.mrmlScene.AddNode(T) slicer.mrmlScene.AddNode(I)
def build_xml_scene(self, nodeName): ''' obs - list of slicer objects file - filepath to write the xml builds the XML scene of all object in self.SlicerSelectedModelsList ''' x_scene = Element('scene') s_scene = slicer.mrmlScene if slicer.util.getNode(nodeName) is not None: model = slicer.util.getNode(nodeName) try: transform = slicer.util.getNode(model.GetName()+'_trans') except slicer.util.MRMLNodeNotFoundException: transform = None if not transform: transform = slicer.vtkMRMLTransformNode() transform.SetName(model.GetName()+'_trans') s_scene.AddNode(transform) model.SetAndObserveTransformNodeID(transform.GetID()) xob = SubElement(x_scene, 'b_object') xob.set('name', model.GetName()) my_matrix = transform.GetMatrixTransformFromParent() xmlmx = self.matrix_to_xml_element(slicer.util.arrayFromVTKMatrix(my_matrix)) xob.extend([xmlmx]) return x_scene
def generate3DVisualisationNode(self, polydata, name, color=(1, 1, 1), initial_pos_x=0): #create Model Node shape_node = slicer.vtkMRMLModelNode() shape_node.SetAndObservePolyData(polydata) shape_node.SetName(name) #create display node model_display = slicer.vtkMRMLModelDisplayNode() model_display.SetColor(color[0], color[1], color[2]) model_display.AutoScalarRangeOff() model_display.SetScene(slicer.mrmlScene) model_display.SetName("Display " + name) #create transform node transform = vtk.vtkTransform() transform.Translate(initial_pos_x, 0, 0) transform_node = slicer.vtkMRMLTransformNode() transform_node.SetName("Translation " + name) transform_node.SetAndObserveTransformToParent(transform) #Add Nodes to Slicer slicer.mrmlScene.AddNode(transform_node) slicer.mrmlScene.AddNode(model_display) slicer.mrmlScene.AddNode(shape_node) #Link nodes shape_node.SetAndObserveTransformNodeID(transform_node.GetID()) shape_node.SetAndObserveDisplayNodeID(model_display.GetID())
def CreateNewTransform(self, trial_num, numSamp): transformNode = slicer.vtkMRMLTransformNode() slicer.mrmlScene.AddNode(transformNode) transformNode.CreateDefaultStorageNode() transform_name = 'Transform_trial_'+str(trial_num)+'_nsamp_'+str(numSamp) transformNode.SetName(transform_name) return transformNode
def setUp(self): """ Do whatever is needed to reset the state - typically a scene clear will be enough. """ slicer.mrmlScene.Clear(0) self.transformNode1 = slicer.vtkMRMLTransformNode() self.transformNode1.SetName("Transform 1") slicer.mrmlScene.AddNode(self.transformNode1) self.transformNode2 = slicer.vtkMRMLTransformNode() self.transformNode2.SetName("Transform 2") slicer.mrmlScene.AddNode(self.transformNode2) self.transformNode3 = slicer.vtkMRMLTransformNode() self.transformNode3.SetName("Transform 3") slicer.mrmlScene.AddNode(self.transformNode3) self.delayMs = 700
def update_scene(self, xml): if not self.watching: return try: #any better ideas?? tree = ET.ElementTree(ET.fromstring(xml)) except: return x_scene = tree.getroot() s_scene = slicer.mrmlScene #scene = slicer.mrmlScene for b_ob in x_scene: #get the name of blender object name = b_ob.get('name') xml_mx = b_ob.find('matrix') try: slicer_model = slicer.util.getNode(name) except slicer.util.MRMLNodeNotFoundException: slicer_model = None return #if not slicer_model: #try to get transform node try: transform = slicer.util.getNode(name+'_trans') except slicer.util.MRMLNodeNotFoundException: transform = None if not transform: transform = slicer.vtkMRMLTransformNode() transform.SetName(name+'_trans') s_scene.AddNode(transform) slicer_model.SetAndObserveTransformNodeID(transform.GetID()) #set the elements of transform form the matrix #my_matrix = vtk.vtkMatrix4x4() my_matrix = transform.GetMatrixTransformFromParent() for i in range(0,4): for j in range(0,4): my_matrix.SetElement(i,j,float(xml_mx[i][j].text)) #update object location in scene transform.SetAndObserveMatrixTransformToParent(my_matrix) #update color if b_ob.find("material"): mat_color = b_ob.find('material') slicer_model.GetDisplayNode().SetColor(float(mat_color.find('r').text), float(mat_color.find('g').text), float(mat_color.find('b').text))
def onHelloWorldButtonClicked(self): #find the temp directory self_dir = os.path.dirname(os.path.abspath(__file__)) tmp_dir = os.path.join(self_dir, "tmp") print(tmp_dir) if not os.path.exists(tmp_dir): print('there is no temporary directory') print('there needs to be a folder named "tmp"') print('it should be in same folder with this script') print('this script is located here') print(self_dir) return scene_file = os.path.join(tmp_dir, "blend_to_slicer.xml") print(scene_file) if not os.path.exists(scene_file): print('NO XML FILE IN THE TEMP DIRECOTRY') print("Export scene from Blender") print("it needs to be stored in the following directory") print(tmp_dir) return my_file = open(scene_file) tree = ET.parse(my_file) x_scene = tree.getroot() my_file.close() s_scene = slicer.mrmlScene #scene = slicer.mrmlScene for b_ob in x_scene: #get the name of blender object name = b_ob.get('name') #check if there is the same sicer model in the scene slicer_model = slicer.util.getNode(name) if not slicer_model: ob_file = os.path.join(tmp_dir, name + ".ply") if os.path.exists(ob_file): print(name + ' loading model from .ply') ret = slicer.util.loadModel(ob_file) if not ret: print('could not load the ply model') slicer_model = slicer.util.getNode(name) if not slicer_model: continue disp_node = slicer_model.GetDisplayNode() disp_node.SetSliceIntersectionVisibility(True) disp_node.SetSliceIntersectionThickness(2) mat = b_ob.find('material') if mat is not None: r, g, b = mat.find('r').text, mat.find( 'g').text, mat.find('b').text disp_node.SetColor(float(r), float(g), float(b)) else: continue else: print(name + ' model exists in scene already') xml_mx = b_ob.find('matrix') #try to get transform node transform = slicer.util.getNode(name + '_trans') if not transform: transform = slicer.vtkMRMLTransformNode() transform.SetName(name + '_trans') s_scene.AddNode(transform) slicer_model.SetAndObserveTransformNodeID(transform.GetID()) #set the elements of transform form the matrix #my_matrix = vtk.vtkMatrix4x4() my_matrix = transform.GetMatrixTransformFromParent() for i in range(0, 4): for j in range(0, 4): my_matrix.SetElement(i, j, float(xml_mx[i][j].text)) #update object location in scene transform.SetAndObserveMatrixTransformToParent(my_matrix)
def do_correctionsClicked(self): # Copy and paste into Slicer Python interactor # Drag visible fiducials to deform transform # Scale defines how large cube will be created # numPerEdge defines how many fiducials to put on each edge of the cube size = 0 scalex = 10.0 scaley = 10.0 scalez = 10.0 numPerEdge = size / 10 # Create the FROM fiducial list, and hide it so it doesn't change with mouse interactions slicer.mrmlScene.AddNode(self.toFids) self.toFids.Copy(self.fromFids) self.toFids.SetName('toFids') self.toFids.GetDisplayNode().SetVisibility(True) # Create the transform node to hold the deformable transformation later try: slicer.mrmlScene.RemoveNode(slicer.util.getNode("TpsTransform")) tNode = slicer.vtkMRMLTransformNode() tNode.SetName('TpsTransform') slicer.mrmlScene.AddNode(tNode) except: print("creating new TpsTransform") tNode = slicer.vtkMRMLTransformNode() tNode.SetName('TpsTransform') slicer.mrmlScene.AddNode(tNode) # Function that will be called whenever a TO fiducial moves and the transform needs an update def updateTpsTransform(caller, eventid): numPerEdge = self.fromFids.GetNumberOfFiducials() if numPerEdge != self.toFids.GetNumberOfFiducials(): print 'Error: Fiducial numbers are not equal!' return fp = vtk.vtkPoints() tp = vtk.vtkPoints() f = [0, 0, 0] t = [0, 0, 0] for i in range(numPerEdge): self.fromFids.GetNthFiducialPosition(i, f) self.toFids.GetNthFiducialPosition(i, t) fp.InsertNextPoint(f) tp.InsertNextPoint(t) tps = vtk.vtkThinPlateSplineTransform() tps.SetSourceLandmarks(fp) tps.SetTargetLandmarks(tp) tps.SetBasisToR() tNode.SetAndObserveTransformToParent(tps) self.toFids.AddObserver(vtk.vtkCommand.ModifiedEvent, updateTpsTransform) # A ROI annotation defines the region where the transform will be visualized roi = self.inputSelector1.currentNode() slicer.mrmlScene.AddNode(roi) roi.SetDisplayVisibility(True) # Set up transform visualization as gridlines tNode.CreateDefaultDisplayNodes() d = tNode.GetDisplayNode() d.SetAndObserveRegionNode(roi) d.SetVisualizationMode( slicer.vtkMRMLTransformDisplayNode.VIS_MODE_GRID) d.SetVisibility(False) roi.SetAndObserveTransformNodeID(tNode.GetID()) TransformNode = self.inputSelector4.currentNode() tNode.SetAndObserveTransformNodeID(TransformNode.GetID())