Showing you my setup for Python scripting in FreeCAD with Pyzo IDE

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
freedman
Veteran
Posts: 3467
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: Showing you my setup for Python scripting in FreeCAD with Pyzo IDE

Post by freedman »

bdm, do you know if Pyzo has TCP in it or I guess have you searched the source? Have you seen it make an internet connection?
Thanks
bdm
Posts: 180
Joined: Sat Dec 31, 2022 12:10 pm

Re: Showing you my setup for Python scripting in FreeCAD with Pyzo IDE

Post by bdm »

freedman wrote: Fri May 05, 2023 5:53 pm bdm, do you know if Pyzo has TCP in it or I guess have you searched the source? Have you seen it make an internet connection?
Thanks
Pyzo opens a local TCP connection to its own threads, but this is normal - see https://github.com/pyzo/pyzo/blob/main/ ... andline.py for example.

I have had a look at quite a large junk of the Pyzo codebase within the last year, to fix some bugs and to add little features, and also because of curiosity of course. The code base is quite small, is fully written in Python and has no dependencies except PyQt5 or PySide2.
I have never encountered anything malicious. There is no data collection and no data transmission.

Of course, if you select the menu entry "Help -> Check for updates" Pyzo will retrieve the newest version number on the github site but without transmitting your data - see https://github.com/pyzo/pyzo/blob/main/ ... u.py#L2317. And if you select "Help -> Pyzo website" it will open your default web browser to go to that site.
There is "Tools -> Web browser" which is just an independent browser widget not communicating with the rest of the application, I guess similar to FreeCAD's Web Workbench - see https://github.com/pyzo/pyzo/blob/main/ ... Browser.py if you are interested.

You even do not need to obtain the binaries to run Pyzo. Just run "python3 -m pip install pyzo" or download the source ZIP archive on github.com and execute it with your existing python environment (with "import pyzo; pyzo.start()"). See https://github.com/pyzo/pyzo/blob/main/ ... her.py#L13 for more information.

I am not affiliated to neither the Pyzo nor the FreeCAD project. I am just a happy user that has contributed a few Pull Requests to the Pyzo Github repository. Feel free to ask more if you have questions.
User avatar
jonasb
Posts: 162
Joined: Tue Dec 22, 2020 7:57 pm

Re: Showing you my setup for Python scripting in FreeCAD with Pyzo IDE

Post by jonasb »

This is pretty cool, thanks for sharing!

On a Mac, using the weekly build from yesterday, i.e. 0.21.0R33071 which does include Werner's patch, this config works for debugging:

exe: /Applications/FreeCAD.app/Contents/Resources/bin/python
gui: PySide2
pythonPath: /Applications/FreeCAD.app/Contents/Resources/lib
environ: LC_NUMERIC=en_US.UTF-8
startupScript: ([x] Code to run at startup)

Code: Select all

import FreeCAD # has to come first, as it sets additional sys.path entries!
import FreeCADGui
from PySide import QtCore, QtWidgets
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_ShareOpenGLContexts, True)
# AFTER_GUI - code below runs after integrating the GUI
FreeCADGui.showMainWindow()
However, the tool bar icons appear too large and playing around with the AA_-attribtues did not help. The rest of the GUI looks fine, though, and playing with the env var QT_SCREEN_SCALE_FACTORS makes things only worse.

Also note that one has to open files using pyzo's file browser widget in order to set breakpoints. Using "file -> open" pops up macOS' file dialog, which does not allow browsing inside application bundles. And just using the build-in breakpoint() method makes the whole shell hang -- at least in my setup here.
bdm
Posts: 180
Joined: Sat Dec 31, 2022 12:10 pm

Re: Showing you my setup for Python scripting in FreeCAD with Pyzo IDE

Post by bdm »

jonasb wrote: Fri May 05, 2023 9:53 pm ... However, the tool bar icons appear too large ...
I have the same problem in Linux. My workaround is: open the preferences dialog in FreeCAD and just press the "OK" button. Somehow the smaller icon size setting is correctly displayed in the preference dialog, but it takes a press on "OK" or "Apply" to update the toolbar icon size.

Regarding opening files:
Another way to open files in Pyzo is to type a magic command in the python shell, for example

Code: Select all

open "/tmp/aa.py"
(requires single or double quotes around the filepath). That also works for relative paths. The current directory can be displayed by typing just the "cd" magic command in the python shell, and by typing

Code: Select all

cd somefoldername
you can change that directory.
You could also try to just drag'n'drop the file from an external file browser to Pyzo's editor widget, if that is possible in macOS.
heron
Posts: 307
Joined: Mon Apr 20, 2020 5:32 pm

Re: Showing you my setup for Python scripting in FreeCAD with Pyzo IDE

Post by heron »

bdm wrote: Mon May 01, 2023 9:55 am So I am just sharing this -- maybe that will also make life easier for you. Feedback is welcome.
Hello @bdm, many thanks for sharing this!

I would like to try pyzo later and while reading the thread a question came up:
After wmayer's commit, is needed or inconvenient to follow the instructions: :?:

Code: Select all

Modify one line in the Pyzo source:
    pyzo/pyzokernel/interpreter.py
            if sys.stdin.closed or not self.context.connection_count:
            -->
            if getattr(sys.stdin, 'closed', False) or not self.context.connection_count: # for FreeCAD
 
bdm
Posts: 180
Joined: Sat Dec 31, 2022 12:10 pm

Re: Showing you my setup for Python scripting in FreeCAD with Pyzo IDE

Post by bdm »

heron wrote: Sat May 06, 2023 3:14 pm After wmayer's commit, is needed or inconvenient to follow the instructions: :?:
Hi heron,
thanks for your interest.

If you use the weekly build of FreeCAD (date 2023-05-04 or newer) from https://github.com/FreeCAD/FreeCAD-Bund ... kly-builds then do not modify Pyzo's source code.
But if you use the current stable version 0.20.2 (or older) then you will have to modify Pyzo's code (and this is simply editing the file with a text editor -- no compilation required because everything is interpreted at runtime).
heron
Posts: 307
Joined: Mon Apr 20, 2020 5:32 pm

Re: Showing you my setup for Python scripting in FreeCAD with Pyzo IDE

Post by heron »

Hi @bdm and thanks for answering,

I can't get FreeCAD to start from Pizo, when I run your test file "python_freecad_pyzo_example.py" or just try to start FreeCAD with Gui.showMainWindow() I get the following message:

Code: Select all

Python 3.10.10 | packaged by conda-forge | (main, Mar 24 2023, 20:00:38) on Windows (64 bits).
This is the Pyzo interpreter with integrated event loop for PYSIDE2.
Type 'help' for help, type '?' for a list of *magic* commands.

>>> (executing file "python_freecad_pyzo_example.py")
Assembly4 workbench (v0.50.1) loaded
Sheet Metal workbench loaded
Qt WebEngine seems to be initialized from a plugin. Please set Qt::AA_ShareOpenGLContexts using QCoreApplication::setAttribute before constructing QGuiApplication.
WebEngineContext used before QtWebEngine::initialize() or OpenGL context creation failed.
Qt has caught an exception thrown from an event handler. Throwing
exceptions from an event handler is not supported in Qt.
You must not let any exception whatsoever propagate through Qt code.
If that is not possible, in Qt 5 you must at least reimplement
QCoreApplication::notify() and catch all exceptions there.


The kernel process exited. (3765269347)

Code: Select all

Python 3.10.10 | packaged by conda-forge | (main, Mar 24 2023, 20:00:38) on Windows (64 bits).
This is the Pyzo interpreter with integrated event loop for PYSIDE2.
Type 'help' for help, type '?' for a list of *magic* commands.

>>> import FreeCADGui as Gui # auto-import
Assembly4 workbench (v0.50.1) loaded
Sheet Metal workbench loaded

>>> Gui.showMainWindow()
Qt WebEngine seems to be initialized from a plugin. Please set Qt::AA_ShareOpenGLContexts using QCoreApplication::setAttribute before constructing QGuiApplication.
WebEngineContext used before QtWebEngine::initialize() or OpenGL context creation failed.
Qt has caught an exception thrown from an event handler. Throwing
exceptions from an event handler is not supported in Qt.
You must not let any exception whatsoever propagate through Qt code.
If that is not possible, in Qt 5 you must at least reimplement
QCoreApplication::notify() and catch all exceptions there.


The kernel process exited. (3765269347)
I can write and run code but nothing occurs in FreeCAD side:
Captura1.PNG
Captura1.PNG (33 KiB) Viewed 1639 times
I modified the pointed line in pyzo/pyzokernel/interpreter.py and set the shell configuration as indicated
Captura2.PNG
Captura2.PNG (14.96 KiB) Viewed 1639 times
My full FreeCAD version is:

Code: Select all

OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.21.0.32985 (Git)
Build type: Release
Branch: master
Hash: 5cd377f931312ede305e4c764ba2028b20eaacfd
Python 3.10.10, Qt 5.15.8, Coin 4.0.0, Vtk 9.1.0, OCC 7.6.3
Locale: English/United States (en_US) [ OS: Spanish/Spain (es_ES) ]
Installed mods: 
  * Assembly4 0.50.1
  * BIM 2021.12.0
  * Curves 0.6.8
  * DynamicData 2.46.0
  * fasteners 0.4.56
  * freecad.gears 1.0.0
  * Help 1.0.3
  * InvGears 0.1.2
  * kicadStepUpMod 10.18.8
  * lattice2 1.0.0
  * Manipulator 1.5.0
  * ratchetWB 0.1.0
  * Render 2023.2.4
  * sheetmetal 0.2.63
I'm missing something but I can't figure out what it is. Any idea?
bdm
Posts: 180
Joined: Sat Dec 31, 2022 12:10 pm

Re: Showing you my setup for Python scripting in FreeCAD with Pyzo IDE

Post by bdm »

heron wrote: Sun May 07, 2023 1:14 pm I'm missing something but I can't figure out what it is. Any idea?
Hi heron!
According to your post, you are running FreeCAD Version 0.21.0.32985 (Git). This seems to be an older weekly-build that does not contain Werner's patch. The last number in the FreeCAD version is the number of commits on Github. When I count back from the current number of commits, your version seems to be from 2023-04-23 (+/- 1 day), and that would be definitely too old to contain the patch.

I do not have access to a windows machine right now, so I suggest you to try the two following steps:
1) try it again with the brand new version: https://github.com/FreeCAD/FreeCAD-Bund ... 4-py310.7z
2) or try it with the older stable version (https://github.com/FreeCAD/FreeCAD/rele ... ller-3.exe) and modify the Pyzo source code as described in my very first post of this thread -- this worked for me on a Windows 10 computer.
Tomorrow I will have access to my computer running Windows10 again and check if the weekly build runs with Pyzo.
heron
Posts: 307
Joined: Mon Apr 20, 2020 5:32 pm

Re: Showing you my setup for Python scripting in FreeCAD with Pyzo IDE

Post by heron »

bdm wrote: Sun May 07, 2023 2:51 pm I do not have access to a windows machine right now, so I suggest you to try the two following steps:
Hello and thanks for the hint, I tried the latest 0.21.33110 and works fine, it just outputs the following message between the code tags, but everything else works as expected:

Code: Select all

Python 3.10.10 | packaged by conda-forge | (main, Mar 24 2023, 20:00:38) on Windows (64 bits).
This is the Pyzo interpreter with integrated event loop for PYSIDE2.
Type 'help' for help, type '?' for a list of *magic* commands.

>>> (executing file "python_freecad_pyzo_example.py")
Assembly4 workbench (v0.50.1) loaded
Sheet Metal workbench loaded
Qt WebEngine seems to be initialized from a plugin. Please set Qt::AA_ShareOpenGLContexts using QCoreApplication::setAttribute before constructing QGuiApplication.
WebEngineContext used before QtWebEngine::initialize() or OpenGL context creation failed.

test2
test1
test3
test2b
Traceback (most recent call last):
  File "d:\aac\trabajo\formacion\tutoriales\pizo\python_freecad_pyzo_example.py", line 79, in <module>
    assert False, 'stopping execution of this script here -- run the next cell manually (pressing keys CTRL+ENTER)'
AssertionError: stopping execution of this script here -- run the next cell manually (pressing keys CTRL+ENTER)

>>> 
I also tested with stable version 0.20.2 with Pyzo source code modification as described in your first post, and it works perfectly without any messages.
However version 0.21.32985 doesn't work either with or without modified Pyzo source code, always getting the same result and getting the message attached to my post above.
User avatar
jonasb
Posts: 162
Joined: Tue Dec 22, 2020 7:57 pm

Re: Showing you my setup for Python scripting in FreeCAD with Pyzo IDE

Post by jonasb »

heron wrote: Sun May 07, 2023 8:46 pm

Code: Select all

Qt WebEngine seems to be initialized from a plugin. Please set Qt::AA_ShareOpenGLContexts using QCoreApplication::setAttribute before constructing QGuiApplication.
WebEngineContext used before QtWebEngine::initialize() or OpenGL context creation failed.
Try adding this into your startup script, before # AFTER_GUI

Code: Select all

from PySide import QtCore
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_ShareOpenGLContexts, True)
This made the message no away on my Mac. See also my post from May 5th, above.
Post Reply