I want to visualize one or more STL file(s) using a vtk render viewport inside my application. I was able to get it all running with this example here: https://kitware.github.io/vtk-examples/site/Python/IO/ReadSTL/
My question is as follows: I have a mouse to control it and it is just unbelievable how bad default the controls of the camera are. It is almost impossible to get the model rotated intentionally or focus on a certain part. I am confused as I could not find any topic here or elsewhere discussing this. Normally I would read through other threads to find an answer, but there doesn't seem to be any. So, maybe you can point me to an approach or even certain functions of how I can change these settings, that would be really great! Basically I want to mimic the camera behavior of say, Paraview or a common CAD tool. Like zooming with mouse wheel, rotating with RMB and holding down the wheel to pan.
Some background on my project: I have written a python program and created a quite ok UI using tkinter. However, recently I was thinking of rewriting some basic parts of it and also upgrading everything visually and in terms of handling. So, I want to move to Qt widgets and, there shall be a VTK rendering viewport inside my application to show some of the stuff that can be interacted with.
I was able to find a solution in the tutorials. By explicitly defining the vtkInteractorStyle with
style = vtk.vtkInteractorStyleTrackballCamera()
The handling is just as I wanted it to be.
Related
I am fairly new to Python and is stuck at a problem. I am using Python 3.7 and intend to add a "drag-and-drop" functionality to my GUI.
I have some files that needs to be read, thus i am looking for an option where i can simply drag those files to a particular area in my GUI and file name or Path is read by my program.
Being a newbie a detailed answer or a code would be highly helpful.
Thanks
Unfortunately, tkinter only supports experimental widget drag and drop, nothing else. I ran into similar problems trying to make a complex GUI.
I would look into wxPython. It's complex, Object Oriented, and daunting, but has and unbelievable amount of features that reward you for learning it. Including file drag and drop.
Look at the wxPython thumbnails at https://wxpython.org/Phoenix/docs/html/gallery.html for images of what the widgets look like. The thumbnails include images for about half of the available widgets. The images are also a bit out of date.
Look at pronounced https://pythonlibrary.org or https://zetcode.com for the best tutorials. After them, your best resource is Google and the forums.
I am working on the following project and I am having really difficulties in finding the right way of doing that. I would like to build in Python (but I am open to other possibilities) a very basic interface that allows the user to draw with the mouse (or the pen if used on a surface laptop) something and then save the image. Ideally I would like this to work on a website or at least in a jupyter notebook (at least I imagine this to be utterly difficult).
Anyone can point me in the right direction? The goal would be to use the images as input to a neural network model to demonstrate its result with real life examples.
I am looking at tk but I don't seem to find much in terms of examples.
Thanks in advance, Umberto
I'd take a look at pyautogui to capture the mouse location then "draw" it in matplotlib -- should be able to do this in a loop. You'll want to watch the tkinter window size to sync the mouse coordinates with the relative location.
Why not just have your script open create a new blank img and automatically open it with paint - then read it on close? Seems easier than creating a drawing GUI.
Have a look at my Github repository which have exactly what you need.
Link : CanvasDraw Repo
Depending on the complexity you could either use tkinter which is a package for complex GUIs or something from the gaming community like pygames. You have user input and graphical output so libraries made for games will do what you want but provide way more stuff then you need. This site might help you: Drawing Libarys
Also the answere draw-on-python-tkinter-canvas-using-mouse-and-obtain-points-to-a-list might help you.
I'm trying to embed multitouch events into my wxPython GUI, however i'm a bit lost as to the best approach.
Currently, I have a TUIO server which transmits the multitouch events to be captured. I then use the pytuio library to receive the multitouch events in a separate thread for my GUI. My GUI is composed of a wxFrame with multiple matplotlib panels and a single OpenGL panel.
The problem is that I have had to manually write code to determine how many fingers are being used, the locations and the touch type. I then send a custom event which can be received by my GUI.
This works fine for the matplotlib panels (albeit I have to provide a very small constant offset to the reported location of the fingers), but for the OpenGL panel the finger locations seem to be incorrect. This is a problem as the offset of the touch locations in the OpenGL panel is not even a constant, it seems to vary depending where on the panel the touch event occurs. So I cannot compensate for it.
I feel like there must be a more comprehensive multitouch library, which does all the hard work determining the number of fingers and touch type (tap, double tap, drag, release etc). And possibly would overcome my issue with the OpenGL panel. I have looked but I've not seen a library which can distinguish the touch type etc., they just seem to provide a list of the number of fingers and the locations.
The only comprehensive GUI library supporting:
Python
More than one OS
Multitouch
is Kivy. I was able to cobble together something which works for Windows 7 and higher and wxPython (by extracting the relevant part from Kivy for processing WM_TOUCH events), so in principle it could be done. But none of this would solve your specific problem.
SO I am infact doing something very similar to this user posts:
https://stackoverflow.com/questions/6800292/python-ai-and-3d-animation
but it has no answers and I couldn't contact the user.
Basically I have a functioning python script that answers me with an action accordingly to my voice command. (Fetch emails, weather forecast, turn lights ON/OFF, etc), it has been made using the pyspeech library which is pretty darn good.
Now I want to give my programm a "face"! I thought about modelling the face with Blender (have some knowledge and would build up on it) and I know I could animate it, so the lips move and such.
So I want to know if it is at all possible to:
Load the "face" that I made from blender from my main python script (so when my programm start the face would be there on the screen too)
Run from the script the animations such that when for example when my programm says "You're welcome" I would run the animation that the lips move on the face to simulate it is speaking.
I know that blender has a good python integration (maybe correct is to say it is built on?) and that is why I thought it would be a good program to use.
Hope someone can help and tell me if that is at all possible and maybe show me some right way to go, my googling just showed me always python scripting with Blender which is not what I exactly need here... I think...
Cheers,
Flavio
Indeed, what you want is possible.
If all you want is to play pre-rendered animation videos based on decisions on your program, any GUI that allows you to embedd and play video in a widget will do for your application.
You could rool out your own GUI using Pygame (which has video support, but you will need one of the "minor" more or less "amateur" widget toolkits made for pygame to make up the remaining of your application, as pygame is pretty low level.
On a higher level, although I'had not embedded video, I think you could go with PyQT4 (googled a bit, not that many examples either, buthints that there are eamples in QT4 source) or GTK+ (the samething, it looks like there are more examples).
Another option would be to build your application to run inside the Blener Game Engine itself - It offers both a high level Toolkit, and ways to customize behaviors to user actions (even without coding).
The major drawback in doing this is: I don't know which are the options to distribute an application that needs Blender Game Engine nowadays - your users will need to install Blender (but it is likely Blender folks made an easy way to jhandle this).
On the upper hand: you get the most flexibility, it would even be possible to render some sequences in realtime (as opposed to pre-rendered videos) in your app.
One thing: Blender nowadays use Python 3.x - if the other libraries you need are Python 2, you willl need to make one different process for the GUI inside Blender, and exchange data with your application's backeend in Python 2 (for example using jsonrpc or xmlrpc - that is enoguh simple in Python).
For my own benefit and possibly for educational use, I would like to make a pygame-like API for Python and Cairo. But I don't want it to be exactly pygame. I would instead like to make it a semi-static drawing platform that display one or more images using GTK/GDK, and I would like to imitate the excellent API principles of TiKZ (the latex package). The PyGame API is not bad, but I'm not satisfied with it. One particular issue is that I would like the package to handle window refresh by drawing everything into a pixbuf (with Cairo), and automatically redraw the pixbuf when the window is uncovered. That way the end programmer doesn't have to worry about window refresh. In fact, the end programmer shouldn't have to write a single class or function or any more than a straight sequence of lines of code to draw a smiley face (say). The graphics library also doesn't have to maintain an ever-longer list of stored shape objects as is the case in TkInter. (At least, I hope that Cairo doesn't do that against my intentions.)
I succeeded in drawing various things in pycairo with output to ImageMagick and Postscript. So I'm okay with pycairo itself.
Unfortunately, the cairo/gtk/pycairo/pygtk documentation that I found --- I don't know who it's written for, but not for me. At the moment, I am a Project Euler type of programmer, not a "5 bleeding edge ultra-object-oriented APIs" type of programmer. I'd like to see a clear explanation of what to do, and/or a clear example.
Okay, I accepted the one answer that was posted because it was at least a little helpful. But here in a nutshell is the real point. The point is that GDK make a temporary double buffer when you draw things in GDK, including using Cairo. It is expected that when you handle an expose event, you will just redraw everything. But if you have a very complicated image, this is a slow process, especially in Python. So it would be much nicer if Cairo could write to a permanent double buffer rather than a temporary one, and then that permanent double buffer would be exposed with GDK. Several developers have wanted a solution to this problem. One of the projects that seems to have some kind of solution is Google Chromium --- have you ever noticed how great window exposure is in Google Chrome, for instance in Linux? So I will look at the Chromium source code to see if I can do this easily.
Addendum: I see that I did confuse the issue by referring specifically to "pixbufs". I don't really care about pixbufs (and I changed the question title again). What I really care about is creating a permanent double buffer pixel array between Cairo and GTK/GDK, instead of a temporary double buffer pixel array. It seems that the easiest way to do that is to make the GTK window a Cairo surface and make the double buffer another Cairo surface. Since I asked for an sample in my question, here is some:
class Canvas(gtk.DrawingArea):
def __init__(self):
super(Canvas, self).__init__()
self.connect("expose_event", self.expose)
self.set_size_request(width,height)
def expose(self, widget, event):
cr = widget.window.cairo_create()
cr.set_source_surface(mybuffer,0,0)
cr.paint()
Another tricky issue that quickly arose is that I wanted this to be a WYSISWYG drawing environment that immediately draws what Python asks it to draw --- and that can be extended to animations. However, most GTK examples aren't set up that way: event handling is postponed until I either call gtk.main(). (Or in Python, I was surprised to discover that raw_input() also somehow flushes the GTK event queue.) I found a nice explanation, with Python examples, of alternatives to giving away event control to GTK. The simplest solution and possibly the one that I will adopt is to use this to flush the event buffer whenever you want to do that:
while gtk.events_pending(): gtk.main_iteration(False)
There is one final thing that I will need, to flush the pixel buffer as well as the event buffer. It looks like one way to do that is window.queue_draw()
As this was too big for a comment I have added this as a response.
The question is not quite clear. Do you mean to ask how to use cairo drawing to draw onto Gtk Widgets? Firstly, there is nothing called GTK Pixbuf, I think you are referring to GDK Pixbuf. Most the drawing stuff in GTK is done at GDK layer. If you want to find out about windowing, drawing mechanism or image manipulation you should look into GDK for more details. These links will hopefully help you get some insight about cairo-gdk interaction. Although my experience with python bindings for GTK, GDK & Cairo is nil, but I think that Google will provide you with some good resources if you look up gdk-cairo sample.
Hope this helps at least a bit!