Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting the id or name of the newly spawned prop through PythonAPI #8611

Open
MasoudJTehrani opened this issue Jan 29, 2025 · 3 comments
Open

Comments

@MasoudJTehrani
Copy link

Hello everyone,
I used the PythonAPI to spawn a prop, namely, the "static.prop.ironplank". How can I then get the ID or the name of the prop in my current world?

I used this code:

names = world.get_names_of_all_objects()
    for n in sorted(names):
        print(n)

and manually searched through the values and found that my prop is called "StaticMeshActor_2147479215".
How can I do it without manually searching through the names of all objects?

This is the code that I used to spawn the prop

prop = blueprints.find('static.prop.ironplank')
    location = carla.Location(x=80, y=108, z=7)
    ts = world.try_spawn_actor(prop, carla.Transform(location))

and if I print ts I get:
Actor(id=26, type=static.prop.ironplank)
Which is not what I am looking for

@PatrickPromitzer
Copy link

Hi,
what do you want to do with that information?

If you want to spawn an actor and find the actor instance at a later time, you can use the "role_name" attribute.

client: carla.Client = carla.Client("127.0.0.1", 2000)
world: carla.World = client.get_world()
blueprints = world.get_blueprint_library()
prop = blueprints.find("static.prop.ironplank")

prop.set_attribute('role_name', 'hero')
ts = world.try_spawn_actor(prop, carla.Transform(location))

# at  later time

actor_list = world.get_actors()

your_actor = None
for actor in actor_list:
    if "role_name" not in actor.attributes:
        continue
    actor_role: str = actor.attributes["role_name"]
    if actor_role == "hero":
        your_actor = actor

It used role_name with vehicles, but it should work with all actors.

@MasoudJTehrani
Copy link
Author

Hi,
I want to then change its texture using
world.apply_color_texture_to_object('StaticMeshActor_2147479217', carla.MaterialParameter.Diffuse, texture)
and unfortunately, your solution is not applicable in this case.
I guess I have to either use UE editor to change its name or just look for a StaticMeshActor that has the longest number in front of it.

@PatrickPromitzer
Copy link

Hi,
you could look in the actor.attributes if the object name is included, or maybe it is somewhere else in the actor.
I would need to check the actor class myself to be sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants