diff --git a/Docs/python_api.md b/Docs/python_api.md index 9e97342b8..19c0b428c 100644 --- a/Docs/python_api.md +++ b/Docs/python_api.md @@ -38,12 +38,15 @@ - `contains_attribute(key)` - `get_attribute(key)` - `set_attribute(key, value)` +- `__len__` +- `__iter__` ## `carla.ActorAttribute` -- `is_modifiable` +- `id` - `type` - `recommended_values` +- `is_modifiable` - `as_bool()` - `as_int()` - `as_float()` diff --git a/LibCarla/source/carla/client/ActorAttribute.h b/LibCarla/source/carla/client/ActorAttribute.h index 1f022da21..e8609157c 100644 --- a/LibCarla/source/carla/client/ActorAttribute.h +++ b/LibCarla/source/carla/client/ActorAttribute.h @@ -53,14 +53,8 @@ namespace client { Validate(); } - /// Set the value of this attribute. - /// - /// @throw InvalidAttributeValue if attribute is not modifiable. - /// @throw InvalidAttributeValue if format does not match this type. - void Set(std::string value); - - bool IsModifiable() const { - return _attribute.is_modifiable; + const std::string &GetId() const { + return _attribute.id; } rpc::ActorAttributeType GetType() const { @@ -71,6 +65,16 @@ namespace client { return _attribute.recommended_values; } + bool IsModifiable() const { + return _attribute.is_modifiable; + } + + /// Set the value of this attribute. + /// + /// @throw InvalidAttributeValue if attribute is not modifiable. + /// @throw InvalidAttributeValue if format does not match this type. + void Set(std::string value); + /// Cast the value to the given type. /// /// @throw BadAttributeCast if the cast fails. diff --git a/LibCarla/source/carla/client/ActorBlueprint.h b/LibCarla/source/carla/client/ActorBlueprint.h index 563b0558c..8b8686cf6 100644 --- a/LibCarla/source/carla/client/ActorBlueprint.h +++ b/LibCarla/source/carla/client/ActorBlueprint.h @@ -89,6 +89,10 @@ namespace client { _attributes.at(id).Set(std::move(value)); } + size_t size() const { + return _attributes.size(); + } + auto begin() const { return iterator::make_map_values_iterator(_attributes.begin()); } diff --git a/PythonAPI/source/libcarla/Blueprint.cpp b/PythonAPI/source/libcarla/Blueprint.cpp index 897f55794..452cb0e59 100644 --- a/PythonAPI/source/libcarla/Blueprint.cpp +++ b/PythonAPI/source/libcarla/Blueprint.cpp @@ -35,14 +35,42 @@ namespace carla { namespace client { std::ostream &operator<<(std::ostream &out, const Color &color) { - out << "Color(r=" << color.r - << ", g=" << color.g - << ", b=" << color.b << ')'; + out << "Color(" << int(color.r) << ',' << int(color.g) << ',' << int(color.b) << ')'; + return out; + } + + std::ostream &operator<<(std::ostream &out, const ActorAttribute &attr) { + using Type = carla::rpc::ActorAttributeType; + static_assert(static_cast(Type::SIZE) == 5u, "Please update this function."); + out << "ActorAttribute(id=" << attr.GetId(); + switch (attr.GetType()) { + case Type::Bool: + out << ",type=bool,value=" << (attr.As() ? "True" : "False"); + break; + case Type::Int: + out << ",type=int,value=" << attr.As(); + break; + case Type::Float: + out << ",type=float,value=" << attr.As(); + break; + case Type::String: + out << ",type=str,value=" << attr.As(); + break; + case Type::RGBColor: + out << ",type=Color,value=" << attr.As(); + break; + default: + out << ",INVALID"; + } + if (!attr.IsModifiable()) { + out << "(const)"; + } + out << ')'; return out; } std::ostream &operator<<(std::ostream &out, const ActorBlueprint &bp) { - out << "ActorBlueprint(id=" << bp.GetId() << "tags=" << bp.GetTags() << ')'; + out << "ActorBlueprint(id=" << bp.GetId() << ",tags=" << bp.GetTags() << ')'; return out; } @@ -80,11 +108,14 @@ void export_blueprint() { ; class_("ActorAttribute", no_init) - .add_property("is_modifiable", &cc::ActorAttribute::IsModifiable) + .add_property("id", +[](const cc::ActorAttribute &self) -> std::string { + return self.GetId(); + }) .add_property("type", &cc::ActorAttribute::GetType) .add_property("recommended_values", +[](const cc::ActorAttribute &self) -> std::vector { return self.GetRecommendedValues(); }) + .add_property("is_modifiable", &cc::ActorAttribute::IsModifiable) .def("as_bool", &cc::ActorAttribute::As) .def("as_int", &cc::ActorAttribute::As) .def("as_float", &cc::ActorAttribute::As) @@ -107,6 +138,7 @@ void export_blueprint() { .def("__int__", &cc::ActorAttribute::As) .def("__float__", &cc::ActorAttribute::As) .def("__str__", &cc::ActorAttribute::As) + .def(self_ns::str(self_ns::self)) ; class_("ActorBlueprint", no_init) @@ -121,6 +153,8 @@ void export_blueprint() { return self.GetAttribute(id); }) .def("set_attribute", &cc::ActorBlueprint::SetAttribute) + .def("__len__", &cc::ActorBlueprint::size) + .def("__iter__", range(&cc::ActorBlueprint::begin, &cc::ActorBlueprint::end)) .def(self_ns::str(self_ns::self)) ;