Skip to content

Error on example of "Object manipulation" on Robobosimpy wiki #1

@HectorAldao

Description

@HectorAldao

Error

The example "Sample object manipulation code" from the Robobosimpy subsection of the wiki throws this error:

Traceback (most recent call last):
  File "robobosim.py", line 14, in <module>
    object_one = objects[0]
                 ~~~~~~~^^^
TypeError: 'dict_keys' object is not subscriptable

sim.getObjects() now returns a dict_keys, so the code in the wiki no longer works.

Solution

Convert objects to a list before indexing:

# Import the library
from robobosim.RoboboSim import RoboboSim

# Connect to the RoboboSim
IP = "localhost"
sim = RoboboSim(IP)
sim.connect()

sim.wait(0.5)

# Get the interactable objects of the scene
objects = sim.getObjects()
if objects is not None and len(objects) > 0:
    # Get the first object of the list, provided that there is at least one
    object_one = list(objects)[0]
    print(object_one)
    # Get the object's absolute position
    loc = sim.getObjectLocation(object_one)
    print(loc["position"])
    sim.wait(0.5)

    # Move the selected object +50 mm along the X axis
    pos = loc['position']
    pos["x"] += 50
    sim.setObjectLocation(object_one, loc['position'])
    sim.wait(0.5)

sim.disconnect()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions