CadQuery / OCP

Apache License 2.0
86 stars 28 forks source link

Installation Order - OCP with VTK and PyVista [pip specific issues] #119

Open akaszynski opened 11 months ago

akaszynski commented 11 months ago

Ran into an issue recently where tessellation to VTK wasn't working:

import cadquery as cq
import OCP

shape = cq.Workplane().box(1, 1, 1).faces(">Z").hole(0.7).edges().fillet(0.03).val()

vs = OCP.IVtkOCC.IVtkOCC_Shape(shape.wrapped)
sd = OCP.IVtkVTK.IVtkVTK_ShapeData()
sm = OCP.IVtkOCC.IVtkOCC_ShapeMesher()
sm.Build(vs, sd)

pdata = sd.getVtkPolyData()
print(pdata)

Where pdata was output as None. Same issue using the more concise:

import cadquery as cq

shape = cq.Workplane().box(1, 1, 1).faces(">Z").hole(0.7).edges().fillet(0.03).val()
pdata = shape.toVtkPolyData()

After a bit of debugging, turns out the issue will occur if you install vtk with:

pip install vtk==9.2.5.

Since OCP installs the vtkmodules directory, this is silently overwritten when installing vtk.

This is an issue on my end since I'm using cadquery and I'd like to also plot the resulting CAD using pyvista, which depends on the vtk package. The only way I can think to work around this is to install pyvista with no dependencies. If anyone wishes to plot cadquery shapes using pyvista, here's a minimum working example:

pip install cadquery

Followed by installing pyvista without VTK

pip install pyvista --no-deps
pip install scooby matplotlib pooch

This works and the plots look great!

import pyvista as pv
import cadquery as cq

shape = cq.Workplane().box(1, 1, 1).faces(">Z").hole(0.7).edges().fillet(0.03).val()
pdata = pv.wrap(shape.toVtkPolyData())
pdata.plot()

tmp


Ask for the maintainers: Is there any way for OCP to use a pre-compiled version of VTK? Is there any other way of working around this issue?

whophil commented 11 months ago

@akaszynski - are you installing OCP via conda?

akaszynski commented 11 months ago

This is all just from good old pip.

whophil commented 11 months ago

@akaszynski I see. This may not be an adequate solution for you, but this should "just work" if you use conda. I certainly understand the many reasons you might not want to use conda.

This seems to be mainly an issue with the wheels, whose builds are handled in a separate project - https://github.com/CadQuery/ocp-build-system/.

Cross-referencing your issue there (https://github.com/CadQuery/ocp-build-system/issues/14)

akaszynski commented 11 months ago

Yep, I recall opening that there, but reposted here so that others could know there's a solution.

Sadly, conda isn't an option for me at this point due to issues repackaging with pyinstaller. One solution I thought of was creating a simple fork of pyvista called pyvista-no-vtk that installs pyvista without installing vtk. PyVista (for my use case) still works out of the box since we use only vtkmodules.

akaszynski commented 11 months ago

Here's another (albeit still non-ideal) hack. Install everything normally and then proceed to reinstall cadquery after deleting the vtkmodules directory:

pip install pyvista cadquery
rm -rf ~/.venv311/lib/python3.11/site-packages/vtkmodules  # will need to modify the directory
pip install cadquery --force-reinstall
adam-urbanczyk commented 11 months ago

Yep, I recall opening that there, but reposted here so that others could know there's a solution.

Thanks for sharing, noted. Still, the advised installation method is conda.

Sadly, conda isn't an option for me at this point due to issues repackaging with pyinstaller. One solution I thought of was creating a simple fork of pyvista called pyvista-no-vtk that installs pyvista without installing vtk. PyVista (for my use case) still works out of the box since we use only vtkmodules.

There should not be any fundamental issue pyinstaller and conda. BTW: recently for CQ-editor, we switched to constructor. Maybe something to try?