PythonOCC getting started guide

Alexander

New member
Hi Quaoar, thanks a lot for these great tutorials. I am looking forward to the next episodes!

It is actually straight forward to get started with PythonOCC, but it requires the conda package manager. Here is a guide:

Get conda package manager
Install the anaconda distribution (individual edition). You can obtain an installer for your OS here. The installation will give you the conda package manager and a base environment containing python together with a lot of commonly used python packages for data science (e.g. numpy, matplotlib and more).

Note:
  • The windows installation wizard will by default not add anaconda to your path (but there is a non-recommended option to do so (which I always use, alternatively you can use the shell coming with anaconda))
  • Depending on your shell/terminal you may have to run some initialization script after installation.

If you don't want the additional bloat (i.e. data science packages) comming with a full anaconda installation (which seems unnecessary if the only purpose is to install PythonOCC), then you may want to install miniconda instead. I imagine the installation procedure is very similar, but I never tried miniconda. From a quick google search it seems like you can get an installer to miniconda here

Obtaining PythonOCC using conda
Open your favorite shell (that has conda in its path), and run the following three commands (you may be prompted for a y/n answer):
  • conda create -n pyoccenv python=3.8
  • conda activate pyoccenv
  • conda install -c conda-forge pythonocc-core

First command can be omitted if you don't want to create a separate environment for PythonOCC. In that case the second command should be changed to 'conda activate base'.

You are now good to go. You can test the installation by activating the environment containing PythonOCC (e.g. 'conda activate base' or 'conda activate pyoccenv') and run
  • python -c "import OCC; print(OCC.VERSION)"


Resources
PythonOCC has a lot of examples in one of their github repos which you might find interesting. You can find them here:

https://github.com/tpaviot/pythonocc-demos

The examples requires a gui backend (for instance PySide2) to be installed in the conda environment containing PythonOCC. One warning, though, I had to make a small change (using PySide2 backend) to get the examples to work (can't remember exactly what, but when having the error message it was quite easy to google).

Alternatives to PythonOCC
PythonOCC seems to have the largest community and a lot of good examples, but in case you are interested there are two other projects (that I know of) that provides python bindings to OpenCascade:
 

Quaoar

Administrator
Staff member
@Alexander wow, that was easy and straightforward, as you promised :D

1626432527133.png

Thanks a lot for this thorough and well-structured memo. I'm sure it deserves a separate thread so that other folks could easily find it. Let me see, if this forum engine allows for such a trick.
 

Quaoar

Administrator
Staff member
So I started off with the following program:

Python:
from OCC.Display.SimpleGui import init_display
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox

display, start_display, add_menu, add_function_to_menu = init_display()
my_box = BRepPrimAPI_MakeBox(10., 20., 30.).Shape()

display.DisplayShape(my_box, update=True)
start_display()

Then installed PySide2 in conda:

Code:
conda install -c conda-forge pyside2

Executing it from the console led to the following error:

(pyoccenv) C:\Users\serge>python c:\Users\serge\Desktop\Untitled-2.py
qt-pyside2 backend - Qt version 5.12.9
####### 3D rendering pipe initialisation #####
Display3d class initialization starting ...
Aspect_DisplayConnection created.
OpenGl_GraphicDriver created.
V3d_Viewer created.
AIS_InteractiveContext created.
V3d_View created
WNT window created.
Traceback (most recent call last):
File "c:\Users\serge\Desktop\Untitled-2.py", line 4, in <module>
display, start_display, add_menu, add_function_to_menu = init_display()
File "C:\Users\serge\.conda\envs\pyoccenv\lib\site-packages\OCC\Display\SimpleGui.py", line 198, in init_display
win.canva.InitDriver()
File "C:\Users\serge\.conda\envs\pyoccenv\lib\site-packages\OCC\Display\qtDisplay.py", line 102, in InitDriver
self._display.Create(window_handle=int(self.winId()), parent=self)
File "C:\Users\serge\.conda\envs\pyoccenv\lib\site-packages\OCC\Display\OCCViewer.py", line 176, in Create
self.Init(self._window_handle)
File "C:\Users\serge\.conda\envs\pyoccenv\lib\site-packages\OCC\Core\Visualization.py", line 171, in Init
return _Visualization.Display3d_Init(self, handle)
RuntimeError: Aspect_GraphicDeviceDefinitionErrorOpenGl_Window::CreateWindow: SetPixelFormat failed. Error code: 2000 raised from method Init of class Display3d
That's likely what @Alexander mentioned at the end. Thomas Paviot's answer regarding this issue is here. Following his advice, I changed the class name from `QtOpenGL.QGLWidget` to `QtWidgets.QWidget` in the file qtDisplay.py buried somewhere in local profile (I just used file search to find it):

1626434320731.png

And here we go!

1626434355986.png

Personally, I've never used Python and this whole thing is kinda completely new to me. But it's very satisfying to see how easily one could get started.
 

rpurohit15

New member
Hi Quaoar, thanks a lot for these great tutorials. I am looking forward to the next episodes!

It is actually straight forward to get started with PythonOCC, but it requires the conda package manager. Here is a guide:

Get conda package manager
Install the anaconda distribution (individual edition). You can obtain an installer for your OS here. The installation will give you the conda package manager and a base environment containing python together with a lot of commonly used python packages for data science (e.g. numpy, matplotlib and more).

Note:
  • The windows installation wizard will by default not add anaconda to your path (but there is a non-recommended option to do so (which I always use, alternatively you can use the shell coming with anaconda))
  • Depending on your shell/terminal you may have to run some initialization script after installation.

If you don't want the additional bloat (i.e. data science packages) comming with a full anaconda installation (which seems unnecessary if the only purpose is to install PythonOCC), then you may want to install miniconda instead. I imagine the installation procedure is very similar, but I never tried miniconda. From a quick google search it seems like you can get an installer to miniconda here

Obtaining PythonOCC using conda
Open your favorite shell (that has conda in its path), and run the following three commands (you may be prompted for a y/n answer):
  • conda create -n pyoccenv python=3.8
  • conda activate pyoccenv
  • conda install -c conda-forge pythonocc-core

First command can be omitted if you don't want to create a separate environment for PythonOCC. In that case the second command should be changed to 'conda activate base'.

You are now good to go. You can test the installation by activating the environment containing PythonOCC (e.g. 'conda activate base' or 'conda activate pyoccenv') and run
  • python -c "import OCC; print(OCC.VERSION)"


Resources
PythonOCC has a lot of examples in one of their github repos which you might find interesting. You can find them here:

https://github.com/tpaviot/pythonocc-demos

The examples requires a gui backend (for instance PySide2) to be installed in the conda environment containing PythonOCC. One warning, though, I had to make a small change (using PySide2 backend) to get the examples to work (can't remember exactly what, but when having the error message it was quite easy to google).

Alternatives to PythonOCC
PythonOCC seems to have the largest community and a lot of good examples, but in case you are interested there are two other projects (that I know of) that provides python bindings to OpenCascade:
I started with pyocct and pyqt5 but had some dependency errors while setting up conda environment. Then I tried pythonocc with pyside2. I could see most of the demo scripts mentioned in the above directory are working well. Thanks for the post.
 

Jojain

New member
About python bindings for OCCT, there is the Cadquery project that is using the OCP bindings and implement an high level API on top of OCCT, it may be interesting for some of you that want inspiration.
 
Top