# vtkActor getCenter() doesn't work unless getBounds() is called first

**URL:** https://discourse.vtk.org/t/vtkactor-getcenter-doesnt-work-unless-getbounds-is-called-first/7701
**Category:** Web
**Created:** [January 22, 2022, 2:04pm UTC](https://discourse.vtk.org/t/vtkactor-getcenter-doesnt-work-unless-getbounds-is-called-first/7701 "2022-01-22T14:04:48Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Mohamed\_Waheed](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mohamed_waheed/32/4536_2.png) [@Mohamed\_Waheed](https://discourse.vtk.org/u/Mohamed_Waheed)
#### Post date: [January 22, 2022, 2:04pm UTC](https://discourse.vtk.org/t/vtkactor-getcenter-doesnt-work-unless-getbounds-is-called-first/7701/1 "2022-01-22T14:04:49Z")

</div>

Hello,

The getCenter() method of vtkActor (of vtkProp3D really) does not work (returns [0,0,0]) unless getBounds() is called first. I am using STL models as sources.

here is the official STLReader example, with the added code:

```auto
import '@kitware/vtk.js/favicon';

// Load the rendering pieces we want to use (for both WebGL and WebGPU)
import '@kitware/vtk.js/Rendering/Profiles/Geometry';

import vtkActor from '@kitware/vtk.js/Rendering/Core/Actor';
import vtkFullScreenRenderWindow from '@kitware/vtk.js/Rendering/Misc/FullScreenRenderWindow';
import vtkMapper from '@kitware/vtk.js/Rendering/Core/Mapper';
import vtkSTLReader from '@kitware/vtk.js/IO/Geometry/STLReader';

// ----------------------------------------------------------------------------
// Example code
// ----------------------------------------------------------------------------

const reader = vtkSTLReader.newInstance();
const mapper = vtkMapper.newInstance({ scalarVisibility: false });
const actor = vtkActor.newInstance();

actor.setMapper(mapper);
mapper.setInputConnection(reader.getOutputPort());

// ----------------------------------------------------------------------------

function update() {
  const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance();
  const renderer = fullScreenRenderer.getRenderer();
  const renderWindow = fullScreenRenderer.getRenderWindow();

  const resetCamera = renderer.resetCamera;
  const render = renderWindow.render;

  // My code
  console.log(actor.getCenter()); // [0, 0, 0]
  actor.getBounds();
  console.log(actor.getCenter()); // [x, y, z]

  renderer.addActor(actor);
  resetCamera();
  render();
}

// ----------------------------------------------------------------------------
// Use a file reader to load a local file
// ----------------------------------------------------------------------------

const myContainer = document.querySelector('body');
const fileContainer = document.createElement('div');
fileContainer.innerHTML = '<input type="file" class="file"/>';
myContainer.appendChild(fileContainer);

const fileInput = fileContainer.querySelector('input');

function handleFile(event) {
  event.preventDefault();
  const dataTransfer = event.dataTransfer;
  const files = event.target.files || dataTransfer.files;
  if (files.length === 1) {
    myContainer.removeChild(fileContainer);
    const fileReader = new FileReader();
    fileReader.onload = function onLoad(e) {
      reader.parseAsArrayBuffer(fileReader.result);
      update();
    };
    fileReader.readAsArrayBuffer(files[0]);
  }
}

fileInput.addEventListener('change', handleFile);

```

and here is a small stl file:

> **[LED\_strip\_mount.stl](https://drive.google.com/file/d/1HnchqVJJnByK56vZidb4l_VTrlCWQbfh/view?usp=sharing)**
>
> Google Drive file.

This seems like a bad side effect, especially since that isn’t explained somewhere.

or that is normal behavior known to many?

Thank you

---

<div class="post-metadata">

### Author: ![Sebastien\_Jourdain](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sebastien_jourdain/32/100_2.png) [@Sebastien\_Jourdain](https://discourse.vtk.org/u/Sebastien_Jourdain)
#### Post date: [January 23, 2022, 11:30pm UTC](https://discourse.vtk.org/t/vtkactor-getcenter-doesnt-work-unless-getbounds-is-called-first/7701/2 "2022-01-23T23:30:28Z")

</div>

I think this is related to pipeline execution. The data has not been flushed all the way to the actor.  
If the actor was rendered, I would assume getCenter() would give you the right information.  
I’m not saying this is a well known behavior, but in general with VTK, you need to make sure your object is up to date before reading some of its state value. Some method will force things to update such as algo.update(), rw.render() while other will report the current state of the object without any pipeline flush.

My guess is that getBounds() in actor execute the pipeline while getCenter() is just doing a local read.

---

<div class="post-metadata">

### Author: ![Mohamed\_Waheed](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mohamed_waheed/32/4536_2.png) [@Mohamed\_Waheed](https://discourse.vtk.org/u/Mohamed_Waheed)
#### Post date: [January 24, 2022, 11:46am UTC](https://discourse.vtk.org/t/vtkactor-getcenter-doesnt-work-unless-getbounds-is-called-first/7701/3 "2022-01-24T11:46:28Z")

</div>

Thank you for your reply.

Yes your guess is correct indeed, if getCenter() is called after rw,render() has been called, it shows the right values.

Thanks again.
