Bug and customization of CameraOrientationWidget

Hi all!

I wanted to add a CameraOrientationWidget to my app because it seems to be a very handful widget but I’m facing two problems that I wanted to expose you.

  • First, there is an annoying behavior when choosing one direction : for example if you click on “+X” the view is well aligned along “+X” direction, but then if you rotate the view and click again on the “+X” the view is then aligned along the “-X” direction. It seems that the widget keeps in mind that the last clicked was the “+X” and then a new click on X leads to the opposite direction, even if the view have been rotated before.
  • Secondly, it would be great to be able to customize some parameters of the widget, like the colors, the font, etc… Some parameters are available in the vtkCameraOrientationRepresentation but for colors for example they seems not easily accessible.

Maybe one may have some solutions for that? Or maybe my thoughts will orient some future improvement :slight_smile:

I adapted the CameraOrientationWidget before it was accepted by VTK. I can recommend that you make a clone-and-own of the class and add properties for changing the 6 labels. To address the annoying behavior you can look into the variable LastPickedDir. I believe the intention is that it should be easy to flip the orientation without the need to spin-the-ball. If the directions -X and +X sufficiently overlap and +X was picked last, -X will be picked if you click +X.

Thank you for your reply and suggestions!

What you recommend is to modify the class by myself. This is a good idea but as I use a python version of vtk I suppose it will require that I setup a full compilation chain of vtk (a bit overkill for me am afraid :slight_smile: ). Do you know if it is possible to modify such a class easily in python?

Another problem I encountered with this widget is that if I make some changes on the parameters, for example with a self.CameraOrientationWidget.GetRepresentation().AnchorToUpperLeft() the changes are only taken into account when I resize the window. I tried to force an update with some actions like self.GetRenderWindow().Render() without success :confused: do you have any idea for that?


You are right. It is pretty easy to download the VTK repository and build and install it with python support.

Modifying the vtkCameraOrientationWidget in python is very difficult - more difficult than creating a new from scratch. The developers of vtkCameraOrientationWidget has not exposed a method for changing the representation, vtkCameraOrientationWidget::SetRepresentation, so it is not enough to implement a new representation (which is also non-trivial).

If you really want to fix these issues, I suggest you modify the existing implementation and compile your own VTK with python support.

With respect to the update, I believe that figuring out the bounds and calling PlaceWidget(bounds) on the representation should do the trick.


Thank you for your reply, I will try to do as you suggest this will be a big first for me :slight_smile:
For the graphic update I found that calling the SquareResize() function do the job…

Since our last discussion, and before to make any modification in the widget, I tried to build a clean vtk from scratch. After quite a lot of tries I think I am close to the end… but need your help for the last step :wink:

Here is what I did so far (for information I’m working from a fresh AWS EC2 virtual machine under windows), my objective is to build a python wheel:

  • Install git
  • Install Visual Studio Community (Desktop development with C++)
  • Install Python 3.8 (and install wheel with pip)
  • Install CMake


  • Download vtk sources from github
  • Launch a command line “x64 native tools command prompt”
  • From this command line launch cmake-gui
  • In cmake-gui, setup the source and build folders and then “Configure” and modify those options:
    VTK_WRAP_PYTHON : true
    VTK_WHEEL_BUILD : true
  • Then “Generate” and after all ok “Open Project”
  • Then under Visual Studio, right click on ALL_BUILD and after a lonnng time I have a “382 succeede, 0 failed” :slight_smile:

But then I tried to build a python wheel with the command “python setup.py bdist_wheel” and after a while here is my error:

running build_ext
Microsoft (R) Build Engine version 17.1.0+ae57d105c for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.CppBuild.targets(437,5): er
ror MSB8013: This project doesn't contain the Configuration and Platform combination of Debug|x64. [C:\vtk\build\ZERO_C
Traceback (most recent call last):
  File "setup.py", line 139, in <module>
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\setuptools\__init__.py", line 153, in setup
    return distutils.core.setup(**attrs)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\distutils\core.py", line 148, in setup
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\distutils\dist.py", line 966, in run_commands
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\distutils\dist.py", line 985, in run_command
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\wheel\bdist_wheel.py", line 299, in run
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\distutils\cmd.py", line 313, in run_command
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\distutils\dist.py", line 985, in run_command
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\distutils\command\build.py", line 135, in run
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\distutils\cmd.py", line 313, in run_command
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\distutils\dist.py", line 985, in run_command
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\setuptools\command\build_ext.py", line 79, in run
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\distutils\command\build_ext.py", line 340, in run
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\distutils\command\build_ext.py", line 449, in build_extensions
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\distutils\command\build_ext.py", line 474, in _build_extensions_serial
  File "setup.py", line 37, in build_extension
    subprocess.check_call([CMAKE_EXE, '--build', BUILD_DIR, '--target', ext.target],
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 364, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['C:\\Program Files\\CMake\\bin\\cmake.EXE', '--build', 'C:\\vtk\\build', '--target', 'vtkCommonCorePython']' returned non-zero exit status 1.

So far I didn’t manage to go further… That’s why I am calling for your help :slight_smile:

But maybe I don’t have to do so much, as I just want to modify the cameraorientationwidget isn’t there a way to build only this file and replace it directly under my python vtk installation?

Thanks in advance for your help, sorry for the long message :slight_smile:

I haven’t done this myself in a while, but I believe that in addition to telling CMake that you want a release configuration you also need to change VisualStudio to Release mode (using a menu on the toolbar). HTH.

Using Visual Studio, it has to be a Release build. I am not sure why you try to build wheel. What I did was simple to copy the python build from VTK into either a virtual environment or the system python installation. You will need to put it into the site-packages folder like the one you get from pip install vtk. You also need to copy the .dlls from your VTK build next to the .pyd files. Otherwise you need to add the path to the location of the .dlls and also figure out to issue an os.add_dll_directory to the location of the .dlls (from VTK). This last thing is new to Python 3.8 and newer.

I wanted to build a wheel just because it would be easier to install the new build in other python.

But following your instructions I manage to copy the modified build in my python installation and it works!
So I manage to do the color modifications and deactivate the bad +/- behaviour as I wanted :grinning:

Finally my comment on this widget :

  • I continue to think that it could be a great idea to allow the user to modify at least the colors natively.
  • The direction flip behaviour is also a good idea but on my side at least it doesn’t works well.
    *Another idea could be also to align the view not always in the same orientation but on the closest of the current one. I mean, currently when you click on +X the view is always aligned with +Z upward and +Y rightward. What I think could be great is, if when you click on the +X you are already in an orientation with +Y quite upward and +Z quite leftward, then the widget should align in this orientation with +Y upward and +Z leftward. Maybe this behaviour could be user selected.

Thanks for all!