New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PythonSlicer problem with vtk and ReadData #6484
Comments
Using Slicer 5.1.0-2022-07-26 (Revision 30196) and also latest Slicer stable 5.0.3 (Revision 30893) I'm not able to replicate these issues when using the python interactor from inside Slicer. I downloaded both of these versions from https://download.slicer.org/. Notably this was on Windows 11, but I don't see why Windows 10 would cause this issue for you, but likely due to the environment of PythonSlicer.exe. |
https://discourse.slicer.org/t/how-to-import-slicer-in-python-cli/8083/3?u=jamesobutler
|
Importing the python-wrapped VTK C++ code can be tricky - if they aren't imported in the correct order then the methods may not be available and sometimes you need to import the libraries directly. This probably hasn't been looked at before for the PythonSlicer environment with the new VTK. I'm guessing it is solvable with a script to manage the import process or some change in the way they are packaged. |
Thanks, @pieper your comment lead me to try different import orders and it seems that it works if I import vtk first and everything in the same line: import vtk, slicer, slicer.util, mrml
A = slicer.vtkMRMLNRRDStorageNode()
A.SetFileName("foo")
n = mrml.vtkMRMLScalarVolumeNode()
A.ReadData(n) The error now is relative to the incorrect filename "foo" but if I select a valid file, everything seems to work and ReadData returns 1. GetClassName also works |
My understanding is that the very first time you instantiate a Python-wrapped VTK class, the wrapper traverses up the class hierarchy to determine the base classes of the object. This is done only once, at the first instantiation. If at that point you haven't imported all the base classes then you'll never have full hierarchy information for that instantiated class. Therefore, you need to import vtk before you instantiate VTK-based classes. I've checked and calling |
Thanks @lassoan, it is indeed the import order like I've described above. Since it was working on Slicer 4 without importing vtk, I was thinking it could be a bug. Anyways, this could serve as a reference for people that bumps into the same issue. |
Since many parts of Slicer (all module logics, PythonQt, etc.) are loaded by the Slicer application, currently it is quite limited what can be done in a Python environment that is not created by the Slicer application. That's why we don't put too much effort into documenting this. But in the long term we want to make more and more parts of Slicer usable without the Slicer application (starting with MRML, DICOM, module logic classes). So, it is good that you explore this and we would be open to making improvements that helps moving in this direction. |
Fix DWI shell extraction module import sorting: follow the class hierarchy and library dependencies. Fixes: ``` ``` raised for example in. Related discussions: Slicer/Slicer#6484 https://discourse.slicer.org/t/extension-tests-fail-on-cdash-why/26571/13
Fix DWI shell extraction module import sorting: sort the imported packages following the class hierarchy and library dependencies. Fixes: ``` loading: /SlicerDMRI/SlicerDMRI/Testdata/3x3x3_13_b1000_b3000.nrrd Traceback (most recent call last): File "/SlicerDMRI-build/inner-build/lib/Slicer-5.5/cli-modules/ExtractDWIShells.py", line 207, in <module> main() File "/SlicerDMRI-build/inner-build/lib/Slicer-5.5/cli-modules/ExtractDWIShells.py", line 127, in main sn.ReadData(node_in) TypeError: ReadData argument 1: method requires a VTK object ``` raised for example at: https://github.com/SlicerDMRI/SlicerDMRI/actions/runs/6722459285/job/18270424711?pr=191#step:8:2631 Related discussion: Slicer/Slicer#6484
Fix DWI shell extraction module import sorting: sort the imported packages following the class hierarchy and library dependencies. Fixes: ``` loading: SlicerDMRI/SlicerDMRI/Testdata/3x3x3_13_b1000_b3000.nrrd Traceback (most recent call last): File "SlicerDMRI-build/inner-build/lib/Slicer-5.5/cli-modules/ExtractDWIShells.py", line 207, in <module> main() File "SlicerDMRI-build/inner-build/lib/Slicer-5.5/cli-modules/ExtractDWIShells.py", line 127, in main sn.ReadData(node_in) TypeError: ReadData argument 1: method requires a VTK object ``` raised for example at: https://github.com/SlicerDMRI/SlicerDMRI/actions/runs/6722459285/job/18270424711?pr=191#step:8:2631 Related discussion: Slicer/Slicer#6484
The code block below reports an error when run with PythonSlicer.exe on Windows 10
TypeError: ReadData argument 1: method requires a VTK object
This occurred on Slicer 5 nightly build 31096
I suspect this issue is also affecting SlicerCAT built custom apps CLIs, since my custom app clis are not being able to ReadData() as well.
The code block above used to work on Slicer 4.
Another issue:
AttributeError: 'MRMLCore.vtkMRMLScalarVolumeNode' object has no attribute 'GetClassName'
The text was updated successfully, but these errors were encountered: