How to use DzAssetMgr.searchForAssets()?
Hello all,
I am trying to use DzAssetMgr.searchForAssets() this is my code.
// DAZ Studio version 4.21.0.5 filetype DAZ Scriptvar assetManager = App.getAssetMgr();var searchResult = assetManager.searchForAssets( 'lazair', // filteText: search string ['/Default/Scenes'], // categories: Array of categories paths ['/Genesis 8', '/Genesis 8.1'], // compatibilityBases: compatibility bases Array ['scene'], // contentTypes: content types Array 1 // Sort by name);var assetsCount = searchResult.getNumAssets();print("Assets found: " + assetsCount);var containersCount = searchResult.getNumChildContainers();print("Child containers found: " + containersCount);
This is the output.
Executing Script...Assets found: 0Child containers found: 0Result: Script executed in 0 secs 2 msecs.
The asset I am attempting to search for is located at "Scenes/moka_lazair_01.duf"
relative to one of the Content Library directories I set and "Content Library" pane can find it both in "File" and "Database" search mode using either "moka" or "lazair" as the search string.
For the contentTypes
parameter I used either "Scene"
and "scene"
with the same result.
Categories and Compatibility Bases paths arrays are not invented but obtained with the following two scripts.
// DAZ Studio version 4.21.0.5 filetype DAZ Scriptvar assetManager = App.getAssetMgr();var categoriesContainer = assetManager.getCategories();categoriesContainer.getChildContainers().forEach( function (container) { print('\tpath: ' + container.getPath()); print('Child containers count: ' + container.getNumChildContainers()); container.getChildContainers().forEach( function (childContainer, index) { print('\tPath ' + index + ': ' + childContainer.getPath()); } ); });
// DAZ Studio version 4.21.0.5 filetype DAZ Scriptvar assetManager = App.getAssetMgr();var compatibilityBasesContainer = assetManager.getCompatibilityBases();print('Compatibilily Base container informations');print('{');print("\tpath: " + compatibilityBasesContainer.getPath());print("\tChildren count: " + compatibilityBasesContainer.getNumChildContainers());print('}');compatibilityBasesContainer.getChildContainers().forEach( function (container) { print('\tChild container informations'); print('\t{'); print('\t\tpath: ' + container.getPath()); print('\t}'); });
Despite this I am not sure of some things.
- Since the file is a Scene I created, does it have any Compatibility Base?
- If not, what value should I pass ass the
compatibilityBases
parameter? I attempted to passnull
and it is not valid (quite weird behaviour for a JavaScript API); emptyArray
([]
) is accepted but useless. - I have the same doubts for
categories
parameter... - One of
['Scene']
or['scene']
should be ok but: which one is actually correct?
I found the list of types with this script.
var assetManager = App.getAssetMgr();var types = assetManager.getTypes();types.forEach( function (type) { print('Type: ' + type); });
And it returns "Scene"
. Though in the "Info" tab at the bottom of the "Content Library" pane displays "scene"
as the value of the "type" property of the asset.
P.S. There is something that eats new line characters in <pre><code>...</code></pre>
combo in this forum. Database side stored source codes are clearly safe because I can edit them with new line characters untouched therefore I guess it is a render problem. Also the code formatter takes everything as a comment if there is a comment in the beginning of the code.
Comments
Not sure about the compatibility base. The API would usually use an empty array here for any though.
I used DzAssetMgr.getTypeForContentFile(...) on a scene file, and the correct contentType is ['Scene'].
In DS 4.20.1.29 the SortType enum values were added to DzAssetMgr although the documentation was missed.
You can replace '1' with 'assetManager.NameSort' for clarity.
The search functions are asynchronous and may take some time. You can use DzTopLevelAssetContainer.getState() and DzTopLevelAssetContainer.stateChanged(...) to monitor for completion
The issue with code blocks in the forum is a known issue and has had several bug reports filed.
Ehi Omniflux,
thank you for your extensive answer I really appreciate it.
About tracking state changes: should I use a Slot listening to the DzTopLevelAssetContainer::stateChanged() Signal?
Yes, that is how I would do it.
something like
(function(){
var assetManager = App.getAssetMgr();
function checkSearch (state) {
if (state !== DzTopLevelAssetContainer.csBusy) {
searchResult.stateChanged.disconnect (checkSearch);
var assetsCount = searchResult.getNumAssets();
print("Assets found: " + assetsCount);
var containersCount = searchResult.getNumChildContainers();
print("Child containers found: " + containersCount);
finishBackgroundProgress();
}
}
startBackgroundProgress ('Doing my custom thing', 0, true, true)
var searchResult = assetManager.searchForAssets ('Omni', [], [], [], assetManager.NameSort);
searchResult.stateChanged.connect (checkSearch);
while (backgroundProgressIsActive())
processEvents();
})();
2022-12-13 15:16:27.966 [DEBUG] :: Executing Script...
2022-12-13 15:16:28.114 [DEBUG] :: Assets found: 914
2022-12-13 15:16:28.115 [DEBUG] :: Child containers found: 0
2022-12-13 15:16:28.149 [DEBUG] :: Result:
2022-12-13 15:16:28.150 [DEBUG] :: Script executed in 0 secs 183 msecs.
The code I posted above originally had a bug. If you've already copied it,
if (state === DzTopLevelAssetContainer.csFinished) {
should be
if (state !== DzTopLevelAssetContainer.csBusy) {