Blender V5.3
writefile.cc File Reference
#include <cerrno>
#include <climits>
#include <condition_variable>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <iomanip>
#include <mutex>
#include <sstream>
#include <xxhash.h>
#include <unistd.h>
#include <fmt/format.h>
#include "BLI_utildefines.hh"
#include "CLG_log.h"
#include "DNA_fileglobal_types.h"
#include "DNA_genfile.h"
#include "DNA_key_types.h"
#include "DNA_print.hh"
#include "DNA_sdna_pointers.hh"
#include "DNA_sdna_types.h"
#include "DNA_userdef_types.h"
#include "DNA_windowmanager_types.h"
#include "BLI_endian_defines.hh"
#include "BLI_fileops.hh"
#include "BLI_implicit_sharing.hh"
#include "BLI_listbase.hh"
#include "BLI_math_base_c.hh"
#include "BLI_math_matrix_c.hh"
#include "BLI_multi_value_map.hh"
#include "BLI_path_utils.hh"
#include "BLI_set.hh"
#include "BLI_string.hh"
#include "BLI_task_c.hh"
#include "BLI_threads.hh"
#include "BLI_time.hh"
#include "MEM_guardedalloc.h"
#include "BKE_asset.hh"
#include "BKE_blender_version.h"
#include "BKE_bpath.hh"
#include "BKE_global.hh"
#include "BKE_idprop.hh"
#include "BKE_idtype.hh"
#include "BKE_layer.hh"
#include "BKE_lib_id.hh"
#include "BKE_lib_override.hh"
#include "BKE_lib_query.hh"
#include "BKE_library.hh"
#include "BKE_main.hh"
#include "BKE_main_namemap.hh"
#include "BKE_node.hh"
#include "BKE_packedFile.hh"
#include "BKE_preferences.h"
#include "BKE_report.hh"
#include "BKE_workspace.hh"
#include "DRW_engine.hh"
#include "BLO_blend_validate.hh"
#include "BLO_read_write.hh"
#include "BLO_readfile.hh"
#include "BLO_undofile.hh"
#include "BLO_writefile.hh"
#include "readfile.hh"
#include "writefile.hh"
#include <zstd.h>

Go to the source code of this file.

Classes

class  blender::WriteWrap
class  blender::RawWriteWrap
class  blender::ZstdWriteWrap
struct  blender::RenderInfo

Namespaces

namespace  blender
 BMesh Iterators.

Macros

#define DNA_DEPRECATED_ALLOW
#define U   (*((const UserDef *)&U))
#define GENERATE_DEBUG_BLEND_FILE   0
#define DEBUG_BLEND_FILE_SUFFIX   ".debug.txt"
#define MEM_BUFFER_SIZE   MEM_SIZE_OPTIMAL(1 << 17)
#define MEM_CHUNK_SIZE   MEM_SIZE_OPTIMAL(1 << 15)
#define ZSTD_BUFFER_SIZE   (1 << 21)
#define ZSTD_CHUNK_SIZE   (1 << 20)
#define ZSTD_COMPRESSION_LEVEL   3

Functions

Write Data Type & Functions
static WriteData * blender::writedata_new (WriteWrap *ww)
static void blender::writedata_do_write (WriteData *wd, const void *mem, const size_t memlen)
static void blender::writedata_free (WriteData *wd)
Local Writing API 'mywrite'
static void blender::mywrite_flush (WriteData *wd)
static void blender::mywrite (WriteData *wd, const void *adr, size_t len)
static WriteData * blender::mywrite_begin (WriteWrap *ww, MemFile *compare, MemFile *current)
static bool blender::mywrite_end (WriteData *wd)
static uint64_t blender::get_stable_pointer_hint_for_id (const ID &id, const bool is_undo)
static void blender::mywrite_id_begin (WriteData *wd, ID *id)
static void blender::mywrite_id_end (WriteData *wd, ID *)
Typed DNA File Writing

These functions are used by blender's .blend system for file saving/loading.

static void blender::current_screen_compat (Main *mainvar, const bool use_active_win, bScreen **r_screen, Scene **r_scene, ViewLayer **r_view_layer)
static void blender::write_renderinfo (WriteData *wd, Main *mainvar)
static void blender::write_keymapitem (BlendWriter *writer, const wmKeyMapItem *kmi)
static void blender::write_userdef (BlendWriter *writer, const UserDef *userdef)
static void blender::write_id (WriteData *wd, ID *id)
static void blender::write_id_placeholder (WriteData *wd, ID *id)
static bool blender::write_library_if_needed (WriteData *wd, MultiValueMap< Library *, ID * > &linked_ids_by_library, Set< Library * > &written_libraries, Library &library)
static void blender::write_libraries (WriteData *wd, Main *bmain)
static void blender::write_global (WriteData *wd, const int fileflags, Main *mainvar)
static void blender::write_thumb (WriteData *wd, const BlendThumbnail *thumb)
File Writing (Private)
static std::string blender::get_blend_file_header ()
static void blender::write_blend_file_header (WriteData *wd)
static Vector< ID * > blender::gather_local_ids_to_write (Main *bmain, const bool is_undo)
static void blender::prepare_stable_data_block_ids (WriteData &wd, Main &bmain)
static bool blender::write_file_handle (Main *mainvar, WriteWrap *ww, StringRefNull filepath, MemFile *compare, MemFile *current, const int write_flags, const bool use_userdef, const BlendThumbnail *thumb, std::ostream *debug_dst)
static bool blender::do_history (const char *filepath, ReportList *reports)
static void blender::write_file_main_validate_pre (Main &bmain, const BlendFileWriteParams &params, ReportList *reports)
static void blender::write_file_main_validate_post (Main *bmain, ReportList *reports)
static bool blender::BLO_write_file_impl (Main *mainvar, const char *filepath, const int write_flags, const BlendFileWriteParams *params, ReportList *reports, WriteWrap &ww)
BLO Write File API
See also
BLO_read_from_file for file reading.
bool blender::BLO_write_file (Main *mainvar, const char *filepath, int write_flags, const BlendFileWriteParams *params, ReportList *reports)
bool blender::BLO_write_file_mem (Main *mainvar, MemFile *compare, MemFile *current, int write_flags)

Variables

static CLG_LogRef blender::LOG = {"blend.writefile"}
static CLG_LogRef blender::LOG_UNDO = {"undo"}

Generic DNA File Writing

#define writestruct_at_address(wd, filecode, struct_id, nr, adr, data, fn)
#define writestruct(wd, filecode, struct_id, nr, adr, fn)
constexpr uint64_t blender::generated_address_id_on_undo_flag = uint64_t(1) << 63
constexpr uint64_t blender::generated_address_id_mask = generated_address_id_on_undo_flag
static bool blender::write_at_address_validate (WriteData *wd, const int filecode, const void *address)
static void blender::write_bhead (WriteData *wd, const BHead &bhead)
static uint64_t blender::stable_id_from_hint (const uint64_t hint)
static uint64_t blender::get_next_stable_address_id (WriteData &wd, uint64_t &hint)
static uint64_t blender::get_address_id_int (WriteData &wd, const void *address)
static const void * blender::get_address_id (WriteData &wd, const void *address)
static uint64_t blender::get_address_id_for_undo (WriteData &wd)
static void blender::writestruct_at_address_nr (WriteData *wd, const int filecode, const int struct_nr, const int64_t nr, const void *adr, const void *data, const BlendStructWriterFn fn)
static void blender::writestruct_nr (WriteData *wd, const int filecode, const int struct_nr, const int64_t nr, const void *adr, const BlendStructWriterFn fn)
static void blender::write_raw_data_in_debug_file (WriteData *wd, const size_t len, const void *address_id, const void *data)
static void blender::writedata (WriteData *wd, const int filecode, const void *data, const size_t len, const void *adr)
static void blender::writedata (WriteData *wd, const int filecode, const size_t len, const void *adr)
static void blender::writelist_nr (WriteData *wd, const int filecode, const int struct_nr, const ListBase *lb, const BlendStructWriterFn fn)

Macro Definition Documentation

◆ DEBUG_BLEND_FILE_SUFFIX

#define DEBUG_BLEND_FILE_SUFFIX   ".debug.txt"

Definition at line 158 of file writefile.cc.

Referenced by blender::BLO_write_file_impl().

◆ DNA_DEPRECATED_ALLOW

#define DNA_DEPRECATED_ALLOW

FILE FORMAT

IFF-style structure (but not IFF compatible!)

Start of the file:

Historic Blend-files (pre-Blender 5.0): BLENDER_V100 : Fixed 12 bytes length. See BLEND_FILE_FORMAT_VERSION_0 for details.

Current Blend-files (Blender 5.0 and later): BLENDER17-01v0500: Variable bytes length. See BLEND_FILE_FORMAT_VERSION_1 for details.

data-blocks: (also see struct #BHead).

`bh.code`       `char[4]` see `BLO_core_bhead.hh` for a list of known types.
`bh.len`        `int32` length data after #BHead in bytes.
`bh.old`        `void *` old pointer (the address at the time of writing the file).
`bh.SDNAnr`     `int32` struct index of structs stored in #DNA1 data.
`bh.nr`         `int32` in case of array: number of structs.
data
...
...

Almost all data in Blender are structures. Each struct saved gets a BHead header. With BHead the struct can be linked again and compared with #StructDNA.

WRITE

Preferred writing order: (not really a must, but why would you do it random?) Any case: direct data is ALWAYS after the lib block.

(Local file data)

  • for each LibBlock
    • write LibBlock
    • write associated direct data (External file data)
  • per library
    • write library block
    • per LibBlock
      • write the ID of LibBlock
  • write #BLO_CODE_GLOB (#RenderInfo struct. 128x128 blend file preview is optional).
  • write #BLO_CODE_GLOB (#FileGlobal struct) (some global vars).
  • write #BLO_CODE_DNA1 (#SDNA struct)
  • write #BLO_CODE_USER (#UserDef struct) for file paths:

Definition at line 89 of file writefile.cc.

◆ GENERATE_DEBUG_BLEND_FILE

#define GENERATE_DEBUG_BLEND_FILE   0

Generate an additional file next to every saved .blend file that contains the file content in a more human readable form.

Definition at line 157 of file writefile.cc.

◆ MEM_BUFFER_SIZE

#define MEM_BUFFER_SIZE   MEM_SIZE_OPTIMAL(1 << 17)

Definition at line 163 of file writefile.cc.

Referenced by blender::writedata_new().

◆ MEM_CHUNK_SIZE

#define MEM_CHUNK_SIZE   MEM_SIZE_OPTIMAL(1 << 15)

Definition at line 164 of file writefile.cc.

Referenced by blender::writedata_new().

◆ U

#define U   (*((const UserDef *)&U))

Definition at line 151 of file writefile.cc.

◆ writestruct

#define writestruct ( wd,
filecode,
struct_id,
nr,
adr,
fn )
Value:
writestruct_nr(wd, filecode, dna::sdna_struct_id_get<struct_id>(), nr, adr, fn)

Definition at line 1135 of file writefile.cc.

Referenced by blender::BKE_modifier_blend_write(), blender::write_global(), and blender::write_userdef().

◆ writestruct_at_address

#define writestruct_at_address ( wd,
filecode,
struct_id,
nr,
adr,
data,
fn )
Value:
writestruct_at_address_nr(wd, filecode, dna::sdna_struct_id_get<struct_id>(), nr, adr, data, fn)

Definition at line 1132 of file writefile.cc.

Referenced by blender::write_id_placeholder().

◆ ZSTD_BUFFER_SIZE

#define ZSTD_BUFFER_SIZE   (1 << 21)

Definition at line 166 of file writefile.cc.

Referenced by blender::writedata_new().

◆ ZSTD_CHUNK_SIZE

#define ZSTD_CHUNK_SIZE   (1 << 20)

Definition at line 167 of file writefile.cc.

Referenced by blender::writedata_new().

◆ ZSTD_COMPRESSION_LEVEL

#define ZSTD_COMPRESSION_LEVEL   3

Definition at line 169 of file writefile.cc.