pyvista: Missing meshes in Renderer

When using the .add_mesh() method of BackgroundPlotter, some meshes are missing in the plotting window. An error is printed in the terminal which seems to have to do with qt.

QSocketNotifier: Multiple socket notifiers for same socket 848 and type Read
QSocketNotifier: Multiple socket notifiers for same socket 848 and type Read

Unfortunately, I have not been able to reproduce this outside of my package to. It seems that the bug is fixed by setting a unique name keyword argument for each mesh.

System Information:

Date: Mon Sep 30 21:12:28 2019 GMT, sommartid

       Windows : OS
             8 : CPU(s)
         AMD64 : Machine
         64bit : Architecture
       Jupyter : Environment

Python 3.6.8 |Anaconda, Inc.| (default, Feb 11 2019, 15:03:47) [MSC v.1915 64 bit (AMD64)]

        0.22.2 : pyvista
         8.2.0 : vtk
        1.15.4 : numpy
         2.5.0 : imageio
         1.4.3 : appdirs
         0.4.3 : scooby
         3.1.0 : matplotlib
         5.9.2 : PyQt5
         7.2.0 : IPython
         7.4.2 : ipywidgets

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 18 (14 by maintainers)

Most upvoted comments

Great that you were able to solve it @banesullivan! I have very limited time at the moment so cannot check the pull request right now but please go ahead anyway.

This jives with my situation. I was doing something along the lines of:

p.add_mesh(pv.Sphere(...), ...)
p.add_mesh(pv.Sphere(...), ...)
p.add_mesh(pv.Sphere(...), ...)
p.add_mesh(pv.Cylinder(...), ...)
etc.

Clearly the intermediate objects constructed this way could run the risk of getting collected and having nonunique ids, like you mention above.

Modifying @GuillaumeFavelier’s snippet from the above coment, we can see that memory ID addresses are reused when creating these spheres.

import pyvista as pv

N = 10
color = "tan" # <<<< setting the color here
addresses = []

p = pv.Plotter()
for i in range(N):
    for j in range(N):
        center = (i, j, 0)
        mesh = pv.Sphere(center=center)
        name = str(hex(id(mesh)))
        addresses.append(name)
        p.add_mesh(mesh, color=color, name=name) # color used here
p.show()

assert len(set(addresses)) == len(addresses)

which raises an assertion error and produces

download

It appears that since we instantiate the PyVista mesh in a for loop, that memory management is seeing no references to those IDs and reusing them. What this means is that we need a different default name tracker in PyVista other than the id() of that object as apparently it is not unique in these cases:

from https://docs.python.org/3/library/functions.html#id

Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.

From what I could find, it could be related to the fact that the _point_arrays attribute of the mesh has not been set correctly. Could you maybe try the following patch as a temporary workaround @kjelljorner ?

...
        # Draw molecule
        for atom in self._atoms:
            color = hex2color(jmol_colors[atom.element])
            radius = atom.radius * atom_scale - self._probe_radius
            sphere = pv.Sphere(center=list(atom.coordinates),
                                          radius=radius)
            sphere.point_arrays # <<<< test call here
            p.add_mesh(sphere, color=color, opacity=1, name=str(atom.index))
...

@GuillaumeFavelier That mirrors my experiences. I tried to reproduce my example with this quite elaborate code examples.

Using color property and missing out on some of the spheres:

sphere = pv.Sphere(center=[1.795792, 0.915039, 0.065149], radius=2.1)
p.add_mesh(sphere, color=(1.0, 0.0784313725490196, 0.5764705882352941), opacity=1)
sphere = pv.Sphere(center=[-0.219245, -0.074009, 0.132583], radius=1.8000000000000003)
p.add_mesh(sphere, color=(1.0, 0.6470588235294118, 0.0), opacity=1)
sphere = pv.Sphere(center=[-1.639481, 1.007726, -0.667928], radius=1.6999999999999997)
p.add_mesh(sphere, color=(0.7843137254901961, 0.7843137254901961, 0.7843137254901961), opacity=1)
sphere = pv.Sphere(center=[-0.151258, -1.824279, -0.713922], radius=1.6999999999999997)
p.add_mesh(sphere, color=(0.7843137254901961, 0.7843137254901961, 0.7843137254901961), opacity=1)
sphere = pv.Sphere(center=[-0.899942, -0.362797, 1.899497], radius=1.6999999999999997)
p.add_mesh(sphere, color=(0.7843137254901961, 0.7843137254901961, 0.7843137254901961), opacity=1)
sphere = pv.Sphere(center=[1.029041, -2.605917, -0.086961], radius=1.6999999999999997)
p.add_mesh(sphere, color=(0.7843137254901961, 0.7843137254901961, 0.7843137254901961), opacity=1)
sphere = pv.Sphere(center=[0.880376, -2.870422, 0.958149], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)
sphere = pv.Sphere(center=[1.163723, -3.542504, -0.644503], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)
sphere = pv.Sphere(center=[1.955347, -2.02541, -0.161003], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)
sphere = pv.Sphere(center=[0.221158, -1.685319, -2.208478], radius=1.6999999999999997)
p.add_mesh(sphere, color=(0.7843137254901961, 0.7843137254901961, 0.7843137254901961), opacity=1)
sphere = pv.Sphere(center=[1.062939, -0.995764, -2.340139], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)
sphere = pv.Sphere(center=[0.531775, -2.668726, -2.584777], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)
sphere = pv.Sphere(center=[-0.604418, -1.350192, -2.83437], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)
sphere = pv.Sphere(center=[-1.455162, -2.629783, -0.571278], radius=1.6999999999999997)
p.add_mesh(sphere, color=(0.7843137254901961, 0.7843137254901961, 0.7843137254901961), opacity=1)
sphere = pv.Sphere(center=[-1.735601, -2.786913, 0.475935], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)
sphere = pv.Sphere(center=[-2.295738, -2.143821, -1.078264], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)
sphere = pv.Sphere(center=[-1.331285, -3.621826, -1.027183], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)
sphere = pv.Sphere(center=[-3.045638, 0.722335, -0.099278], radius=1.6999999999999997)
p.add_mesh(sphere, color=(0.7843137254901961, 0.7843137254901961, 0.7843137254901961), opacity=1)
sphere = pv.Sphere(center=[-3.137901, 0.977198, 0.959757], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)
sphere = pv.Sphere(center=[-3.78039, 1.333812, -0.640525], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)
sphere = pv.Sphere(center=[-3.340981, -0.324677, -0.223919], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)
sphere = pv.Sphere(center=[-1.710901, 0.836608, -2.197938], radius=1.6999999999999997)
p.add_mesh(sphere, color=(0.7843137254901961, 0.7843137254901961, 0.7843137254901961), opacity=1)
sphere = pv.Sphere(center=[-2.357646, 1.624326, -2.606925], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)
sphere = pv.Sphere(center=[-0.730406, 0.936667, -2.672792], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)
sphere = pv.Sphere(center=[-2.150682, -0.120696, -2.490307], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)
sphere = pv.Sphere(center=[-1.271364, 2.490447, -0.42133], radius=1.6999999999999997)
p.add_mesh(sphere, color=(0.7843137254901961, 0.7843137254901961, 0.7843137254901961), opacity=1)
sphere = pv.Sphere(center=[-2.056669, 3.130297, -0.847919], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)
sphere = pv.Sphere(center=[-1.171723, 2.745782, 0.635125], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)
sphere = pv.Sphere(center=[-0.320343, 2.735476, -0.905001], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)
sphere = pv.Sphere(center=[-0.124684, -1.40233, 2.727869], radius=1.6999999999999997)
p.add_mesh(sphere, color=(0.7843137254901961, 0.7843137254901961, 0.7843137254901961), opacity=1)
sphere = pv.Sphere(center=[0.955153, -1.218509, 2.696292], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)
sphere = pv.Sphere(center=[-0.314749, -2.428249, 2.405785], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)
sphere = pv.Sphere(center=[-0.443104, -1.333919, 3.775766], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)
sphere = pv.Sphere(center=[-0.905192, 0.959995, 2.691072], radius=1.6999999999999997)
p.add_mesh(sphere, color=(0.7843137254901961, 0.7843137254901961, 0.7843137254901961), opacity=1)
sphere = pv.Sphere(center=[-1.933707, -0.714517, 1.800005], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)
sphere = pv.Sphere(center=[0.101037, 1.39403, 2.719558], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)
sphere = pv.Sphere(center=[-1.225443, 0.766975, 3.722773], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)
sphere = pv.Sphere(center=[-1.582551, 1.709322, 2.278416], radius=1.1)
p.add_mesh(sphere, color=(1.0, 1.0, 1.0), opacity=1)

Without color and plotting all atoms:

sphere = pv.Sphere(center=[1.795792, 0.915039, 0.065149], radius=2.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-0.219245, -0.074009, 0.132583], radius=1.8000000000000003)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-1.639481, 1.007726, -0.667928], radius=1.6999999999999997)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-0.151258, -1.824279, -0.713922], radius=1.6999999999999997)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-0.899942, -0.362797, 1.899497], radius=1.6999999999999997)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[1.029041, -2.605917, -0.086961], radius=1.6999999999999997)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[0.880376, -2.870422, 0.958149], radius=1.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[1.163723, -3.542504, -0.644503], radius=1.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[1.955347, -2.02541, -0.161003], radius=1.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[0.221158, -1.685319, -2.208478], radius=1.6999999999999997)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[1.062939, -0.995764, -2.340139], radius=1.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[0.531775, -2.668726, -2.584777], radius=1.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-0.604418, -1.350192, -2.83437], radius=1.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-1.455162, -2.629783, -0.571278], radius=1.6999999999999997)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-1.735601, -2.786913, 0.475935], radius=1.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-2.295738, -2.143821, -1.078264], radius=1.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-1.331285, -3.621826, -1.027183], radius=1.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-3.045638, 0.722335, -0.099278], radius=1.6999999999999997)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-3.137901, 0.977198, 0.959757], radius=1.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-3.78039, 1.333812, -0.640525], radius=1.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-3.340981, -0.324677, -0.223919], radius=1.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-1.710901, 0.836608, -2.197938], radius=1.6999999999999997)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-2.357646, 1.624326, -2.606925], radius=1.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-0.730406, 0.936667, -2.672792], radius=1.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-2.150682, -0.120696, -2.490307], radius=1.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-1.271364, 2.490447, -0.42133], radius=1.6999999999999997)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-2.056669, 3.130297, -0.847919], radius=1.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-1.171723, 2.745782, 0.635125], radius=1.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-0.320343, 2.735476, -0.905001], radius=1.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-0.124684, -1.40233, 2.727869], radius=1.6999999999999997)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[0.955153, -1.218509, 2.696292], radius=1.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-0.314749, -2.428249, 2.405785], radius=1.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-0.443104, -1.333919, 3.775766], radius=1.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-0.905192, 0.959995, 2.691072], radius=1.6999999999999997)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-1.933707, -0.714517, 1.800005], radius=1.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[0.101037, 1.39403, 2.719558], radius=1.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-1.225443, 0.766975, 3.722773], radius=1.1)
p.add_mesh(sphere, opacity=1)
sphere = pv.Sphere(center=[-1.582551, 1.709322, 2.278416], radius=1.1)
p.add_mesh(sphere, opacity=1)