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 filetype DAZ Script
 
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function(){
 
	// Initialize 'static' variables that hold modifier key state
	var s_bShiftPressed = false;
	var s_bControlPressed = false;
	var s_bAltPressed = false;
	var s_bMetaPressed = false;
 
	// If the "Action" global transient is defined, and its the correct type
	if( typeof( Action ) != "undefined" && Action.inherits( "DzScriptAction" ) ){
		// If the current key sequence for the action is not pressed
		if( !App.isKeySequenceDown( Action.shortcut ) ){
			updateModifierKeyState();
		}
	// If the "Action" global transient is not defined
	} else if( typeof( Action ) == "undefined" ) {
		updateModifierKeyState();
	}
 
	/*********************************************************************/
	// void : A function for updating the keyboard modifier state
	function updateModifierKeyState()
	{
		// Get the current modifier key state
		var nModifierState = App.modifierKeyState();
		// Update variables that hold modifier key state
		s_bShiftPressed = (nModifierState & 0x02000000) != 0;
		s_bControlPressed = (nModifierState & 0x04000000) != 0;
		s_bAltPressed = (nModifierState & 0x08000000) != 0;
		s_bMetaPressed = (nModifierState & 0x10000000) != 0;
	};
 
	/*********************************************************************/
	// void : A function for printing only if debugging
	function debug()
	{
		// If we are not debugging
		if( !s_bAltPressed ){
			// We are done...
			return;
		}
 
		// Convert the arguments object into an array
		var aArguments = [].slice.call( arguments );
 
		// Print the array
		print( aArguments.join(" ") );
	};
 
	/*********************************************************************/
	// String : A function for retrieving a translation if one exists
	function text( sText )
	{
		// If the version of the application supports qsTr()
		if( typeof( qsTr ) != "undefined" ){
			// Return the translated (if any) text
			return qsTr( sText );
		}
 
		// Return the original text
		return sText;
	};
 
	/*********************************************************************/
	// void : Method for creating a link to an asset
	function createAssetLink( sBasePath, oAsset )
	{
		// If the asset is already a link
		if ( oAsset.isSymLink ){
			// We are done...
			return;
		}
 
		// Get the relative path of the asset
		var sRelativePath = oAsset.getRelativeFilePath();
		// If we do not have a relative path
		if( sRelativePath.isEmpty() ){
			// We are done...
			return;
		}
 
		// If the path does not start with a slash
		if( !sRelativePath.startsWith( "/" ) ){
			// Prepend the path with a slash
			sRelativePath = "/" + 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
		var oFileInfo = new DzFileInfo( sAbsolutePath );
		// If the file already exists
		if( oFileInfo.exists() ){
			// We are done...
			return;
		}
 
		// Create the link
		oAsset.saveDSLink( sAbsolutePath );
 
		//Provide feedback
		debug( "Created:", sAbsolutePath );
	};
 
	/*********************************************************************/
	// void : Method for recursively creating links to assets
	function createAssetLinkRecurse( sBasePath, oContainer, bRecurse )
	{
		// 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 ) );
		}
 
		// If we are not recursing
		if( !bRecurse ){
			// We are done...
			return;
		}
 
		// Iterate over the child containers
		for( var i = 0, nContainers = oContainer.getNumChildContainers(); i < nContainers; ++i ){
			// Recurse
			createAssetLinkRecurse( sBasePath, oContainer.getChildContainer( i ), true );
		}
	};
 
	/*********************************************************************/
	// Get the pane manager
	var oPaneMgr = MainWindow.getPaneMgr();
	// If the pane manager was not found
	if( !oPaneMgr ){
		//Provide feedback
		debug( "Missing DzPaneMgr!" );
 
		// We are done...
		return;
	}
 
	// Find the content library pane
	var oPane = oPaneMgr.findPane( "DzContentLibraryPane" );
	// If the pane was not found
	if( !oPane ){
		//Provide feedback
		debug( "Missing DzContentLibraryPane pane!" ); 
 
		// We are done...
		return;
	}
 
	// Get the selected container
	var oContainer = oPane.getSelectedContainer();
	// If no container is selected
	if( !oContainer ){
		//Provide feedback
		debug( "No selected container." );
 
		// We are done...
		return;
	}
 
	// Get the content manager
	var oContentMgr = App.getContentMgr();
	// If we do not have any mapped native directories
	if( oContentMgr.getNumContentDirectories() < 1 ){
		//Provide feedback
		debug( "No mapped directories." );
 
		// We are done...
		return;
	}
 
	// Set up a path to save to
	var sBasePath = FileDialog.doDirectoryDialog( text( "Save To" ), "",
			oContentMgr.getContentDirectoryPath( 0 ) + "/Generated Asset Links" );
 
	// If the user cancelled
	if( sBasePath.isEmpty() ){
		//Provide feedback
		debug( "User cancelled." ); 
		// We are done...
		return;
	}
 
	// Create links for each asset in the container, recursively
	createAssetLinkRecurse( sBasePath, oContainer, true );
 
	// Get the asset manager
	var oAssetMgr = App.getAssetMgr();
	// If the asset manager was not found
	if( !oAssetMgr ){
		//Provide feedback
		debug( "Missing DzAssetMgr!" ); 
 
		// We are done...
		return;
	}
 
	// Force a refresh of folder based containers
	oAssetMgr.updateFolderBasedContainers();
 
// Finalize the function and invoke
})();