Skip to content

Commit

Permalink
Merge pull request #4467 from SimtoReal/master
Browse files Browse the repository at this point in the history
add meterial element index
  • Loading branch information
zimmy87 authored May 2, 2022
2 parents 9b68c3d + cd0a687 commit 19ac0f2
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 30 deletions.
4 changes: 2 additions & 2 deletions AirLib/include/api/RpcLibClientBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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();
Expand Down
4 changes: 2 additions & 2 deletions AirLib/include/api/WorldSimApiBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ namespace airlib
virtual bool setObjectScale(const std::string& object_name, const Vector3r& scale) = 0;
virtual std::unique_ptr<std::vector<std::string>> 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<MeshPositionVertexBuffersResponse> 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;
Expand Down
8 changes: 4 additions & 4 deletions AirLib/src/api/RpcLibClientBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,14 +493,14 @@ __pragma(warning(disable : 4239))
return pimpl_->client.call("simSwapTextures", tags, tex_id, component_id, material_id).as<vector<string>>();
}

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<bool>();
return pimpl_->client.call("simSetObjectMaterial", object_name, material_name, component_id).as<bool>();
}

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<bool>();
return pimpl_->client.call("simSetObjectMaterialFromTexture", object_name, texture_path, component_id).as<bool>();
}

bool RpcLibClientBase::simLoadLevel(const string& level_name)
Expand Down
8 changes: 4 additions & 4 deletions AirLib/src/api/RpcLibServerBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 7 additions & 5 deletions PythonClient/airsim/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
4 changes: 2 additions & 2 deletions Unity/AirLibWrapper/AirsimWrapper/Source/WorldSimApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ std::unique_ptr<std::vector<std::string>> 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")
.c_str());
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")
Expand Down
4 changes: 2 additions & 2 deletions Unity/AirLibWrapper/AirsimWrapper/Source/WorldSimApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::vector<std::string>> 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<std::string> listSceneObjects(const std::string& name_regex) const override;
virtual Pose getObjectPose(const std::string& object_name) const override;

Expand Down
13 changes: 6 additions & 7 deletions Unreal/Plugins/AirSim/Source/WorldSimApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,10 @@ std::unique_ptr<std::vector<std::string>> 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",
"",
Expand All @@ -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;
}
Expand All @@ -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<AActor>(simmode_, FString(object_name.c_str()));
UMaterial* material = static_cast<UMaterial*>(StaticLoadObject(UMaterial::StaticClass(), nullptr, *FString(material_name.c_str())));

Expand All @@ -528,7 +528,7 @@ bool WorldSimApi::setObjectMaterial(const std::string& object_name, const std::s
TArray<UStaticMeshComponent*> components;
actor->GetComponents<UStaticMeshComponent>(components);
for (UStaticMeshComponent* staticMeshComponent : components) {
staticMeshComponent->SetMaterial(0, material);
staticMeshComponent->SetMaterial(component_id, material);
}
success = true;
}
Expand Down Expand Up @@ -843,7 +843,6 @@ std::vector<msr::airlib::GeoPoint> WorldSimApi::getWorldExtents() const

return result;
}

msr::airlib::CameraInfo WorldSimApi::getCameraInfo(const CameraDetails& camera_details) const
{
msr::airlib::CameraInfo info;
Expand Down
4 changes: 2 additions & 2 deletions Unreal/Plugins/AirSim/Source/WorldSimApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::vector<std::string>> 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<std::string> 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;
Expand Down

0 comments on commit 19ac0f2

Please sign in to comment.