From one execution to another, application may work normally or show nothing

Hello

I have a Python script that displays some actors in a window. It’s quite simple (more or less what you can find in the examples).

Sometimes, I run my script and everything works normally: I see the 2 actors and I can interact with the scene.

Sometimes, the window is empty.

How can I investigate this issue? Is there any logs or traces that I can enabled?
Do you have any idea of the cause of such an issue?

Thanks a lot!

Please share your script

Hello

I have created a simplified version of my application, that reproduces the issue.

  • This script first loads definitions for points, triangles and tetrahedrons, from a BDF file. This is the load_bdf_file() function.
  • Then, in the function create_mappers(), I create two mappers: one to render for the triangles, one for the tetrahedrons, the points being common.
  • Final, I render the mappers with 2 actors.
import collections
import dataclasses

import vtk
from bulkdata import Deck
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonCore import (
    vtkPoints
)
from vtkmodules.vtkCommonDataModel import (
    vtkCellArray,
    vtkPolyData,
    vtkTetra,
    vtkTriangle, vtkUnstructuredGrid,
)
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkMapper,
    vtkPolyDataMapper,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    vtkRenderer, vtkDataSetMapper,
)


@dataclasses.dataclass
class Grid:
    id: int
    x: float
    y: float
    z: float


@dataclasses.dataclass
class Ctria3:
    eid: int
    pid: int
    g1: int
    g2: int
    g3: int


@dataclasses.dataclass
class Ctetra:
    eid: int
    pid: int
    g1: int
    g2: int
    g3: int
    g4: int


@dataclasses.dataclass
class Bdf:
    grids: list[Grid]
    ctria3s: list[Ctria3]
    ctetras: list[Ctetra]


def load_bdf_file(filename: str) -> Bdf:
    # Load bdf model
    with open(filename, encoding='utf-8') as bdf_file:
        deck = Deck.load(bdf_file)
        names = collections.Counter([c.name for c in deck.cards])
        print(names)

    grids = []
    for card in deck.find({'name': 'GRID'}):
        grid = Grid(card[0], card[2], card[3], card[4])
        grids.append(grid)

    ctria3s = []
    for card in deck.find({'name': 'CTRIA3'}):
        ctria3 = Ctria3(card[0], card[1], card[2], card[3], card[4])
        ctria3s.append(ctria3)

    ctetras = []
    for card in deck.find({'name': 'CTETRA'}):
        ctetra = Ctetra(card[0], card[1], card[2], card[3], card[4], card[5])
        ctetras.append(ctetra)

    return Bdf(grids, ctria3s, ctetras)


def create_mappers(bdf: Bdf) -> (vtkMapper, vtkMapper):
    # Create points
    points = vtkPoints()
    for grid in bdf.grids:
        points.InsertPoint(grid.id, (grid.x, grid.y, grid.z))

    # Transform triangles
    triangles = vtkCellArray()

    for ctria3 in bdf.ctria3s:
        triangle = vtkTriangle()
        triangle.GetPointIds().SetId(0, ctria3.g1)
        triangle.GetPointIds().SetId(1, ctria3.g2)
        triangle.GetPointIds().SetId(2, ctria3.g3)
        triangles.InsertNextCell(triangle)

    data = vtkPolyData()
    data.SetPoints(points)
    data.SetPolys(triangles)

    mapper_triangles = vtkPolyDataMapper()
    mapper_triangles.SetInputData(data)

    # Create tetras
    tetras = vtkCellArray()

    for ctetra in bdf.ctetras:
        tetra = vtkTetra()
        tetra.GetPointIds().SetId(0, ctetra.g1)
        tetra.GetPointIds().SetId(1, ctetra.g2)
        tetra.GetPointIds().SetId(2, ctetra.g3)
        tetra.GetPointIds().SetId(3, ctetra.g4)
        tetras.InsertNextCell(tetra)

    unstructured_grid = vtkUnstructuredGrid()
    unstructured_grid.SetPoints(points)
    unstructured_grid.SetCells(vtk.VTK_TETRA, tetras)

    mapper_tetrahedrons = vtkDataSetMapper()
    mapper_tetrahedrons.SetInputData(unstructured_grid)

    return mapper_triangles, mapper_tetrahedrons


def render(mapper1, mapper2) -> None:
    colors = vtkNamedColors()

    # Triangles
    actor1 = vtkActor()
    actor1.SetMapper(mapper1)
    actor1.GetProperty().SetColor(colors.GetColor3d('turquoise'))

    # Tetrahedrons
    actor2 = vtkActor()
    actor2.SetMapper(mapper2)
    actor2.GetProperty().SetColor(colors.GetColor3d('salmon'))

    # Create a renderer, render window, and interactor
    renderer = vtkRenderer()
    renderer.SetBackground(colors.GetColor3d('black'))
    renderer.ResetCamera()

    window = vtkRenderWindow()
    window.SetWindowName('VisualSEA2')
    window.AddRenderer(renderer)

    interactor = vtkRenderWindowInteractor()
    interactor.SetRenderWindow(window)
    style = vtk.vtkInteractorStyleTrackballCamera()
    interactor.SetInteractorStyle(style)

    # Add the actors to the scene
    renderer.AddActor(actor1)
    renderer.AddActor(actor2)

    # Render and interact
    window.Render()
    interactor.Start()


if __name__ == '__main__':
    bdf_model = load_bdf_file('Cube_new_template.bdf')
    m1, m2 = create_mappers(bdf_model)
    render(m1, m2)

Unfortunately, I can’t upload the BDF file as I am a new user.

You can now :slight_smile:

1 Like

Cube_new_template.bdf (28.1 KB)

Here it is!

I dont see anything out of place in your program, you may want to check that when tthe bug shows up, there is actually some data pass to the rendering stack, eg print the number of points.

1 Like

I should do that simply by adding print(len(points)) in the function create_mappers()?

Or should it be something closer to the rendering core (in that case, what woud you suggest)?