| 228 | } |
| 229 | |
| 230 | CreateAssetResult ImportModel::Import(CreateAssetContext& context) |
| 231 | { |
| 232 | // Get import options |
| 233 | Options options; |
| 234 | if (context.CustomArg != nullptr) |
| 235 | { |
| 236 | // Copy import options from argument |
| 237 | options = *static_cast<Options*>(context.CustomArg); |
| 238 | } |
| 239 | else |
| 240 | { |
| 241 | // Restore the previous settings or use default ones |
| 242 | if (!TryGetImportOptions(context.TargetAssetPath, options)) |
| 243 | { |
| 244 | LOG(Warning, "Missing model import options. Using default values."); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // Import model file |
| 249 | ModelData* data = options.Cached ? options.Cached->Data : nullptr; |
| 250 | ModelData dataThis; |
| 251 | Array<IGrouping<StringView, MeshData*>>* meshesByNamePtr = options.Cached ? (Array<IGrouping<StringView, MeshData*>>*)options.Cached->MeshesByName : nullptr; |
| 252 | Array<IGrouping<StringView, MeshData*>> meshesByNameThis; |
| 253 | String autoImportOutput; |
| 254 | if (!data) |
| 255 | { |
| 256 | String errorMsg; |
| 257 | autoImportOutput = StringUtils::GetDirectoryName(context.TargetAssetPath); |
| 258 | autoImportOutput /= options.SubAssetFolder.HasChars() ? options.SubAssetFolder.TrimTrailing() : String(StringUtils::GetFileNameWithoutExtension(context.InputPath)); |
| 259 | if (ModelTool::ImportModel(context.InputPath, dataThis, options, errorMsg, autoImportOutput)) |
| 260 | { |
| 261 | LOG(Error, "Cannot import model file. {0}", errorMsg); |
| 262 | return CreateAssetResult::Error; |
| 263 | } |
| 264 | data = &dataThis; |
| 265 | |
| 266 | // Group meshes by the name (the same mesh name can be used by multiple meshes that use different materials) |
| 267 | if (data->LODs.Count() != 0) |
| 268 | { |
| 269 | const Function<StringView(MeshData* const&)> f = [](MeshData* const& x) -> StringView |
| 270 | { |
| 271 | return x->Name; |
| 272 | }; |
| 273 | ArrayExtensions::GroupBy(data->LODs[0].Meshes, f, meshesByNameThis); |
| 274 | Sorting::QuickSort(meshesByNameThis.Get(), meshesByNameThis.Count(), &SortMeshGroups); |
| 275 | } |
| 276 | meshesByNamePtr = &meshesByNameThis; |
| 277 | } |
| 278 | Array<IGrouping<StringView, MeshData*>>& meshesByName = *meshesByNamePtr; |
| 279 | |
| 280 | // Import objects from file separately |
| 281 | ModelTool::Options::CachedData cached = { data, (void*)meshesByNamePtr }; |
| 282 | Array<PrefabObject> prefabObjects; |
| 283 | if (options.Type == ModelTool::ModelType::Prefab) |
| 284 | { |
| 285 | // Normalize options |
| 286 | options.SplitObjects = false; |
| 287 | options.ObjectIndex = -1; |
nothing calls this directly
no test coverage detected