Difference between revisions of "Slicer3:Python:ScriptedActor"
(New page: After a conversation with Gordon Kindlmann, I (Steve Pieper) decided to try an experiment... == Question == Can python be used to tie non-VTK-aware OpenGL C++ code into VTK? == Answer =...) |
(→Answer) |
||
Line 6: | Line 6: | ||
== Answer == | == Answer == | ||
+ | [[Image:VtkOpenGLScriptedActor.png|thumb|300px|right]] | ||
I [http://viewvc.slicer.org/viewcvs.cgi?rev=9422&view=rev checked in some code] to make this possible. The C++ class, vtkOpenGLScriptedActor, allows you to specify a script that should be executed inside the VTK render process. This calls out to python, where arbitrary code can be run (something like a callback). | I [http://viewvc.slicer.org/viewcvs.cgi?rev=9422&view=rev checked in some code] to make this possible. The C++ class, vtkOpenGLScriptedActor, allows you to specify a script that should be executed inside the VTK render process. This calls out to python, where arbitrary code can be run (something like a callback). | ||
Line 13: | Line 14: | ||
The result is shown in the image to the right. | The result is shown in the image to the right. | ||
− | + | == Considerations == | |
+ | |||
+ | Making individual vertex calls through python is not advised for efficiency, however that doesn't mean this is simply a demo. In particular, there are some ways this could be applied: | ||
+ | |||
+ | * OpenGL is moving to Vertex Buffer Objects and these are interoperable with numpy arrays, so large amounts of data can be manipulated with single calls. | ||
+ | * The python code can invoke arbitrary C or C++ code through various mechanisms. If this code isn't VTK-aware, then the python code may be able to provide the needed compatibility glue. |
Revision as of 18:23, 7 May 2009
Home < Slicer3:Python:ScriptedActorAfter a conversation with Gordon Kindlmann, I (Steve Pieper) decided to try an experiment...
Question
Can python be used to tie non-VTK-aware OpenGL C++ code into VTK?
Answer
I checked in some code to make this possible. The C++ class, vtkOpenGLScriptedActor, allows you to specify a script that should be executed inside the VTK render process. This calls out to python, where arbitrary code can be run (something like a callback).
The example script shows how to draw some simple OpenGL shapes in the same window with a VTK managed sphere. The example relies on having PyOpenGL installed in Slicer's python.
The result is shown in the image to the right.
Considerations
Making individual vertex calls through python is not advised for efficiency, however that doesn't mean this is simply a demo. In particular, there are some ways this could be applied:
- OpenGL is moving to Vertex Buffer Objects and these are interoperable with numpy arrays, so large amounts of data can be manipulated with single calls.
- The python code can invoke arbitrary C or C++ code through various mechanisms. If this code isn't VTK-aware, then the python code may be able to provide the needed compatibility glue.