Skip to content

Commit ceea413

Browse files
authored
Merge pull request #3940 from rajat2004/list-assets
Add API to list all assets
2 parents de1c8ae + 92a887b commit ceea413

File tree

11 files changed

+102
-14
lines changed

11 files changed

+102
-14
lines changed

AirLib/include/api/RpcLibClientBase.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ namespace airlib
6565
Vector3r simGetObjectScale(const std::string& object_name) const;
6666
bool simSetObjectPose(const std::string& object_name, const Pose& pose, bool teleport = true);
6767
bool simSetObjectScale(const std::string& object_name, const Vector3r& scale);
68+
std::string simSpawnObject(const std::string& object_name, const std::string& load_component, const Pose& pose,
69+
const Vector3r& scale, bool physics_enabled);
70+
bool simDestroyObject(const std::string& object_name);
6871

6972
//task management APIs
7073
void cancelLastTask(const std::string& vehicle_name = "");
@@ -144,6 +147,8 @@ namespace airlib
144147

145148
std::string getSettingsString() const;
146149

150+
std::vector<std::string> simListAssets() const;
151+
147152
protected:
148153
void* getClient();
149154
const void* getClient() const;

AirLib/include/api/WorldSimApiBase.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace airlib
3434
virtual bool loadLevel(const std::string& level_name) = 0;
3535
virtual string spawnObject(const std::string& object_name, const std::string& load_component, const Pose& pose, const Vector3r& scale, bool physics_enabled, bool is_blueprint) = 0;
3636
virtual bool destroyObject(const std::string& object_name) = 0;
37+
virtual std::vector<std::string> listAssets() const = 0;
3738

3839
virtual bool isPaused() const = 0;
3940
virtual void reset() = 0;

AirLib/src/api/RpcLibClientBase.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,17 @@ __pragma(warning(disable : 4239))
431431
return pimpl_->client.call("simLoadLevel", level_name).as<bool>();
432432
}
433433

434+
std::string RpcLibClientBase::simSpawnObject(const std::string& object_name, const std::string& load_component, const Pose& pose,
435+
const Vector3r& scale, bool physics_enabled)
436+
{
437+
return pimpl_->client.call("simSpawnObject", object_name, load_component, RpcLibAdaptorsBase::Pose(pose), RpcLibAdaptorsBase::Vector3r(scale), physics_enabled).as<std::string>();
438+
}
439+
440+
bool RpcLibClientBase::simDestroyObject(const std::string& object_name)
441+
{
442+
return pimpl_->client.call("simDestroyObject", object_name).as<bool>();
443+
}
444+
434445
msr::airlib::Vector3r RpcLibClientBase::simGetObjectScale(const std::string& object_name) const
435446
{
436447
return pimpl_->client.call("simGetObjectScale", object_name).as<RpcLibAdaptorsBase::Vector3r>().to();
@@ -543,6 +554,11 @@ __pragma(warning(disable : 4239))
543554
return pimpl_->client.call("getSettingsString").as<std::string>();
544555
}
545556

557+
std::vector<std::string> RpcLibClientBase::simListAssets() const
558+
{
559+
return pimpl_->client.call("simListAssets").as<std::vector<std::string>>();
560+
}
561+
546562
void* RpcLibClientBase::getClient()
547563
{
548564
return &pimpl_->client;

AirLib/src/api/RpcLibServerBase.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,10 @@ namespace airlib
310310
return getWorldSimApi()->destroyObject(object_name);
311311
});
312312

313+
pimpl_->server.bind("simListAssets", [&]() -> std::vector<std::string> {
314+
return getWorldSimApi()->listAssets();
315+
});
316+
313317
pimpl_->server.bind("simGetObjectPose", [&](const std::string& object_name) -> RpcLibAdaptorsBase::Pose {
314318
const auto& pose = getWorldSimApi()->getObjectPose(object_name);
315319
return RpcLibAdaptorsBase::Pose(pose);

PythonClient/airsim/client.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def simContinueForTime(self, seconds):
110110
seconds (float): Time to run the simulation for
111111
"""
112112
self.client.call('simContinueForTime', seconds)
113-
113+
114114
def simContinueForFrames(self, frames):
115115
"""
116116
Continue (or resume if paused) the simulation for the specified number of frames, after which the simulation will be paused.
@@ -305,7 +305,7 @@ def simGetImages(self, requests, vehicle_name = '', external = False):
305305
"""
306306
responses_raw = self.client.call('simGetImages', requests, vehicle_name, external)
307307
return [ImageResponse.from_msgpack(response_raw) for response_raw in responses_raw]
308-
308+
309309
def simTestLineOfSightToPoint(self, point, vehicle_name = ''):
310310
"""
311311
Returns whether the target point is visible from the perspective of the inputted vehicle
@@ -318,7 +318,7 @@ def simTestLineOfSightToPoint(self, point, vehicle_name = ''):
318318
[bool]: Success
319319
"""
320320
return self.client.call('simTestLineOfSightToPoint', point, vehicle_name)
321-
321+
322322
def simTestLineOfSightBetweenPoints(self, point1, point2):
323323
"""
324324
Returns whether the target point is visible from the perspective of the source point
@@ -331,7 +331,7 @@ def simTestLineOfSightBetweenPoints(self, point1, point2):
331331
[bool]: Success
332332
"""
333333
return self.client.call('simTestLineOfSightBetweenPoints', point1, point2)
334-
334+
335335
def simGetWorldExtents(self):
336336
"""
337337
Returns a list of GeoPoints representing the minimum and maximum extents of the world
@@ -483,7 +483,7 @@ def simListSceneObjects(self, name_regex = '.*'):
483483
list[str]: List containing all the names
484484
"""
485485
return self.client.call('simListSceneObjects', name_regex)
486-
486+
487487
def simLoadLevel(self, level_name):
488488
"""
489489
Loads a level specified by its name
@@ -496,28 +496,37 @@ def simLoadLevel(self, level_name):
496496
"""
497497
return self.client.call('simLoadLevel', level_name)
498498

499+
def simListAssets(self):
500+
"""
501+
Lists all the assets present in the Asset Registry
502+
503+
Returns:
504+
list[str]: Names of all the assets
505+
"""
506+
return self.client.call('simListAssets')
507+
499508
def simSpawnObject(self, object_name, asset_name, pose, scale, physics_enabled=False, is_blueprint=False):
500509
"""Spawned selected object in the world
501-
510+
502511
Args:
503512
object_name (str): Desired name of new object
504513
asset_name (str): Name of asset(mesh) in the project database
505514
pose (airsim.Pose): Desired pose of object
506515
scale (airsim.Vector3r): Desired scale of object
507516
physics_enabled (bool, optional): Whether to enable physics for the object
508517
is_blueprint (bool, optional): Whether to spawn a blueprint or an actor
509-
518+
510519
Returns:
511520
str: Name of spawned object, in case it had to be modified
512521
"""
513522
return self.client.call('simSpawnObject', object_name, asset_name, pose, scale, physics_enabled, is_blueprint)
514523

515524
def simDestroyObject(self, object_name):
516525
"""Removes selected object from the world
517-
526+
518527
Args:
519528
object_name (str): Name of object to be removed
520-
529+
521530
Returns:
522531
bool: True if object is queued up for removal
523532
"""
@@ -567,7 +576,7 @@ def simAddDetectionFilterMeshName(self, camera_name, image_type, mesh_name, vehi
567576
568577
"""
569578
self.client.call('simAddDetectionFilterMeshName', camera_name, image_type, mesh_name, vehicle_name, external)
570-
579+
571580
def simSetDetectionFilterRadius(self, camera_name, image_type, radius_cm, vehicle_name = '', external = False):
572581
"""
573582
Set detection radius for all cameras
@@ -580,7 +589,7 @@ def simSetDetectionFilterRadius(self, camera_name, image_type, radius_cm, vehicl
580589
external (bool, optional): Whether the camera is an External Camera
581590
"""
582591
self.client.call('simSetDetectionFilterRadius', camera_name, image_type, radius_cm, vehicle_name, external)
583-
592+
584593
def simClearDetectionMeshNames(self, camera_name, image_type, vehicle_name = '', external = False):
585594
"""
586595
Clear all mesh names from detection filter
@@ -654,7 +663,7 @@ def simGetDistortionParams(self, camera_name, vehicle_name = '', external = Fals
654663
Returns:
655664
List (float): List of distortion parameter values corresponding to K1, K2, K3, P1, P2 respectively.
656665
"""
657-
666+
658667
return self.client.call('simGetDistortionParams', str(camera_name), vehicle_name, external)
659668

660669
def simSetDistortionParams(self, camera_name, distortion_params, vehicle_name = '', external = False):
@@ -975,7 +984,7 @@ def simSetWind(self, wind):
975984
Set simulated wind, in World frame, NED direction, m/s
976985
977986
Args:
978-
wind (Vector3r): Wind, in World frame, NED direction, in m/s
987+
wind (Vector3r): Wind, in World frame, NED direction, in m/s
979988
"""
980989
self.client.call('simSetWind', wind)
981990

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import setup_path
2+
import airsim
3+
import random
4+
import time
5+
6+
client = airsim.VehicleClient()
7+
client.confirmConnection()
8+
9+
assets = client.simListAssets()
10+
print(f"Assets: {assets}")
11+
12+
scale = airsim.Vector3r(1.0, 1.0, 1.0)
13+
14+
# asset_name = random.choice(assets)
15+
asset_name = '1M_Cube_Chamfer'
16+
17+
desired_name = f"{asset_name}_spawn_{random.randint(0, 100)}"
18+
pose = airsim.Pose(position_val=airsim.Vector3r(5.0, 0.0, 0.0))
19+
20+
obj_name = client.simSpawnObject(desired_name, asset_name, pose, scale, True)
21+
22+
print(f"Created object {obj_name} from asset {asset_name} "
23+
f"at pose {pose}, scale {scale}")
24+
25+
all_objects = client.simListSceneObjects()
26+
if obj_name not in all_objects:
27+
print(f"Object {obj_name} not present!")
28+
29+
time.sleep(10.0)
30+
31+
print(f"Destroying {obj_name}")
32+
client.simDestroyObject(obj_name)

Unity/AirLibWrapper/AirsimWrapper/Source/WorldSimApi.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,12 @@ std::vector<msr::airlib::DetectionInfo> WorldSimApi::getDetections(ImageCaptureB
397397
return std::vector<msr::airlib::DetectionInfo>();
398398
}
399399

400+
std::vector<std::string> WorldSimApi::listAssets() const
401+
{
402+
throw std::invalid_argument(common_utils::Utils::stringf(
403+
"listAssets API is not supported on Unity")
404+
.c_str());
405+
return {};
406+
}
407+
400408
#pragma endregion

Unity/AirLibWrapper/AirsimWrapper/Source/WorldSimApi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class WorldSimApi : public msr::airlib::WorldSimApiBase
2020
virtual bool loadLevel(const std::string& level_name) override { return false; };
2121
virtual std::string spawnObject(const std::string& object_name, const std::string& load_component, const Pose& pose, const Vector3r& scale, bool physics_enabled, bool is_blueprint) override { return ""; };
2222
virtual bool destroyObject(const std::string& object_name) override { return false; };
23+
virtual std::vector<std::string> listAssets() const override;
2324

2425
virtual bool isPaused() const override;
2526
virtual void reset() override;

Unreal/Plugins/AirSim/Source/AirBlueprintLib.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ void UAirBlueprintLib::GenerateAssetRegistryMap(const UObject* context, TMap<FSt
248248
AssetRegistryModule.Get().GetAssets(Filter, AssetData);
249249

250250
UObject* LoadObject = NULL;
251-
for (auto asset : AssetData) {
251+
for (const auto& asset : AssetData) {
252252
FString asset_name = asset.AssetName.ToString();
253253
asset_map.Add(asset_name, asset);
254254
}

Unreal/Plugins/AirSim/Source/WorldSimApi.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ bool WorldSimApi::destroyObject(const std::string& object_name)
8585
return result;
8686
}
8787

88+
std::vector<std::string> WorldSimApi::listAssets() const
89+
{
90+
std::vector<std::string> all_assets;
91+
92+
for (const TPair<FString, FAssetData>& pair : simmode_->asset_map) {
93+
all_assets.push_back(std::string(TCHAR_TO_UTF8(*pair.Key)));
94+
}
95+
96+
return all_assets;
97+
}
98+
8899
std::string WorldSimApi::spawnObject(const std::string& object_name, const std::string& load_object, const WorldSimApi::Pose& pose, const WorldSimApi::Vector3r& scale, bool physics_enabled, bool is_blueprint)
89100
{
90101
FString asset_name(load_object.c_str());

0 commit comments

Comments
 (0)