# error with a same vtkactor add in different vtkrender

**URL:** https://discourse.vtk.org/t/error-with-a-same-vtkactor-add-in-different-vtkrender/6045
**Category:** Support
**Created:** [June 23, 2021, 4:46am UTC](https://discourse.vtk.org/t/error-with-a-same-vtkactor-add-in-different-vtkrender/6045 "2021-06-23T04:46:21Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Hurmean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hurmean/32/2964_2.png) [@Hurmean](https://discourse.vtk.org/u/Hurmean)
#### Post date: [June 23, 2021, 4:46am UTC](https://discourse.vtk.org/t/error-with-a-same-vtkactor-add-in-different-vtkrender/6045/1 "2021-06-23T04:46:21Z")

</div>

when a vtkactor add in differert vtkrender with different vtkrenderwindow, this doesn’t work.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [June 23, 2021, 5:15am UTC](https://discourse.vtk.org/t/error-with-a-same-vtkactor-add-in-different-vtkrender/6045/2 "2021-06-23T05:15:27Z")

</div>

You can reuse the data object (such as vtkImageData, vtkPolyData), but you cannot reuse mappers or actors in different renderers.

---

<div class="post-metadata">

### Author: ![Hurmean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hurmean/32/2964_2.png) [@Hurmean](https://discourse.vtk.org/u/Hurmean)
#### Post date: [June 23, 2021, 5:37am UTC](https://discourse.vtk.org/t/error-with-a-same-vtkactor-add-in-different-vtkrender/6045/3 "2021-06-23T05:37:03Z")

</div>

but this code can work  
namespace {  
std::vector\<vtkSmartPointer\> GetSources();

```
// Define interaction style
class KeyPressInteractorStyle : public vtkInteractorStyleTrackballCamera
{
public:
    static KeyPressInteractorStyle* New();
    vtkTypeMacro(KeyPressInteractorStyle, vtkInteractorStyleTrackballCamera);

    virtual void OnKeyPress()
    {
        // Get the keypress
        vtkRenderWindowInteractor* rwi = this->Interactor;
        std::string key = rwi->GetKeySym();

        if (key == "e" || key == "q" || key == "E" || key == "Q")
        {
            *status = false;
        }

        // Forward events
        vtkInteractorStyleTrackballCamera::OnKeyPress();
    }

public:
    bool* status = nullptr;
};
vtkStandardNewMacro(KeyPressInteractorStyle);

```

} // namespace

int multiwindows()  
{  
auto simultaneousUpdate = false;  
/_if ((argc \> 1) &&  
(std::strin_///g(argv[1]) == “s” || std::string(argv[1]) == “S”))  
{  
simultaneousUpdate = true;  
}

```
vtkNew<vtkNamedColors> colors;

// Have some fun with colors
std::vector<std::string> renBkg{ "AliceBlue", "GhostWhite", "WhiteSmoke",
                                "Seashell" };
std::vector<std::string> actorColor{ "Bisque", "RosyBrown", "Goldenrod",
                                    "Chocolate" };
// Window sizes and spacing.
auto width = 300;
auto height = 300;
// Add extra space around each window.
auto dx = 20;
auto dy = 40;
auto w = width + dx;
auto h = height + dy;

std::vector<vtkSmartPointer<vtkRenderWindowInteractor>> interactors;
std::vector<vtkSmartPointer<KeyPressInteractorStyle>> styles;
std::array<bool, 4> running{ true, true, true, true };

vtkCamera* camera = nullptr;
auto sources = GetSources();

vtkNew<vtkPolyDataMapper> mapperCommon;
mapperCommon->SetInputConnection(sources[1]->GetOutputPort());

vtkNew<vtkActor> actorCommon;
actorCommon->SetMapper(mapperCommon);
actorCommon->SetOrientation(40, 40, 40);

for (unsigned int i = 0; i < 4; i++)
{
    vtkNew<vtkRenderWindow> renderWindow;
    renderWindow->SetSize(width, height);

    vtkNew<vtkRenderer> renderer;

    // Use the same initial camera for each renderer.
    if (i == 0)
    {
        camera = renderer->GetActiveCamera();
        camera->Azimuth(30);
        camera->Elevation(30);
    }
    else
    {
        renderer->SetActiveCamera(camera);
    }

    renderWindow->AddRenderer(renderer);

    vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;

    interactors.push_back(renderWindowInteractor);

    renderWindowInteractor->SetRenderWindow(renderWindow);
    renderWindow->Render();
    std::stringstream ss;
    ss << "MultipleRenderWindows " << i;
    renderWindow->SetWindowName(ss.str().c_str());
    renderWindow->SetPosition((i % 2) * w, h - (i / 2) * h);

    // Create a mapper and actor.
    vtkNew<vtkPolyDataMapper> mapper;
    mapper->SetInputConnection(sources[i]->GetOutputPort());

    vtkNew<vtkActor> actor;
    actor->SetMapper(mapper);
    //actor->GetProperty()->SetColor(colors->GetColor3d(actorColor[i]).GetData());

    renderer->AddActor(actor);
    renderer->AddActor(actorCommon);
    renderer->ResetCamera();

    running[i] = true;
    vtkNew<KeyPressInteractorStyle> style;
    styles.push_back(style);
    styles[i]->status = &running[i];
    interactors[i]->SetInteractorStyle(styles[i]);
    styles[i]->SetCurrentRenderer(renderer);
}

// Start the interaction.
if (simultaneousUpdate)
{
    // Changes in any window will be simultaneously reflected in the other
    // windows.
    interactors[0]->Initialize();
    // If all are running then process the commands.
    while (std::all_of(running.begin(), running.end(),
        [](bool i) { return i == true; }))
    {
        for (unsigned int i = 0; i < 4; i++)
        {
            if (running[i])
            {
                interactors[i]->ProcessEvents();
                interactors[i]->Render();
            }
            else
            {
                interactors[i]->TerminateApp();
                std::cout << "Window " << i << " has stopped running." << std::endl;
            }
        }
    }
}
else
{
    // Changes in any window will be reflected in the other
    // windows when you click in the window.
    interactors[0]->Start();
}

return EXIT_SUCCESS;

```

}

namespace {  
std::vector\<vtkSmartPointer\> GetSources()  
{  
std::vector\<vtkSmartPointer\> sources;  
for (unsigned i = 0; i \< 4; i++)  
{  
// Create a sphere.  
vtkNew sphere;  
sphere-\>SetCenter(0.0, 0.0, 0.0);  
sphere-\>Update();  
sources.push\_back(sphere);  
// Create a cone.  
vtkNew cone;  
cone-\>SetCenter(0.0, 0.0, 0.0);  
cone-\>SetDirection(0, 1, 0);  
cone-\>Update();  
sources.push\_back(cone);  
// Create a cube.  
vtkNew cube;  
cube-\>SetCenter(0.0, 0.0, 0.0);  
cube-\>Update();  
sources.push\_back(cube);  
// Create a cylinder.  
vtkNew cylinder;  
cylinder-\>SetCenter(0.0, 0.0, 0.0);  
cylinder-\>Update();  
sources.push\_back(cylinder);  
}  
return sources;  
}  
} // namespace  
why?

---

<div class="post-metadata">

### Author: ![Hurmean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hurmean/32/2964_2.png) [@Hurmean](https://discourse.vtk.org/u/Hurmean)
#### Post date: [June 23, 2021, 5:42am UTC](https://discourse.vtk.org/t/error-with-a-same-vtkactor-add-in-different-vtkrender/6045/4 "2021-06-23T05:42:32Z")

</div>

when I use vtkGenericOpenGLRenderWindow and QVTKOpenGLNativeWidget instead doesn’t work
