Asset import OnAssignMaterialModel callback. Existing material return not work

Hi!) I have a question. The situation is that there are import settings for different paths. Imported fbx assets on these paths should not create materials, we want to return the pre-created material set in the import settings scriptable objects.

The question is that it seems to work only if the library is counted. But when I clone the repository and count the library from scratch, I get a lot of materials created.

Apparently, when we clone a repository and count the library from scratch the string:
FolderImportSettingsList SettingsList = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(“790a6117d66fb134a8c97d86feb28b3d”));
in the GetImportSettings method doesn’t have the desired effect and just returns null

And just as interesting here
return folderImportSettings.ModelSettings.DefaultMaterial;
suppose that earlier we still found this folderImportSettings config, but it contains a reference to a material that is probably not imported yet.

All this is relevant for the case when the library is not present, when the project is open and the library is present everything is fine.

How is it supposed to return any custom material in the OnAssignMaterialModel callback when we consider the library from scratch?

If we consider the library from scratch, does it mean that we don’t have an actual AssetDatabase from where we could try to get something by GUIDs or paths?

It is interesting that I noticed that in many packages also uses AssetDatabase for example ShaderGraphImporter does not mean that in such packages when importing from scratch the code will not be executed correctly?

There seems to be a little bit of clarity on this issue.

FolderImportSettingsList SettingsList = AssetDatabase.LoadAssetAtPath<FolderImportSettingsList>(AssetDatabase.GUIDToAssetPath("f5aaf6c25f304815936c257da3378d15"));

This code finds the guid, but does not find the asset, because this asset is a ScriptableObject in which import settings are stored in a user-friendly form.

Apparently including the information about the import order discussed here:
https://discussions.unity.com/t/859814

It becomes clear that at the moment when I want to return material from this ScriptableObject in the OnAssignMaterialModel method, this ScriptableObject is not imported yet.
In general, it makes sense to feed the material in the OnAssignMaterialModel method, but the question:

How to do this if there are multiple ScriptableObjects storing import settings from which we would like to get the material?

Apart from storing settings in static classes or JSON, nothing comes to mind.

Naturally, for this purpose we do not want to use the OnPostprocessAllAssets call, which in fact will make the import 2 times and entail additional costs

In 2019, a former colleague of mine from my own studio was already asking this question)
https://discussions.unity.com/t/830216

Hello!
Before we dig too deep, I would like to make sure that I understand your setup:

  1. You have Fbx assets
  2. You are using the standard Model Importer (not a custom importer) for these Fbx assets
  3. You have a scriptable object that stores some user settings
  4. You want to use the OnAssignMaterialModel post-processor to assign a material to the Fbx asset, based on what is inside the ScriptableObject

The problem you have is that the ScriptableObject asset’s artifact is not always available (not imported) when your Fbx assets import. This only occurs with a fresh project.

In that situation, you can install a dependency from the Fbx asset to the ScriptableObject’s artifact. Can you try to use AssetImportContext.DependsOnArtifact in OnAssignMaterialModel?

The logic should do something like this:

if the ScriptableObject artifact exists (already imported)
    normal operation 
    Get the ScriptableObject
    Assign material that is defined in the ScriptableObject
else 
    don't assign a material, or use the default material
	
# In all cases, install the dependency. As mentioned in the doc:
#   "Note: If you specify an Asset as a dependency that doesn't exist or hasn't yet been
#   imported, the dependency is still registered. It is registered as an un-imported Asset. 
#   When the Asset is later imported, all Assets which depend on it will also get reimported."
context.DependsOnArtifact(path_to_scriptable_object)

Since you are installing a dependency, the Fbx asset will be re-imported when the ScriptableObject artifact gets created.

What do you think?

Good afternoon!) Thank you for your reply! Yes I already realized all that and added the scripable obj root asset in the build setings. This is now imported in time.

This works since everything that is in the build setting appears to be imported at the very beginning.

EditorBuildSettings.AddConfigObject(“FolderImportSettingsList”, settingsRoot, true);

1 Like