From b4fdc22a17df3762666440211d4cbd7dbf272c74 Mon Sep 17 00:00:00 2001 From: Marcel Pi <25649656+MarcelPiNacy@users.noreply.github.com> Date: Mon, 6 May 2024 13:45:30 +0200 Subject: [PATCH] Properly expose Actor.apply_texture. --- PythonAPI/carla/src/Actor.cpp | 42 +++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/PythonAPI/carla/src/Actor.cpp b/PythonAPI/carla/src/Actor.cpp index a217d87d3..87b2533d0 100644 --- a/PythonAPI/carla/src/Actor.cpp +++ b/PythonAPI/carla/src/Actor.cpp @@ -9,6 +9,9 @@ #include namespace ctm = carla::traffic_manager; +namespace cc = carla::client; +namespace cg = carla::geom; +namespace cr = carla::rpc; template static auto StdVectorToPyList(const std::vector &vec) { @@ -19,32 +22,49 @@ static auto StdVectorToPyList(const std::vector &vec) { return l; } -static auto GetSemanticTags(const carla::client::Actor &self) { +static auto GetSemanticTags(const cc::Actor &self) { const std::vector &tags = self.GetSemanticTags(); return StdVectorToPyList(tags); } -static void AddActorImpulse(carla::client::Actor &self, - const carla::geom::Vector3D &impulse) { +static void AddActorImpulse(cc::Actor &self, + const cg::Vector3D &impulse) { self.AddImpulse(impulse); } -static void AddActorForce(carla::client::Actor &self, - const carla::geom::Vector3D &force) { +static void AddActorForce(cc::Actor &self, + const cg::Vector3D &force) { self.AddForce(force); } -static auto GetGroupTrafficLights(carla::client::TrafficLight &self) { +static auto GetGroupTrafficLights(cc::TrafficLight &self) { auto values = self.GetGroupTrafficLights(); return StdVectorToPyList(values); } template -static void ApplyControl(carla::client::Walker &self, const ControlT &control) { +static void ApplyControl(cc::Walker &self, const ControlT &control) { self.ApplyControl(control); } -static auto GetLightBoxes(const carla::client::TrafficLight &self) { +static void ApplyTexture( + cc::Actor& self, + const carla::rpc::MaterialParameter& MaterialParameter, + const boost::python::object& Texture) +{ + boost::python::extract ext(Texture); + if (ext.check()) + return self.ApplyTexture(MaterialParameter, ext()); + + boost::python::extract ext_float(Texture); + if (ext_float.check()) + return self.ApplyTexture(MaterialParameter, ext_float()); + + throw std::invalid_argument( + "Invalid texture passed to apply_texture. Expected either a TextureColor or TextureFloatColor."); +} + +static auto GetLightBoxes(const cc::TrafficLight &self) { boost::python::list result; for (const auto &bb : self.GetLightBoxes()) { result.append(bb); @@ -53,10 +73,8 @@ static auto GetLightBoxes(const carla::client::TrafficLight &self) { } void export_actor() { + using namespace boost::python; - namespace cc = carla::client; - namespace cr = carla::rpc; - namespace ctm = carla::traffic_manager; enum_("ActorState") .value("Invalid", cr::ActorState::Invalid) @@ -106,7 +124,7 @@ void export_actor() { .def("set_simulate_physics", &cc::Actor::SetSimulatePhysics, (arg("enabled") = true)) .def("set_collisions", &cc::Actor::SetCollisions, (arg("enabled") = true)) .def("set_enable_gravity", &cc::Actor::SetEnableGravity, (arg("enabled") = true)) - .def("apply_texture", &cc::Actor::ApplyTexture, (arg("texture"))) + .def("apply_texture", &ApplyTexture, (arg("material_parameter"), arg("texture"))) .def("destroy", CALL_WITHOUT_GIL(cc::Actor, Destroy)) .def(self_ns::str(self_ns::self)) ;