Simple vispy script for random motion

223 views
Skip to first unread message

HadiM

unread,
Jun 2, 2015, 7:40:16 AM6/2/15
to visp...@googlegroups.com
Hi,

I am trying to learn some vispy using scene.visuals. I really don’t
want to bother with vispy.gloo if it’s possible :-)

So basically I would like to create 4 spheres and make them move in a
random fashion (brownian motion).

The following script works but there is two things:

it’s very slow (I don’t know how I can display FPS)
I am giving a new position to each sphere at each timepoint but I
would like to only specify a displacement, how can I do that ?

My system :

linux 64bits
NVIDIA Corporation GF106GL [Quadro 2000]
conda with python 3.4
git updated vispy version (cd6d936)

The script :

from vispy import geometry
from vispy import scene
from vispy import app
import vispy.app.qt as vispyqt

qtapp = vispy.app.use_app('pyqt4')

QtCore = qtapp.backend_module.QtCore
QtGui = qtapp.backend_module.QtGui

import vispy.app.qt as vispyqt

class MainWindow(QtGui.QWidget):

def __init__(self):
QtGui.QWidget.__init__(self, None)
self.setMinimumSize(800, 600)
self.setWindowTitle('Test')

self.slider = QtGui.QSlider(QtCore.Qt.Horizontal, self)
self.slider.valueChanged.connect(self.move)

self.canvas = vispyqt.QtSceneCanvas(keys='interactive',
app=qtapp, parent=self, bgcolor='white')

self.view = self.canvas.central_widget.add_view()
self.view.camera = scene.cameras.TurntableCamera()

# Create spheres
self.spheres = []
self.colors = ['red', 'red', 'green', 'green']
for i, color in enumerate(self.colors):
sphere = self.create_sphere(color)
self.spheres.append(sphere)
self.view.add(sphere)

# Generate random motion for each spheres
tmax = 1000
self.pos = np.random.random((tmax, len(self.spheres), 3))

# Config slider widget according to random motions
self.slider.setRange(0, tmax)
self.slider.setSingleStep(1)

# Layout
vlayout = QtGui.QVBoxLayout(self)
self.setLayout(vlayout)
vlayout.addWidget(self.canvas.native, 0)
vlayout.addWidget(self.slider, 1)

def create_sphere(self, color):

pos = np.random.random((1, 3))

mdata = geometry.create_sphere(64, 64, radius=0.1)
mesh = scene.visuals.Mesh(meshdata=mdata, shading='flat', color=color)
self.translate(mesh, pos)

return mesh

def translate(self, obj, vect):
t = scene.transforms.AffineTransform()
t.translate(vect)
obj.transform = t

def move(self, t):
for i, (color, sphere) in enumerate(zip(self.colors, self.spheres)):
self.translate(sphere, self.pos[t - 1, i])

qtapp.create()
win = MainWindow()
win.show()
qtapp.run()

Thank you !


HadiM

Luke Campagnola

unread,
Jun 2, 2015, 8:59:17 AM6/2/15
to visp...@googlegroups.com
In translate(), you create a new transform at every call. This is expensive because each new transform forces the shader programs to recompile. Instead, try creating the transforms once and reusing them.

HadiM

--
You received this message because you are subscribed to the Google Groups "vispy-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vispy-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vispy-dev/CAEpSnnZqPryQcyg6eb5i3YZ2p9MGj1CqP%3D_3YCgcH8FwznHzWg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Hadrien Mary

unread,
Jun 2, 2015, 9:23:21 AM6/2/15
to visp...@googlegroups.com
Thank you Luke.

I moved the transform creation so it is now called only once per object.

Correct me if I am wrong but I should be able to update object
position with the following :

```
for i, sphere in enumerate(self.spheres):
sphere.tranform.translate(self.pos[t - 1, i])
```

But the script failed with this erro r:

```
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-40-4fed6354bdd9> in move(self, t)
49 def move(self, t):
50 for i, sphere in enumerate(self.spheres):
---> 51 sphere.tranform.translate(self.pos[t - 1, i])
52
53 qtapp.create()

AttributeError: 'Mesh' object has no attribute 'tranform'
```

Which is weird because `win.spheres[0].transform` return an
AffineTransform object.

I don't understand...

--
Hadrien Mary

Ph.D student in Biology
Tournier-Gachet Team
CNRS - LBCMCP - UMR 5088

Université de Toulouse - Bât. 4R3B1
118, route de Narbonne - 31062 Toulouse
> https://groups.google.com/d/msgid/vispy-dev/CACZXET-t4%2BhY6Q_WzQfu_56En46HFG87TDt9kV%2BusQU7yFHhXw%40mail.gmail.com.

Cyrille Rossant

unread,
Jun 2, 2015, 9:27:49 AM6/2/15
to Vispy dev list
2015-06-02 15:22 GMT+02:00 Hadrien Mary <hadrie...@gmail.com>:
> Thank you Luke.
>
> I moved the transform creation so it is now called only once per object.
>
> Correct me if I am wrong but I should be able to update object
> position with the following :
>
> ```
> for i, sphere in enumerate(self.spheres):
> sphere.tranform.translate(self.pos[t - 1, i])

typo: it should be "transform" ...

HadiM

unread,
Jun 2, 2015, 9:44:17 AM6/2/15
to visp...@googlegroups.com
god.... I hate me !

Thanks !

PS: for the record the script is available here :
https://github.com/hadim/public_notebooks/blob/master/vispy_brownian/vispy_brownian.ipynb

--
HadiM
> --
> You received this message because you are subscribed to the Google Groups "vispy-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to vispy-dev+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/vispy-dev/CA%2B-1RQQ5XF%3DKYZb0erfcJqtgVC02r5%3DqjLyx7AX9yYuqcHjtOg%40mail.gmail.com.

HadiM

unread,
Oct 8, 2015, 12:19:40 PM10/8/15
to visp...@googlegroups.com

Hi,

This script https://github.com/hadim/public_notebooks/blob/master/vispy_brownian/vispy_brownian.ipynb does not work anymore and produce the following error :


---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-1-ecea6325f6ef> in <module>()
      6 
      7 import vispy
----> 8 import vispy.app.qt as vispyqt
      9 
     10 qtapp = vispy.app.use_app('pyqt4')

/home/hadim/Insync/Documents/phd/dev/contrib/python/vispy/vispy/app/qt.py in <module>()
     26     raise RuntimeError("Invalid value for qt_lib %r." % qt_lib)
     27 else:
---> 28     raise RuntimeError("Module backends._qt should not be imported directly.")
     29 
     30 

RuntimeError: Module backends._qt should not be imported directly.

I am not really surprise the API has been modified but I wonder to know how could I embed a vispy canvas into a Qt windows.

Before I used self.canvas = vispyqt.QtSceneCanvas(keys='interactive', app=qtapp, parent=self, bgcolor='white') but it seems that I cant use vispyqt anymore…

Any idea ? Should I open an issue ?

PS: I use the last version on GitHub repo : 1b9f1415e3f263ec7e10fddc8cb8fb275dfa7bf2

--
HadiM

HadiM

unread,
Nov 4, 2015, 12:13:03 PM11/4/15
to visp...@googlegroups.com
Hi again,

... and up :-D


--
HadiM
Reply all
Reply to author
Forward
0 new messages