# Viewe a robot in compas\_view2

**URL:** <https://forum.compas-framework.org/t/viewe-a-robot-in-compas-view2/718>\
**Category:** Uncategorized\
**Created:** [July 22, 2023, 2:41pm UTC](https://forum.compas-framework.org/t/viewe-a-robot-in-compas-view2/718 "2023-07-22T14:41:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zac\_Zhuo\_Zhang](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.compas-framework.org/zac_zhuo_zhang/32/433_2.png) [@Zac\_Zhuo\_Zhang](https://forum.compas-framework.org/u/Zac_Zhuo_Zhang)\
**Post date:** [July 22, 2023, 2:41pm UTC](https://forum.compas-framework.org/t/viewe-a-robot-in-compas-view2/718/1 "2023-07-22T14:41:05Z")

</div>

Hi, I had an idea of visualizing a robot in `compas_view2` and its trajectory, with a slider changing the state. However, I have trouble adding a robot mesh in the view.

Knowing that `compas_view2` only supports `compas.data.Data`, I cannot add `compas_fab.robots.Robot` to the view, but only `compas.robots.RobotModel` (P.S. `compas` and `compad_fab` are a bit messy and confusing in this part of the concept for me). Yet, I still cannot put a robot mesh that **moves**.

1. I load the robot and initialized the app:

```auto
from compas_view2.app import App
from compas.robots import LocalPackageMeshLoader, RobotModel
import compas_fab

viewer = App()

urdf = compas_fab.get("universal_robot/ur_description/urdf/ur5.urdf")
model = RobotModel.from_urdf_file(urdf)
loader = LocalPackageMeshLoader(compas_fab.get("universal_robot"), "ur_description")
model.load_geometry(loader)

```

1. then, I try to change to model a bit, like scaling and re-configuring:

```auto
# Scale not working
model.scale(1000)

# Configure not working
for i in range(6): model.joints[i].position = 1

```

1. last, I view it by `viewer. run()`

 ![image](https://us1.discourse-cdn.com/flex015/uploads/compas_framework1/original/1X/3a112195111d6208e07469014c7fbad7803e5c5c.png)  
I can see the robot but it is always keeping this state.

I assume using `compas.robots.RobotModel` might not be a good idea, but I cannot figure out how to right this part of the code.

Look forward to any insight and thanks in advance.

---

<div class="post-metadata">

**Author:** ![Zac\_Zhuo\_Zhang](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.compas-framework.org/zac_zhuo_zhang/32/433_2.png) [@Zac\_Zhuo\_Zhang](https://forum.compas-framework.org/u/Zac_Zhuo_Zhang)\
**Post date:** [July 27, 2023, 10:24am UTC](https://forum.compas-framework.org/t/viewe-a-robot-in-compas-view2/718/2 "2023-07-27T10:24:43Z")

</div>

Below is the example code.

```auto

import compas_fab
from compas import json_load
from compas.robots import LocalPackageMeshLoader, RobotModel
from compas_view2.app import App

def robot_viewer(trajectory):

    viewer = App(enable_sidebar = True)

    # * Add geo to the scene -----------
    urdf = compas_fab.get("universal_robot/ur_description/urdf/ur5.urdf")
    model = RobotModel.from_urdf_file(urdf)
    loader = LocalPackageMeshLoader(compas_fab.get("universal_robot"), "ur_description")
    model.load_geometry(loader)

    model.scale(1000)
    robot_object = viewer.add(model)
    # * -------------------------------------------

    @viewer.slider(title="Trajectory", maxval=len(trajectory.points) - 1, step=1)
    def slide(value):

        robot_object.update()
        viewer.view.update()

    run = False

    @viewer.button(text="Confirm")
    def click():
        nonlocal run
        if viewer.confirm("The robot will move!") == True:
            run = True
        else:
            run = False

    viewer.run()
    if run == False:
        exit()

    return run

if __name__ == " __main__":

    trajectory = json_load(r"src\trajectory_test.json")

    run = robot_viewer( trajectory=trajectory)

```

---

<div class="post-metadata">

**Author:** ![Li\_Chen](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.compas-framework.org/li_chen/32/276_2.png) [@Li\_Chen](https://forum.compas-framework.org/u/Li_Chen)\
**Post date:** [August 8, 2023, 2:44pm UTC](https://forum.compas-framework.org/t/viewe-a-robot-in-compas-view2/718/3 "2023-08-08T14:44:13Z")

</div>

Hey Zac,

In view2, I recommend manipulate the robot through the `RobotObject` which is returned by `viewer.add()`, The `RobotModel` here is purely for getting the geometry. After added into the scene, you can use `.rotate_joint()` function from `RobotObject` to update it (Assuming your trajectory is a series of FK joint rotation values). You can also move/rotate/scale the RobotObject like any other object classes in view.
