SOLVED: no halt at matplotlib.pyplot.show()

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
HarryvL
Veteran
Posts: 1283
Joined: Sat Jan 06, 2018 7:38 pm
Location: Netherlands

Re: no halt at matplotlib.pyplot.show()

Post by HarryvL »

I put an input() call after matplotlib.pyplot.show() just to see if this could force my macro to pause and give the main thread focus to the plot ... and indeed it does. The plot becomes interactive. However, this is of course no solution because it would mean I have to press enter in the Python console to resume the process. I therefore instead tried to lock the thread of my macro using the threading.Lock.acquire() method and unlock it in my button-press handler with threading.Lock.release(). However, threading.Lock.acquire() does not manage to hold the execution of my macro.

I am way out of my depth here and would appreciate some help.
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: no halt at matplotlib.pyplot.show()

Post by Kunda1 »

@oliveroxtoby would you mind weighing in ? since CfdOF leverages plot workbench I figured you may know a thing or 2 about this issue?
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: no halt at matplotlib.pyplot.show()

Post by heda »

maybe use args to the call? or look at the plt mode
https://matplotlib.org/stable/api/_as_g ... .show.html

fc might behave funny since one suppose mpl is using fc-qt main-loop

if you can boil it down to a mve others might be quick to figure out how to do it
User avatar
HarryvL
Veteran
Posts: 1283
Joined: Sat Jan 06, 2018 7:38 pm
Location: Netherlands

Re: no halt at matplotlib.pyplot.show()

Post by HarryvL »

I tried to run Matplotlib in a separate thread, but it objects to this. Easiest would be to load my script as a module from the Python Console, so it runs in the main thread. How do I get FC to recognize my .py as a module? I tried placing a dummy.py into a ../Mod/fcFEM, directory, but >>>import fcFEM did not do the trick. Do I need to register my file somewhere?
User avatar
HarryvL
Veteran
Posts: 1283
Joined: Sat Jan 06, 2018 7:38 pm
Location: Netherlands

Re: no halt at matplotlib.pyplot.show()

Post by HarryvL »

SORRY - the answer is here: https://wiki.freecadweb.org/Workbench_creation. Import works now.
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: no halt at matplotlib.pyplot.show()

Post by Kunda1 »

Do you mind explicitly pasting the solution? (For posterity)
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: no halt at matplotlib.pyplot.show()

Post by heda »

right, running it in a separate thread is probably not the easiest way to get it working.
(but admittedly have not tried anything with mpl and fc, so not really in the know here)

regarding imports, once the penny drops that fc appears to skips a level of folders (compared to vanilla python) it becomes easier to do working imports

with file structure:

Code: Select all

userdir
|- Mod
|  |- testmodule
|  |  |- submodule
|  |  |  | dummy2.py
|  |  | dummy.py
and files

Code: Select all

# -*- coding: utf-8 -*-

# dummy.py

print('imported', __file__)

def foo():
  print('in foo')
  return 5

Code: Select all

# -*- coding: utf-8 -*-

# dummy2.py

print('imported', __file__)

def bar():
  print('in bar')
  return 10

from the console it becomes...

Code: Select all

# on linux
>>> import dummy
imported /home/x/.FreeCAD/Mod/testmodule/dummy.py
>>> from submodule import dummy2
imported /home/x/.FreeCAD/Mod/testmodule/submodule/dummy2.py
>>> dummy.foo()
in foo
5
>>> dummy2.bar()
in bar
10
>>> 
ps: meant to say: mpl might behave funny since one suppose mpl is using fc-qt main-loop (in the earlier post)

ps2: if nothing else works you can always make your own qt-frame/dialogue as any example of making a fc dialogue and embed a figurecanvas... https://matplotlib.org/stable/gallery/u ... gskip.html
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: no halt at matplotlib.pyplot.show()

Post by Kunda1 »

Thanks!
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
User avatar
HarryvL
Veteran
Posts: 1283
Joined: Sat Jan 06, 2018 7:38 pm
Location: Netherlands

Re: no halt at matplotlib.pyplot.show()

Post by HarryvL »

Kunda1 wrote: Sun May 29, 2022 3:58 pm Do you mind explicitly pasting the solution? (For posterity)
Import works, but the issue with Matlibplot persists. I also notice that it isn't consistent and therefore difficult to reproduce. For example, adding an input() statement after plt.show() activated the buttons at some point in time, but when I tried this at a later point in time, it didn't. There are some posts around that report problems with running Matplotlib with Python 3.10, e.g. https://stackoverflow.com/questions/695 ... ython-3-10, where it says:

Code: Select all

"After some digging and testing I think I've found out that Matplotlib, and pylab aren't compatible with python3.10 yet. For the time being I've kept python3.9 along with 3.10 so whenever I use matplotlib, numpy, or pylab, I just use version 3.9"
I am now trying to recompile FC20 with Python3.9, which I don't find straight forward either ... all-in-all I am still a long way from what I intended to do, i.e. getting my fcFEM macro up and running again.
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: no halt at matplotlib.pyplot.show()

Post by Kunda1 »

python 3.10 is still problematic for using in FreeCAD, JFYI
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
Post Reply