Unable to add to .vtp models to my renderer

I am trying to add a second .vtp model to my render window, this model is supposed to be a second layer inside my main model. I have been using the same logic to add the new model but despite being able to retrieve the number of actors currently added (which equals to 2, I think that makes sense), the model is not showing. I even ““tried”” to force the new model to be added in the same position as the main model. Any ideas?

I already did something similar with an .STL file and it worked fine without much hassle.

Here’s the current code of my function (it’s supposed to be enabled by clicking on a button):

const loadVTPModel = (filePath) => {
console.log(Attempting to load VTP file from: ${filePath});

const reader = vtkXMLPolyDataReader.newInstance();
reader.setUrl(`${filePath}`).then(() => {
  console.log('VTP file URL set successfully');

  reader.loadData().then(() => {
    const polydata = reader.getOutputData(0);
    if (!polydata) {
      console.error('No se pudieron cargar los datos del archivo VTP.');
      return;
    }

    console.log('VTP data loaded successfully');
    console.log('Polydata:', polydata);

    const points = polydata.getPoints().getData();
    console.log('Number of points in polydata:', points.length / 3); // 

    const mapper = vtkMapper.newInstance();
    polydata.getPointData().setActiveScalars('FFR_Corrected');
    mapper.setInputData(polydata);

    const lookupTable = vtkColorTransferFunction.newInstance();
    lookupTable.addRGBPoint(0.0, 0.0, 1.0, 0.0); // Green for new model

    mapper.setLookupTable(lookupTable);

    const actor = vtkActor.newInstance();
    actor.setMapper(mapper);

    actor.getProperty().setColor(0.0, 1.0, 0.0); // Green color
    actor.getProperty().setOpacity(1.0); // Full opacity

    const renderer = global.fullScreenRenderer.getRenderer();
    renderer.addActor(actor);

    console.log('Actor added to renderer');

    const mainActor = renderer.getActors()[0]; 
    if (mainActor) {
      const mainActorPosition = mainActor.getPosition();
      const mainActorOrientation = mainActor.getOrientation();
      const mainActorBounds = mainActor.getBounds();
      console.log('Main actor position:', mainActorPosition);
      console.log('Main actor orientation:', mainActorOrientation);
      console.log('Main actor bounds:', mainActorBounds);
      
      actor.setPosition(mainActorPosition);
      actor.setOrientation(mainActorOrientation);
    } else {
      console.warn('Main actor not found');
    }

    const bounds = actor.getBounds();
    console.log('New actor bounds:', bounds);

    renderer.resetCamera(bounds);
    global.fullScreenRenderer.getRenderWindow().render();

    console.log('Render window updated');


    const actors = renderer.getActors();
    console.log('Current actors in the renderer:', actors.length);
  }).catch((error) => {
    console.error('Error:', error);
  });
}).catch((error) => {
  console.error('Error:', error);
});

};

You might want to check if there are cells in your polydata

That doesn’t seem to be the issue, I tried to replace the file of the main model to the secondary model and it seems to render as expected. So far the problem is that I am not able to render both simultaneously, if I try to render both without using a event it only renders the latest added model.

You said that the second model is a “second layer inside [your] main model”. I assume it’s supposed to still be visible at some given camera rotation?

You do have a possible race condition, where the second model might be loaded before the first model (and thus gets assigned the “main actor” role). I’m not sure if that’s the culprit, since I don’t see any other code that sets the actor position and orientation.

In my case the main model should always be loaded first, since the second one is only enabled through a button click event.

What I am looking for is to make the second model visible once I lower the opacity of the main model, since this should be inside. Take a look at this quick example I made on Paint:

  1. How it should look before triggering the event (main model opacity is set to 1.0)
  2. How it should look after triggering the event (new model is added and visible inside the main model. the main model opacity is set to 0.3.)

A couple of ideas to help you debug this.

  1. Ensure that you can render each vtp file separately. The GeometryViewer is a great tool to do a sanity check on this.
  2. Double check that each vtp file is getting its own vtkPolyDataReader → vtkMapper/vtkActor pipeline.
  3. When you add both actors to the scene, call renderer.resetCamera(); renderWindow.render() to make sure that the positions of your geometries are correct. You can also call actor.getBounds() to verify that the bounds of each actor are as expected (i.e. has sufficient intersection).
1 Like