Add actor component getters to the PythonAPI

Adds functions:
 - get_component_world_transform
 - get_component_relative_transform

to the Python API to acces the transform of actor components by name
This commit is contained in:
Aperiss 2024-02-09 18:53:10 +01:00 committed by Blyron
parent fe117ef1e4
commit 0b13c72d63
9 changed files with 130 additions and 0 deletions

View File

@ -32,6 +32,14 @@ namespace client {
return GetEpisode().Lock()->GetActorAcceleration(*this);
}
geom::Transform Actor::GetComponentWorldTransform(const std::string componentName) const {
return GetEpisode().Lock()->GetActorComponentWorldTransform(*this, componentName);
}
geom::Transform Actor::GetComponentRelativeTransform(const std::string componentName) const {
return GetEpisode().Lock()->GetActorComponentRelativeTransform(*this, componentName);
}
void Actor::SetLocation(const geom::Location &location) {
GetEpisode().Lock()->SetActorLocation(*this, location);
}

View File

@ -60,6 +60,10 @@ namespace client {
/// acceleration calculated after the actor's velocity.
geom::Vector3D GetAcceleration() const;
geom::Transform GetComponentWorldTransform(const std::string componentName) const;
geom::Transform GetComponentRelativeTransform(const std::string componentName) const;
/// Teleport the actor to @a location.
void SetLocation(const geom::Location &location);

View File

@ -408,6 +408,14 @@ namespace detail {
_pimpl->AsyncCall("add_actor_torque", actor, vector);
}
geom::Transform Client::GetActorComponentWorldTransform(rpc::ActorId actor, const std::string componentName) {
return _pimpl->CallAndWait<geom::Transform>("get_actor_component_world_transform", actor, componentName);
}
geom::Transform Client::GetActorComponentRelativeTransform(rpc::ActorId actor, const std::string componentName) {
return _pimpl->CallAndWait<geom::Transform>("get_actor_component_relative_transform", actor, componentName);
}
void Client::SetActorSimulatePhysics(rpc::ActorId actor, const bool enabled) {
_pimpl->CallAndWait<void>("set_actor_simulate_physics", actor, enabled);
}

View File

@ -88,6 +88,7 @@ namespace detail {
void DestroyTrafficManager(uint16_t port) const;
void SetTimeout(time_duration timeout);
time_duration GetTimeout() const;
@ -232,6 +233,14 @@ namespace detail {
rpc::ActorId actor,
const geom::Vector3D &vector);
geom::Transform GetActorComponentWorldTransform(
rpc::ActorId actor,
const std::string componentName);
geom::Transform GetActorComponentRelativeTransform(
rpc::ActorId actor,
const std::string componentName);
void SetActorSimulatePhysics(
rpc::ActorId actor,
bool enabled);

View File

@ -438,6 +438,14 @@ namespace detail {
return GetActorSnapshot(actor).acceleration;
}
geom::Transform GetActorComponentWorldTransform(const Actor &actor, const std::string componentName) {
return _client.GetActorComponentWorldTransform(actor.GetId(), componentName);
}
geom::Transform GetActorComponentRelativeTransform(const Actor &actor, std::string componentName) {
return _client.GetActorComponentRelativeTransform(actor.GetId(), componentName);
}
void SetActorLocation(Actor &actor, const geom::Location &location) {
_client.SetActorLocation(actor.GetId(), location);
}

View File

@ -113,6 +113,8 @@ void export_actor() {
.def("get_velocity", &cc::Actor::GetVelocity)
.def("get_angular_velocity", &cc::Actor::GetAngularVelocity)
.def("get_acceleration", &cc::Actor::GetAcceleration)
.def("get_component_world_transform", &cc::Actor::GetComponentWorldTransform, (arg("component_name")))
.def("get_component_relative_transform", &cc::Actor::GetComponentRelativeTransform, (arg("component_name")))
.def("set_location", &cc::Actor::SetLocation, (arg("location")))
.def("set_transform", &cc::Actor::SetTransform, (arg("transform")))
.def("set_target_velocity", &cc::Actor::SetTargetVelocity, (arg("velocity")))

View File

@ -1274,6 +1274,94 @@ BIND_SYNC(is_sensor_enabled_for_ros) << [this](carla::streaming::detail::stream_
return R<void>::Success();
};
BIND_SYNC(get_actor_component_world_transform) << [this](
cr::ActorId ActorId,
const std::string componentName) -> R<cr::Transform>
{
REQUIRE_CARLA_EPISODE();
FCarlaActor* CarlaActor = Episode->FindCarlaActor(ActorId);
if (!CarlaActor)
{
return RespondError(
"get_actor_component_world_transform",
ECarlaServerResponse::ActorNotFound,
" Actor Id: " + FString::FromInt(ActorId));
}
else
{
TArray<UActorComponent*> Components;
CarlaActor->GetActor()->GetComponents(Components);
USceneComponent* Component;
for(auto Cmp : Components)
{
if(USceneComponent* SCMP = Cast<USceneComponent>(Cmp))
{
if(SCMP->GetName() == componentName.c_str())
{
Component = SCMP;
break;
}
}
}
if(!Component)
{
return RespondError(
"get_actor_component_world_transform",
ECarlaServerResponse::ComponentNotFound,
" Component Name: " + FString(componentName.c_str()));
}
FTransform ComponentWorldTransform = Component->GetComponentTransform();
return cr::Transform(ComponentWorldTransform);
}
};
BIND_SYNC(get_actor_component_relative_transform) << [this](
cr::ActorId ActorId,
const std::string componentName) -> R<cr::Transform>
{
REQUIRE_CARLA_EPISODE();
FCarlaActor* CarlaActor = Episode->FindCarlaActor(ActorId);
if (!CarlaActor)
{
return RespondError(
"get_actor_component_relative_transform",
ECarlaServerResponse::ActorNotFound,
" Actor Id: " + FString::FromInt(ActorId));
}
else
{
TArray<UActorComponent*> Components;
CarlaActor->GetActor()->GetComponents(Components);
USceneComponent* Component;
for(auto Cmp : Components)
{
if(USceneComponent* SCMP = Cast<USceneComponent>(Cmp))
{
if(SCMP->GetName() == componentName.c_str())
{
Component = SCMP;
break;
}
}
}
if(!Component)
{
return RespondError(
"get_actor_component_world_transform",
ECarlaServerResponse::ComponentNotFound,
" Component Name: " + FString(componentName.c_str()));
}
FTransform ComponentRelativeTransform = Component->GetRelativeTransform();
return cr::Transform(ComponentRelativeTransform);
}
};
BIND_SYNC(get_physics_control) << [this](
cr::ActorId ActorId) -> R<cr::VehiclePhysicsControl>
{

View File

@ -14,6 +14,8 @@ FString CarlaGetStringError(ECarlaServerResponse Response)
return "Sucess";
case ECarlaServerResponse::ActorNotFound:
return "Actor could not be found in the registry";
case ECarlaServerResponse::ComponentNotFound:
return "Component could not be found in this actor";
case ECarlaServerResponse::ActorTypeMismatch:
return "Actor does not match the expected type";
case ECarlaServerResponse::MissingActor:

View File

@ -10,6 +10,7 @@ enum class ECarlaServerResponse
{
Success,
ActorNotFound,
ComponentNotFound,
ActorTypeMismatch,
FunctionNotSupported,
NullActor,