[vtkusers] Get a list of selected graph vertices

Eric E. Monson emonson at cs.duke.edu
Sun Sep 19 15:41:59 EDT 2010


This seems to work. Maybe tomorrow I can pop it up on the Wiki, but I have to run right now so I'll just post it here for you to take a look at:


import vtk

source = vtk.vtkRandomGraphSource()
source.Update()

view = vtk.vtkGraphLayoutView()
view.AddRepresentationFromInputConnection(source.GetOutputPort())

def selectionCallback(caller, event):
	# In C++ there is some extra data passed to the callback, but in Python
	# the callback data is lost...
	
	# There should be two selection nodes, but which one is vertices and which is edges
	# does not seem to be guaranteed...
	sel = caller.GetCurrentSelection()
	node0 = sel.GetNode(0)
	node0_field_type = node0.GetFieldType()
	sel_list0 = caller.GetCurrentSelection().GetNode(0).GetSelectionList()
	node1 = sel.GetNode(1)
	node1_field_type = node1.GetFieldType()
	sel_list1 = caller.GetCurrentSelection().GetNode(1).GetSelectionList()
	
	if (sel_list0.GetNumberOfTuples() > 0):
		printFieldType(node0_field_type)
		for ii in range(sel_list0.GetNumberOfTuples()):
			print "\t", sel_list0.GetValue(ii)
	
	if (sel_list1.GetNumberOfTuples() > 0):
		printFieldType(node1_field_type)
		for ii in range(sel_list1.GetNumberOfTuples()):
			print "\t", sel_list1.GetValue(ii)
	
	print "- - -"
	
def printFieldType(field_type):
	if field_type == 3:
		print "Vertices Selected:"
	elif field_type == 4:
		print "Edges Selected:"
	else:
		print "Unknown type:"

rep = view.GetRepresentation(0)

# The vtkRenderedGraphRepresentation should already have a vtkAnnotationLink,
# so we just want to grab it and add an observer with our callback function attached
link = rep.GetAnnotationLink()
link.AddObserver("AnnotationChangedEvent", selectionCallback)

view.GetRenderWindow().SetSize(600, 600)
view.ResetCamera()
view.Render()
view.GetInteractor().Start()



On Sep 19, 2010, at 3:12 PM, Eric E. Monson wrote:

> Hey David,
> 
> In principle, I think you need to get the vtkRenderedGraphRepresentation from the graph layout view with rep = view->GetRepresentation(int), and then from that rep you need to do GetAnnotationLink(). You can set an observer for AnnotationChangedEvent on the annotation link, and then in your callback you can grab the selection list from the link's selection node, and this should contain the selected ids. 
> 
> I think there are some other subtleties necessary, but that should be the basic idea. I'll let you know if I get a Python version working and this should help guide.
> 
> Talk to you later,
> -Eric
> 
> ------------------------------------------------------
> Eric E Monson
> Duke Visualization Technology Group
> 
> 
> On Sep 19, 2010, at 2:35 PM, David Doria wrote:
> 
>>> There are restrictions documented for the vtkHardwareSelector, I am not sure
>>> graph vertices are candidates for selection.
>>> 
>>> Hope that helps,
>>> Jim
>> 
>> I am not sure using the Selector directly is the best way to go - that
>> was just where my search through the code led me. I see now that the
>> Selector is a member of the Renderer, that takes away one level of my
>> mess :)
>> 
>> One of the objects already knows about the selection, because the
>> default behavior is to highlight the selected vertices/edges! All I
>> want to know is the IDs of these things that have been selected. The
>> selection shouldn't have to be performed again.
>> 
>> David
>> _______________________________________________
>> Powered by www.kitware.com
>> 
>> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>> 
>> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>> 
>> Follow this link to subscribe/unsubscribe:
>> http://www.vtk.org/mailman/listinfo/vtkusers
> 
> _______________________________________________
> Powered by www.kitware.com
> 
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
> 
> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
> 
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers




More information about the vtkusers mailing list