diff --git a/AirLib/include/api/RpcLibClientBase.hpp b/AirLib/include/api/RpcLibClientBase.hpp index cc3914b6a1..47fdb7fa9b 100644 --- a/AirLib/include/api/RpcLibClientBase.hpp +++ b/AirLib/include/api/RpcLibClientBase.hpp @@ -152,8 +152,8 @@ namespace airlib void simSetKinematics(const Kinematics::State& state, bool ignore_collision, const std::string& vehicle_name = ""); msr::airlib::Environment::State simGetGroundTruthEnvironment(const std::string& vehicle_name = "") const; std::vector simSwapTextures(const std::string& tags, int tex_id = 0, int component_id = 0, int material_id = 0); - bool simSetObjectMaterial(const std::string& object_name, const std::string& material_name); - bool simSetObjectMaterialFromTexture(const std::string& object_name, const std::string& texture_path); + bool simSetObjectMaterial(const std::string& object_name, const std::string& material_name, const int component_id = 0); + bool simSetObjectMaterialFromTexture(const std::string& object_name, const std::string& texture_path, const int component_id = 0); // Recording APIs void startRecording(); diff --git a/AirLib/include/api/WorldSimApiBase.hpp b/AirLib/include/api/WorldSimApiBase.hpp index e7fc7c3754..833e590d7d 100644 --- a/AirLib/include/api/WorldSimApiBase.hpp +++ b/AirLib/include/api/WorldSimApiBase.hpp @@ -74,8 +74,8 @@ namespace airlib virtual bool setObjectScale(const std::string& object_name, const Vector3r& scale) = 0; virtual std::unique_ptr> swapTextures(const std::string& tag, int tex_id = 0, int component_id = 0, int material_id = 0) = 0; virtual bool setLightIntensity(const std::string& light_name, float intensity) = 0; - virtual bool setObjectMaterial(const std::string& object_name, const std::string& material_name) = 0; - virtual bool setObjectMaterialFromTexture(const std::string& object_name, const std::string& texture_path) = 0; + virtual bool setObjectMaterial(const std::string& object_name, const std::string& material_name, const int component_id = 0) = 0; + virtual bool setObjectMaterialFromTexture(const std::string& object_name, const std::string& texture_path, const int component_id = 0) = 0; virtual vector getMeshPositionVertexBuffers() const = 0; virtual bool createVoxelGrid(const Vector3r& position, const int& x_size, const int& y_size, const int& z_size, const float& res, const std::string& output_file) = 0; diff --git a/AirLib/src/api/RpcLibClientBase.cpp b/AirLib/src/api/RpcLibClientBase.cpp index f9ea4b59ec..36c0c1cf9b 100644 --- a/AirLib/src/api/RpcLibClientBase.cpp +++ b/AirLib/src/api/RpcLibClientBase.cpp @@ -493,14 +493,14 @@ __pragma(warning(disable : 4239)) return pimpl_->client.call("simSwapTextures", tags, tex_id, component_id, material_id).as>(); } - bool RpcLibClientBase::simSetObjectMaterial(const std::string& object_name, const std::string& material_name) + bool RpcLibClientBase::simSetObjectMaterial(const std::string& object_name, const std::string& material_name, const int component_id) { - return pimpl_->client.call("simSetObjectMaterial", object_name, material_name).as(); + return pimpl_->client.call("simSetObjectMaterial", object_name, material_name, component_id).as(); } - bool RpcLibClientBase::simSetObjectMaterialFromTexture(const std::string& object_name, const std::string& texture_path) + bool RpcLibClientBase::simSetObjectMaterialFromTexture(const std::string& object_name, const std::string& texture_path, const int component_id) { - return pimpl_->client.call("simSetObjectMaterialFromTexture", object_name, texture_path).as(); + return pimpl_->client.call("simSetObjectMaterialFromTexture", object_name, texture_path, component_id).as(); } bool RpcLibClientBase::simLoadLevel(const string& level_name) diff --git a/AirLib/src/api/RpcLibServerBase.cpp b/AirLib/src/api/RpcLibServerBase.cpp index 6d1fc7b50a..0710bd8d5a 100644 --- a/AirLib/src/api/RpcLibServerBase.cpp +++ b/AirLib/src/api/RpcLibServerBase.cpp @@ -477,12 +477,12 @@ namespace airlib return *getWorldSimApi()->swapTextures(tag, tex_id, component_id, material_id); }); - pimpl_->server.bind("simSetObjectMaterial", [&](const std::string& object_name, const std::string& material_name) -> bool { - return getWorldSimApi()->setObjectMaterial(object_name, material_name); + pimpl_->server.bind("simSetObjectMaterial", [&](const std::string& object_name, const std::string& material_name, const int component_id) -> bool { + return getWorldSimApi()->setObjectMaterial(object_name, material_name, component_id); }); - pimpl_->server.bind("simSetObjectMaterialFromTexture", [&](const std::string& object_name, const std::string& texture_path) -> bool { - return getWorldSimApi()->setObjectMaterialFromTexture(object_name, texture_path); + pimpl_->server.bind("simSetObjectMaterialFromTexture", [&](const std::string& object_name, const std::string& texture_path, const int component_id) -> bool { + return getWorldSimApi()->setObjectMaterialFromTexture(object_name, texture_path, component_id); }); pimpl_->server.bind("startRecording", [&]() -> void { diff --git a/PythonClient/airsim/client.py b/PythonClient/airsim/client.py index bcda726478..4f139ac552 100644 --- a/PythonClient/airsim/client.py +++ b/PythonClient/airsim/client.py @@ -190,31 +190,33 @@ def simSwapTextures(self, tags, tex_id = 0, component_id = 0, material_id = 0): """ return self.client.call("simSwapTextures", tags, tex_id, component_id, material_id) - def simSetObjectMaterial(self, object_name, material_name): + def simSetObjectMaterial(self, object_name, material_name, component_id = 0): """ Runtime Swap Texture API See https://microsoft.github.io/AirSim/retexturing/ for details Args: object_name (str): name of object to set material for material_name (str): name of material to set for object + component_id (int, optional) : index of material elements Returns: bool: True if material was set """ - return self.client.call("simSetObjectMaterial", object_name, material_name) + return self.client.call("simSetObjectMaterial", object_name, material_name, component_id) - def simSetObjectMaterialFromTexture(self, object_name, texture_path): + def simSetObjectMaterialFromTexture(self, object_name, texture_path, component_id = 0): """ Runtime Swap Texture API See https://microsoft.github.io/AirSim/retexturing/ for details Args: object_name (str): name of object to set material for texture_path (str): path to texture to set for object + component_id (int, optional) : index of material elements Returns: bool: True if material was set """ - return self.client.call("simSetObjectMaterialFromTexture", object_name, texture_path) + return self.client.call("simSetObjectMaterialFromTexture", object_name, texture_path, component_id) # time-of-day control @@ -1616,4 +1618,4 @@ def getCarControls(self, vehicle_name=''): CarControls: """ controls_raw = self.client.call('getCarControls', vehicle_name) - return CarControls.from_msgpack(controls_raw) + return CarControls.from_msgpack(controls_raw) \ No newline at end of file diff --git a/Unity/AirLibWrapper/AirsimWrapper/Source/WorldSimApi.cpp b/Unity/AirLibWrapper/AirsimWrapper/Source/WorldSimApi.cpp index 6bdbe80c5a..dac9eb939d 100644 --- a/Unity/AirLibWrapper/AirsimWrapper/Source/WorldSimApi.cpp +++ b/Unity/AirLibWrapper/AirsimWrapper/Source/WorldSimApi.cpp @@ -75,7 +75,7 @@ std::unique_ptr> WorldSimApi::swapTextures(const std::s return result; } -bool WorldSimApi::setObjectMaterialFromTexture(const std::string& object_name, const std::string& texture_path) +bool WorldSimApi::setObjectMaterialFromTexture(const std::string& object_name, const std::string& texture_path, const int component_id) { throw std::invalid_argument(common_utils::Utils::stringf( "setObjectMaterialFromTexture is not supported on unity") @@ -83,7 +83,7 @@ bool WorldSimApi::setObjectMaterialFromTexture(const std::string& object_name, c return false; } -bool WorldSimApi::setObjectMaterial(const std::string& object_name, const std::string& material_name) +bool WorldSimApi::setObjectMaterial(const std::string& object_name, const std::string& material_name, const int component_id) { throw std::invalid_argument(common_utils::Utils::stringf( "setObjectMaterial is not supported on unity") diff --git a/Unity/AirLibWrapper/AirsimWrapper/Source/WorldSimApi.h b/Unity/AirLibWrapper/AirsimWrapper/Source/WorldSimApi.h index def2b04ed8..e458798c28 100644 --- a/Unity/AirLibWrapper/AirsimWrapper/Source/WorldSimApi.h +++ b/Unity/AirLibWrapper/AirsimWrapper/Source/WorldSimApi.h @@ -40,8 +40,8 @@ class WorldSimApi : public msr::airlib::WorldSimApiBase virtual bool setLightIntensity(const std::string& light_name, float intensity) override; virtual std::unique_ptr> swapTextures(const std::string& tag, int tex_id = 0, int component_id = 0, int material_id = 0) override; - virtual bool setObjectMaterial(const std::string& object_name, const std::string& material_name) override; - virtual bool setObjectMaterialFromTexture(const std::string& object_name, const std::string& texture_path) override; + virtual bool setObjectMaterial(const std::string& object_name, const std::string& material_name, const int component_id = 0) override; + virtual bool setObjectMaterialFromTexture(const std::string& object_name, const std::string& texture_path, const int component_id = 0) override; virtual std::vector listSceneObjects(const std::string& name_regex) const override; virtual Pose getObjectPose(const std::string& object_name) const override; diff --git a/Unreal/Plugins/AirSim/Source/WorldSimApi.cpp b/Unreal/Plugins/AirSim/Source/WorldSimApi.cpp index c832051340..11e7b6cf97 100644 --- a/Unreal/Plugins/AirSim/Source/WorldSimApi.cpp +++ b/Unreal/Plugins/AirSim/Source/WorldSimApi.cpp @@ -476,10 +476,10 @@ std::unique_ptr> WorldSimApi::swapTextures(const std::s return swappedObjectNames; } -bool WorldSimApi::setObjectMaterialFromTexture(const std::string& object_name, const std::string& texture_path) +bool WorldSimApi::setObjectMaterialFromTexture(const std::string& object_name, const std::string& texture_path, const int component_id) { bool success = false; - UAirBlueprintLib::RunCommandOnGameThread([this, &object_name, &texture_path, &success]() { + UAirBlueprintLib::RunCommandOnGameThread([this, &object_name, &texture_path, &success, &component_id]() { if (!IsValid(simmode_->domain_rand_material_)) { UAirBlueprintLib::LogMessageString("Cannot find material for domain randomization", "", @@ -495,7 +495,7 @@ bool WorldSimApi::setObjectMaterialFromTexture(const std::string& object_name, c for (UStaticMeshComponent* staticMeshComponent : components) { UMaterialInstanceDynamic* dynamic_material = UMaterialInstanceDynamic::Create(simmode_->domain_rand_material_, staticMeshComponent); dynamic_material->SetTextureParameterValue("TextureParameter", texture_desired); - staticMeshComponent->SetMaterial(0, dynamic_material); + staticMeshComponent->SetMaterial(component_id, dynamic_material); } success = true; } @@ -511,10 +511,10 @@ bool WorldSimApi::setObjectMaterialFromTexture(const std::string& object_name, c return success; } -bool WorldSimApi::setObjectMaterial(const std::string& object_name, const std::string& material_name) +bool WorldSimApi::setObjectMaterial(const std::string& object_name, const std::string& material_name, const int component_id) { bool success = false; - UAirBlueprintLib::RunCommandOnGameThread([this, &object_name, &material_name, &success]() { + UAirBlueprintLib::RunCommandOnGameThread([this, &object_name, &material_name, &success, &component_id]() { AActor* actor = UAirBlueprintLib::FindActor(simmode_, FString(object_name.c_str())); UMaterial* material = static_cast(StaticLoadObject(UMaterial::StaticClass(), nullptr, *FString(material_name.c_str()))); @@ -528,7 +528,7 @@ bool WorldSimApi::setObjectMaterial(const std::string& object_name, const std::s TArray components; actor->GetComponents(components); for (UStaticMeshComponent* staticMeshComponent : components) { - staticMeshComponent->SetMaterial(0, material); + staticMeshComponent->SetMaterial(component_id, material); } success = true; } @@ -843,7 +843,6 @@ std::vector WorldSimApi::getWorldExtents() const return result; } - msr::airlib::CameraInfo WorldSimApi::getCameraInfo(const CameraDetails& camera_details) const { msr::airlib::CameraInfo info; diff --git a/Unreal/Plugins/AirSim/Source/WorldSimApi.h b/Unreal/Plugins/AirSim/Source/WorldSimApi.h index 7174d89459..a65a8187ef 100644 --- a/Unreal/Plugins/AirSim/Source/WorldSimApi.h +++ b/Unreal/Plugins/AirSim/Source/WorldSimApi.h @@ -50,8 +50,8 @@ class WorldSimApi : public msr::airlib::WorldSimApiBase virtual bool setLightIntensity(const std::string& light_name, float intensity) override; virtual std::unique_ptr> swapTextures(const std::string& tag, int tex_id = 0, int component_id = 0, int material_id = 0) override; - virtual bool setObjectMaterial(const std::string& object_name, const std::string& material_name) override; - virtual bool setObjectMaterialFromTexture(const std::string& object_name, const std::string& texture_path) override; + virtual bool setObjectMaterial(const std::string& object_name, const std::string& material_name, const int component_id = 0) override; + virtual bool setObjectMaterialFromTexture(const std::string& object_name, const std::string& texture_path, const int component_id = 0) override; virtual std::vector listSceneObjects(const std::string& name_regex) const override; virtual Pose getObjectPose(const std::string& object_name) const override; virtual bool setObjectPose(const std::string& object_name, const Pose& pose, bool teleport) override;