5

Can i somehow instance ipython (or even better, ipython-qtconsole) and step trough its's (IPython's) main loop manually?

I want to edit panda3d programs on the fly.

EDIT1: Here is code sample which should clarify a bit what i want to do.

import sys

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from direct.showbase.ShowBase import ShowBase

class MyApp(ShowBase):

    def __init__(self):
        ShowBase.__init__(self)
        self.qtApp = QApplication(sys.argv)
        label = QLabel("Hello World")
        label.show()
        self.m = loader.loadModel("frowney")
        self.m.reparentTo(render)

        while 1:
            self.qtApp.processEvents()  #manual step trough Qt loop
            taskMgr.step()              #manual step trough Panda3D loop

app = MyApp()

So you can see how i can manually step trough panda and qt, i want to do same with ipython if its possible.

ANSWER Complete file:

from direct.showbase.ShowBase import ShowBase
from IPython.lib import inputhook
class MyApp(ShowBase):

def __init__(self):
    ShowBase.__init__(self)
    self.m = loader.loadModel("frowney")
    self.m.reparentTo(render)

def stepMe(self):
    taskMgr.step()              #manual step trough Panda3D loop
    return 0  
if __name__ == "__main__":  
    app = MyApp()  
    inputhook.set_inputhook(app.stepMe)

In your cmd line, just go to directory where file is and do

  1. ipython
  2. run file.py
  3. app.m.setPos(1,1,1)
grizwako
  • 1,563
  • 4
  • 19
  • 23

2 Answers2

2

By "edit panda3d programs on the fly", do you just mean changing things in order to test your running program? Or actually making persistent edits to your program's structure in the interactive environment?

Simply stepping over your loop in an interactive python session is quite easy. You can just replace while 1: with a method declaration such as def step(self):, then call it for each step.

import sys

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from direct.showbase.ShowBase import ShowBase

class MyApp(ShowBase):

    def __init__(self):
        ShowBase.__init__(self)
        self.qtApp = QApplication(sys.argv)
        label = QLabel("Hello World")
        label.show()
        self.m = loader.loadModel("frowney")
        self.m.reparentTo(render)

    def step(self):
        self.qtApp.processEvents()  #manual step trough Qt loop
        taskMgr.step()              #manual step trough Panda3D loop
        return 0                    #PyOS_InputHook expects an integer

if __name__ == "__main__":
    app = MyApp()
    while 1: app.step()

With if __name__ == "__main__" for the main loop, your file will still work as it should when run standalone. But now you can import it into an interactive session and modify things in between steps.

>>> import myfile
>>> app = myfile.MyApp()
>>> app.step()
>>> app.something = something_else
>>> app.step()

Now to add it to IPython's event loop, so it will be run as you use the interpreter, you can use IPython.lib.inputhook.set_inputhook() (new in IPython 0.11).

>>> from IPython.lib import inputhook
>>> inputhook.set_inputhook(app.step)

This should cause your program to run while the interpreter is idle, but still allow manipulation as usual.

mesilliac
  • 1,474
  • 11
  • 8
  • Sorry, but i believe you did not notice _I_Python in question title and body. Or if you did, then i want to do something like IPython.step() in that while loop. – grizwako Aug 16 '11 at 13:59
  • 1
    @GrizzLy I use this method in the standard python interpreter occasionally, so I thought it might be similar to what you were after. For ipython, can you just do `from IPython.lib import inputhook; inputhook.set_inputhook(app.step)`? This seems like it should do what you want, and appears to work for a panda3d app i had lying around, so long as "step" returns 0. – mesilliac Aug 16 '11 at 16:44
  • sorry for bothering, but could you please post instruction how to achieve that? code, and commands that you use to run it like that? i tried, and did not have any luck.. – grizwako Aug 16 '11 at 21:39
1

Run the script in the ipython debugger using

%run -d -b40 myscript

The -b40 parameter sets a breakpoint in the script on line 40 (which should be the first line of the loop you want to debug). Type c at the prompt to start the script which will stop at the breakpoint. Type h to get help on the debugger.

gurney alex
  • 13,247
  • 4
  • 43
  • 57
  • 1
    Yeah, but that would stop execution of my game, i want it to run continuosly. – grizwako Aug 15 '11 at 12:47
  • 1
    @Grizzly how could you step-through the loop and at the same time let your app run if everything happens within the same thread? (or maybe i misunderstood your question) – mdeous Aug 15 '11 at 15:16
  • @Mat Look at code that i posted in question, it is in that while 1: part. Maybe i totally misunderstood something, but stepping trough loop means making your application run? – grizwako Aug 15 '11 at 22:49
  • @Grizzly well, all the action (code that makes your application "run") takes place in the `self.qtApp.processEvents()` and `taskmgr.step()` calls, right? If so, stepping through the loop will do a pause at each loop, thus make your application "freeze" until the next loop. – mdeous Aug 15 '11 at 22:59
  • @Mat This will open two windows, each of them will simply run with this loop. Processing time for qt should be very very low, and yes, that means that events wont be processed while panda3d `taskMgr.step()` is being executed (it includes rendering 3d scene), but that does not mean that Qt GUI window will not be responsive. – grizwako Aug 15 '11 at 23:07