General python question: detect keypress?

MakeHuman python API, python plugins, etc

Moderator: joepal

General python question: detect keypress?

Postby TwoPikachus » Sat Mar 22, 2014 7:42 pm

I have some general programming knowledge (primarily c#) and I've been trying to modify Makehuman for my purposes, but Python's different way of doing things is throwing me for a loop.

I understand most of what's going on and I've been looking through the source, but I can't seem to discern how to detect a keypress. It looks like mh uses a third-party library called pyqt4 (which I had to install to compile). I've Googled and Googled but I just can't seem to find how to detect a keypress using this library or any other, at least not in any easy-to-understand way.

What I'm trying to do is switch the camera projection from ortho to perspective. I asked for this in features but I was rejected, so I've decided to figure out how to do it on my own. Setting the orbital camera's fovAngle to 45 and its projection to 1 seems to accomplish this, but zooming in will only zoom into the torso and the camera won't pan up when zoomed in. I understand this is because the camera is coded for orthographic usage, not perspective usage.

So what I've been trying to test is if, after you've got the camera zoomed and panned where you want it in ortho, when you switch the camera to a different projection mode and fov will it stay there with a perspective projection? I want to be able to press a key and change the fovAngle and projection values, then press a different key and change them back.

So far this is what I've got in the camera.py script at the end of the OrbitalCamera class:

Code: Select all
def keyPressEvent(self, e):
   
        if e.key() == QtCore.Qt.Key_M:
            if self._projection == 0:
                self._fovAngle = 45.0
                self._projection = 1
        if e.key() == QtCore.Qt.Key_N:
            if self._projection == 1:
                self._fovAngle = 90.0
                self._projection = 0


I've imported PyQt4's QtCore, QtGui, QtOpenGL and qtui and qtgui (though I'm not sure I even need those).

Upon compile and trying to use these lines, nothing happens. Is there something I'm missing? Thanks!

Also, please don't tell me about not wanting to use perspective. I know, it's distorted and all that. I've been using 3D packages for years. But if they're the same fov (both this and 3ds at 45) then they come out more or less the same. I just want to be able to control facial morphs in a perspective projection so I know what I'm getting before sending it over to 3ds Max so I can avoid some trial and error. I'm sorry if this paragraph comes across as rude, I don't mean it to. But I know what I want, and I don't want orthographic all the time.

Please and thank you!
TwoPikachus
 
Posts: 3
Joined: Thu Mar 20, 2014 8:53 pm

Re: General python question: detect keypress?

Postby duststorm » Sat Mar 22, 2014 8:47 pm

TwoPikachus wrote:mh uses a third-party library called pyqt4 (which I had to install to compile). I've Googled and Googled but I just can't seem to find how to detect a keypress using this library

Have a look qt the Qt documentation. PyQt is a wrapper around the Qt libraries, and tries to mimick its API as best as possible. There's only a few details that differ, like how to use slots in python, but for all the rest you can follow Qt tutorials and look at the Qt API.

Furthermore, MH already contains example code showing how to catch keyboard presses (core/mhmain.py), using QAction (look it up in Qt documentation).
MakeHuman™ developer
User avatar
duststorm
 
Posts: 2569
Joined: Fri Jan 27, 2012 11:57 am
Location: Belgium

Re: General python question: detect keypress?

Postby TwoPikachus » Tue Apr 01, 2014 12:57 am

Thank you very much for the pointer! I ended up making another button on the toolbar to accomplish this. I'm still having trouble with zoom focus, though.

If one were to put the camera to a projection of 1, does the camera then rotate around the calculated center of the bounding box of the model? If I wanted to make the camera rotate/zoom around the face, what methods in the camera.py script should I be looking at?

Please and thank you!
TwoPikachus
 
Posts: 3
Joined: Thu Mar 20, 2014 8:53 pm

Re: General python question: detect keypress?

Postby duststorm » Thu Apr 03, 2014 1:10 pm

Orthographic mode does not work, don't try unless you want to get your hands dirty actually fixing it.
The camera has functions to set the pivot point, there is even sample code (which can be used via CTRL+right click) on how to use camera focusing.
MakeHuman™ developer
User avatar
duststorm
 
Posts: 2569
Joined: Fri Jan 27, 2012 11:57 am
Location: Belgium


Return to Python scripts

Who is online

Users browsing this forum: No registered users and 1 guest

cron