Filename is a special class to handle filenames and paths. It is used to easily construct and edit paths, filenames and suffixes. It also allows to open dialogs to select files and directories.
Filename objects can be created on demand or can be obtained from other entities.
To retrieve and modify Filename objects stored in a BaseContainer respectively use:
另请参阅 BaseContainer Manual .
To retrieve and modify Filename objects stored in a GeData object ( GeData type is DA_FILENAME ) respectively use:
另请参阅 GeData Manual .
Important paths are obtained from:
// check if the active object is an "Alembic Generator" BaseObject * const object = doc-> GetActiveObject (); if ( object == nullptr && object-> GetType () != Oalembicgenerator ) return maxon::IllegalArgumentError( MAXON_SOURCE_LOCATION );
// read the "Path to Alembic file" parameter if (!object-> GetParameter ( DescID ( ALEMBIC_PATH ), data, DESCFLAGS_GET::NONE )) return maxon::UnexpectedError( MAXON_SOURCE_LOCATION ); ApplicationOutput ( "Alembic File: " + data. GetFilename (). GetString ());
A Filename can be constructed with basic operators:
// open a folder selector dialog if (!selectFolder. FileSelect ( FILESELECTTYPE::ANYTHING , FILESELECT::DIRECTORY , "Select Folder" _s)) return maxon::OK ; const Filename fullFilePath = selectFolder + "cube.c4d" ;
// check if given file exists if ( GeFExist (fullFilePath)) ApplicationOutput ( "The folder contains \"cube.c4d\"" ); else ApplicationOutput ( "The folder does not container \"cube.c4d\"" );
// This example constructs the Filename for an image file // in the plugin's "res" folder and loads this image.
// construct full file name const Filename imageFile = GeGetPluginPath () + Filename ( "res" ) + Filename ( "arbitrary_icon.tif" );
// check if the file exists if (! GeFExist (imageFile)) return maxon::UnexpectedError( MAXON_SOURCE_LOCATION ); AutoAlloc<BaseBitmap> bitmap; if (bitmap == nullptr ) return maxon::OutOfMemoryError( MAXON_SOURCE_LOCATION );
// load image file into the given BaseBitmap if (bitmap-> Init (imageFile) != IMAGERESULT::OK ) return maxon::UnknownError( MAXON_SOURCE_LOCATION );
// show BaseBitmap in the Picture Viewer ShowBitmap (bitmap);
A Filename object can easily be copied:
The content of a Filename object can be represented by a String :
另请参阅 String Manual (Classic) .
// This example prints the full path to the console. const String filenameString = selectedFile.GetString(); ApplicationOutput ( "File selected: " + filenameString);A filename may be composed of a directory part and a file part. Each part can be edited independently:
Part of the filename may also be the file suffix. This suffix can be edited separately:
// check if the given Filename references a Cinema 4D scene file if (selectedFile.CheckSuffix( "c4d" _s)) ApplicationOutput ( "C4D File" ); else ApplicationOutput ( "Some other file" );
It is possible to get the suffix for a given image file type:
The memory mode allows to perform read and write operations on a memory block instead of a file on the hard drive. This is typically used to encrypt or compress the resulting data.
// open HyperFile to write if (!hyperFile->Open(0, fn, FILEOPEN::WRITE , FILEDIALOG::NONE )) return maxon::UnknownError( MAXON_SOURCE_LOCATION ); hyperFile->WriteInt32(123); hyperFile->WriteString( "foo" _s); hyperFile->WriteString( "bar" _s); hyperFile->WriteInt32(456); hyperFile->Close();
// get raw data mfs-> GetData (data, size, false );
A Filename object is also useful to open dialogs to select files and folders:
The files that can be selected in a dialog can be filtered with these flags:
The type of dialog is defined with this flag:
// open a file selector dialog to open a file if (loadFile. FileSelect ( FILESELECTTYPE::ANYTHING , FILESELECT::LOAD , "Load File" _s)) ApplicationOutput ( "File to load: " + loadFile. GetString ()); Filename saveFile;
// open a file selector dialog to save a file if (saveFile. FileSelect ( FILESELECTTYPE::ANYTHING , FILESELECT::SAVE , "Save File" _s)) ApplicationOutput ( "File to save: " + saveFile. GetString ()); Filename folder;
// open a folder selector dialog if (folder. FileSelect ( FILESELECTTYPE::ANYTHING , FILESELECT::DIRECTORY , "Select Folder" _s)) ApplicationOutput ( "Folder: " + folder. GetString ());
A Filename object may also be used to reference a file located in Cinema 4D 's content browser.
// access a filename parameter if (node-> GetParameter ( DescID (ID_FILENAME), geData, DESCFLAGS_GET::NONE )) { const Filename filename = geData. GetFilename ();
// check if the given Filename // is a preset URL if (filename. IsBrowserUrl ()) ApplicationOutput ( "File is stored in presets" _s); }
Two Filename objects can be compared easily:
// open folder selector dialog if (folder. FileSelect ( FILESELECTTYPE::ANYTHING , FILESELECT::DIRECTORY , "Select Folder" _s)) { if (folder == desktop) ApplicationOutput ( "You selected the desktop." ); else ApplicationOutput ( "You selected " + folder. GetString ()); }
A Filename object can be stored in a BaseFile 或 HyperFile using:
// open BaseFile to write if (!bf-> Open (filename, FILEOPEN::WRITE , FILEDIALOG::ANY )) return maxon::UnknownError( MAXON_SOURCE_LOCATION ); bf-> WriteFilename (filenameData); bf-> 关闭 ();
// This example reads a String form the given BaseFile. AutoAlloc<BaseFile> bf; if (bf == nullptr ) return maxon::OutOfMemoryError( MAXON_SOURCE_LOCATION );
// open BaseFile to read if (!bf-> Open (filename, FILEOPEN::READ , FILEDIALOG::ANY )) return maxon::UnknownError( MAXON_SOURCE_LOCATION ); Filename filenameData; bf-> ReadFilename (&filenameData); ApplicationOutput ( "Filename from BaseFile: " + filenameData. GetString ()); bf-> 关闭 ();
// open HyperFile to write if (!hf-> Open (753, filename, FILEOPEN::WRITE , FILEDIALOG::ANY )) return maxon::UnknownError( MAXON_SOURCE_LOCATION ); hf-> WriteFilename (filenameData); hf-> 关闭 ();
// This example reads a String from the given HyperFile. AutoAlloc<HyperFile> hf; if (hf == nullptr ) return maxon::OutOfMemoryError( MAXON_SOURCE_LOCATION );
// open HyperFile to read if (!hf-> Open (753, filename, FILEOPEN::READ , FILEDIALOG::ANY )) return maxon::UnknownError( MAXON_SOURCE_LOCATION ); Filename filenameData; hf-> ReadFilename (&filenameData); ApplicationOutput ( "Filename from HyperFile: " + filenameData. GetString ());