User Tools

Site Tools


Create Asset Links

Summary

Below is an example demonstrating how you can [recursively] create Daz Studio JSON Link (*.djl) files for assets in the selected container (i.e., product, category, etc.) in the Content Library pane via script.

API Areas of Interest

Example

Create_Asset_Links.dsa
// DAZ Studio version 4.9.1.25
// Define an anonymous function to serve as our main loop
(function(){
 
	/*********************************************************************/
	// void : Method for creating a link to an asset
	function createAssetLink( sBasePath, oAsset, bVerbose )
	{
		// If the asset is already a link
		if ( oAsset.isSymLink ){
			// We're done...
			return;
		}
 
		// Get the relative path
		var sRelativePath = oAsset.getRelativeFilePath();
		// If we do not have a relative path
		if( sRelativePath.isEmpty() ){
			// We're done...
			return;
		}
 
		// If the path doesn't start with a slash
		if( !sRelativePath.startsWith( "/" ) ){
			// Prepend the path with a slash
			sRelativePath = String("/%1").arg( sRelativePath );
		}
 
		// Construct an absolute path for the link
		var sAbsolutePath = String("%1%2.%3")
			.arg( sBasePath )
			.arg( sRelativePath )
			.arg( oAsset.getDSLinkExtension() );
 
		// Create a file info object for the path
		var oFileInfo = new DzFileInfo( sAbsolutePath );
		// If the file already exists
		if( oFileInfo.exists() ){
			// We're done...
			return;
		}
 
		// Create the link
		oAsset.saveDSLink( sAbsolutePath );
 
		// If we're providing feedback
		if( bVerbose ){
			print( "Created:", sAbsolutePath );
		}
	};
 
	/*********************************************************************/
	// void : Method for recursively creating links to assets
	function createAssetLinkRecurse( sBasePath, oContainer, bRecurse, bVerbose )
	{
		// Iterate over the assets
		for( var i = 0, nAssets = oContainer.getNumAssets(); i < nAssets; ++i ){
			// Create an asset link for the 'current' asset
			createAssetLink( sBasePath, oContainer.getAsset( i ), bVerbose );
		}		
 
		// If we're not recursing
		if( !bRecurse ){
			// We're done...
			return;
		}
 
		// Iterate over the child containers
		for( var i = 0, nContainers = oContainer.getNumChildContainers(); i < nContainers; ++i ){
			// Recurse the container
			createAssetLinkRecurse( sBasePath, oContainer.getChildContainer( i ), true, bVerbose );
		}	
	};
 
	/*********************************************************************/
	// Define whether or not to provide feedback
	var bVerbose = false;
 
	// Get the pane manager
	var oPaneMgr = MainWindow.getPaneMgr();
	// If the pane manager was not found
	if( !oPaneMgr ){
		// If we're providing feedback
		if( bVerbose ){
			print( "Missing DzPaneMgr!" ); 
		}
 
		// We're done...
		return;
	}
 
	// Find the content library pane
	var oPane = oPaneMgr.findPane( "DzContentLibraryPane" );
	// If the pane was not found
	if( !oPane ){
		// If we're providing feedback
		if( bVerbose ){
			print( "Missing DzContentLibraryPane pane!" ); 
		}
 
		// We're done...
		return;
	}
 
	// Get the selected container
	var oContainer = oPane.getSelectedContainer();
	// If no container is selected
	if( !oContainer ){
		// If we're providing feedback
		if( bVerbose ){
			print( "No selected container." );
		}
 
		// We're done...
		return;
	}
 
	// Get the content manager
	var oContentMgr = App.getContentMgr();
	// If we don't have any mapped native directories
	if( oContentMgr.getNumContentDirectories() < 1 ){
		// If we're providing feedback
		if( bVerbose ){
			print( "No mapped directories." );
		}
 
		// We're done...
		return;
	}	
 
	// Set up a path to save too, could ask the user for a root location.
	// This just puts it in the first mapped directory
	var sBasePath = String("%1/Generated Asset Links").arg( oContentMgr.getContentDirectoryPath( 0 ) );
 
	createAssetLinkRecurse( sBasePath, oContainer, true, bVerbose );
 
	// Get the asset manager
	var oAssetMgr = App.getAssetMgr();
	// If the asset manager was not found
	if( !oAssetMgr ){
		// If we're providing feedback
		if( bVerbose ){
			print( "Missing DzAssetMgr!" ); 
		}
 
		// We're done...
		return;
	}
 
	// Force a refresh of folder based containers
	oAssetMgr.updateFolderBasedContainers();
 
// Close the function and immediately invoke it
})();