diff --git a/Gems/ROS2/Code/Source/RobotImporter/SDFormat/Hooks/ROS2CameraSensorHook.cpp b/Gems/ROS2/Code/Source/RobotImporter/SDFormat/Hooks/ROS2CameraSensorHook.cpp index bbe5fc619..3d400adae 100644 --- a/Gems/ROS2/Code/Source/RobotImporter/SDFormat/Hooks/ROS2CameraSensorHook.cpp +++ b/Gems/ROS2/Code/Source/RobotImporter/SDFormat/Hooks/ROS2CameraSensorHook.cpp @@ -73,8 +73,11 @@ namespace ROS2::SDFormat sensorConfiguration, "depth_camera_info", CameraConstants::CameraInfoMessageType, CameraConstants::DepthInfoConfig); } - if (Utils::CreateComponent(entity) && - Utils::CreateComponent(entity, sensorConfiguration, cameraConfiguration)) + // Create required components + Utils::CreateComponent(entity); + + // Create Camera component + if (Utils::CreateComponent(entity, sensorConfiguration, cameraConfiguration)) { return AZ::Success(); } diff --git a/Gems/ROS2/Code/Source/RobotImporter/SDFormat/Hooks/ROS2ImuSensorHook.cpp b/Gems/ROS2/Code/Source/RobotImporter/SDFormat/Hooks/ROS2ImuSensorHook.cpp index 4835ee018..aa135ab2d 100644 --- a/Gems/ROS2/Code/Source/RobotImporter/SDFormat/Hooks/ROS2ImuSensorHook.cpp +++ b/Gems/ROS2/Code/Source/RobotImporter/SDFormat/Hooks/ROS2ImuSensorHook.cpp @@ -81,9 +81,12 @@ namespace ROS2::SDFormat const AZStd::string messageType = "sensor_msgs::msg::Imu"; Utils::AddTopicConfiguration(sensorConfiguration, "imu", messageType, messageType); - if (Utils::CreateComponent(entity) && - Utils::CreateComponent(entity) && - Utils::CreateComponent(entity, sensorConfiguration, imuConfiguration)) + // Create required components + Utils::CreateComponent(entity); + Utils::CreateComponent(entity); + + // Create Imu component + if (Utils::CreateComponent(entity, sensorConfiguration, imuConfiguration)) { return AZ::Success(); } diff --git a/Gems/ROS2/Code/Source/RobotImporter/SDFormat/Hooks/ROS2LidarSensorHook.cpp b/Gems/ROS2/Code/Source/RobotImporter/SDFormat/Hooks/ROS2LidarSensorHook.cpp index d4353d5cc..fdd09cc64 100644 --- a/Gems/ROS2/Code/Source/RobotImporter/SDFormat/Hooks/ROS2LidarSensorHook.cpp +++ b/Gems/ROS2/Code/Source/RobotImporter/SDFormat/Hooks/ROS2LidarSensorHook.cpp @@ -68,8 +68,11 @@ namespace ROS2::SDFormat lidarConfiguration.m_lidarParameters.m_minRange = lidarSensor->RangeMin(); lidarConfiguration.m_lidarParameters.m_maxRange = lidarSensor->RangeMax(); - if (Utils::CreateComponent(entity) && - Utils::CreateComponent(entity, sensorConfiguration, lidarConfiguration)) + // Create required components + Utils::CreateComponent(entity); + + // Create Lidar component + if (Utils::CreateComponent(entity, sensorConfiguration, lidarConfiguration)) { return AZ::Success(); } diff --git a/Gems/ROS2/Code/Source/RobotImporter/SDFormat/ROS2SensorHooksUtils.h b/Gems/ROS2/Code/Source/RobotImporter/SDFormat/ROS2SensorHooksUtils.h index 368d7fdfd..a11aa414c 100644 --- a/Gems/ROS2/Code/Source/RobotImporter/SDFormat/ROS2SensorHooksUtils.h +++ b/Gems/ROS2/Code/Source/RobotImporter/SDFormat/ROS2SensorHooksUtils.h @@ -42,6 +42,12 @@ namespace ROS2::SDFormat template AZ::Component* CreateComponent(AZ::Entity& entity, Args&&... args) { + // Do not create a component if the same type is already added. + if (entity.FindComponent()) + { + return nullptr; + } + // Create component. // If it's not an "editor component" then wrap it in a GenericComponentWrapper. AZ::Component* component = nullptr; @@ -59,7 +65,7 @@ namespace ROS2::SDFormat if (component) { - if (!entity.AddComponent(component)) + if (!entity.IsComponentReadyToAdd(component) || !entity.AddComponent(component)) { delete component; component = nullptr;