Blender V5.0
writefile.cc File Reference
#include <cerrno>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <iomanip>
#include <sstream>
#include <xxhash.h>
#include <unistd.h>
#include <fmt/format.h>
#include "BLI_utildefines.h"
#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.h"
#include "BLI_fileops.hh"
#include "BLI_implicit_sharing.hh"
#include "BLI_math_base.h"
#include "BLI_math_matrix.h"
#include "BLI_multi_value_map.hh"
#include "BLI_path_utils.hh"
#include "BLI_set.hh"
#include "BLI_string.h"
#include "BLI_threads.h"
#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 <zstd.h>

Go to the source code of this file.

Classes

struct  ZstdFrame
class  WriteWrap
class  RawWriteWrap
class  ZstdWriteWrap
struct  ZstdWriteWrap::ZstdWriteBlockTask
struct  WriteData
struct  BlendWriter
struct  RenderInfo

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) /* 128kb */
#define MEM_CHUNK_SIZE   MEM_SIZE_OPTIMAL(1 << 15) /* ~32kb */
#define ZSTD_BUFFER_SIZE   (1 << 21) /* 2mb */
#define ZSTD_CHUNK_SIZE   (1 << 20) /* 1mb */
#define ZSTD_COMPRESSION_LEVEL   3

Functions

Write Data Type & Functions
static WriteDatawritedata_new (WriteWrap *ww)
static void writedata_do_write (WriteData *wd, const void *mem, const size_t memlen)
static void writedata_free (WriteData *wd)
Local Writing API 'mywrite'
static void mywrite_flush (WriteData *wd)
static void mywrite (WriteData *wd, const void *adr, size_t len)
static WriteDatamywrite_begin (WriteWrap *ww, MemFile *compare, MemFile *current)
static bool mywrite_end (WriteData *wd)
static uint64_t get_stable_pointer_hint_for_id (const ID &id, const bool is_undo)
static void mywrite_id_begin (WriteData *wd, ID *id)
static void 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 current_screen_compat (Main *mainvar, const bool use_active_win, bScreen **r_screen, Scene **r_scene, ViewLayer **r_view_layer)
static void write_renderinfo (WriteData *wd, Main *mainvar)
static void write_keymapitem (BlendWriter *writer, const wmKeyMapItem *kmi)
static void write_userdef (BlendWriter *writer, const UserDef *userdef)
static void write_id (WriteData *wd, ID *id)
static void write_id_placeholder (WriteData *wd, ID *id)
static void write_libraries (WriteData *wd, Main *bmain)
static void write_global (WriteData *wd, const int fileflags, Main *mainvar)
static void write_thumb (WriteData *wd, const BlendThumbnail *thumb)
File Writing (Private)
static int write_id_direct_linked_data_process_cb (LibraryIDLinkCallbackData *cb_data)
static std::string get_blend_file_header ()
static void write_blend_file_header (WriteData *wd)
static blender::Vector< ID * > gather_local_ids_to_write (Main *bmain, const bool is_undo)
static void prepare_stable_data_block_ids (WriteData &wd, Main &bmain)
static bool write_file_handle (Main *mainvar, WriteWrap *ww, MemFile *compare, MemFile *current, const int write_flags, const bool use_userdef, const BlendThumbnail *thumb, std::ostream *debug_dst)
static bool do_history (const char *filepath, ReportList *reports)
static void write_file_main_validate_pre (Main *bmain, ReportList *reports)
static void write_file_main_validate_post (Main *bmain, ReportList *reports)
static bool BLO_write_file_impl (Main *mainvar, const char *filepath, const int write_flags, const BlendFileWriteParams *params, ReportList *reports, WriteWrap &ww)
File Writing (Public)
bool BLO_write_file (Main *mainvar, const char *filepath, const int write_flags, const BlendFileWriteParams *params, ReportList *reports)
bool BLO_write_file_mem (Main *mainvar, MemFile *compare, MemFile *current, const int write_flags)
void BLO_write_raw (BlendWriter *writer, const size_t size_in_bytes, const void *data_ptr)
void BLO_write_struct_by_name (BlendWriter *writer, const char *struct_name, const void *data_ptr)
void BLO_write_struct_array_by_name (BlendWriter *writer, const char *struct_name, const int64_t array_size, const void *data_ptr)
void BLO_write_struct_by_id (BlendWriter *writer, const int struct_id, const void *data_ptr)
void BLO_write_struct_at_address_by_id (BlendWriter *writer, const int struct_id, const void *address, const void *data_ptr)
void BLO_write_struct_at_address_by_id_with_filecode (BlendWriter *writer, const int filecode, const int struct_id, const void *address, const void *data_ptr)
void BLO_write_struct_array_by_id (BlendWriter *writer, const int struct_id, const int64_t array_size, const void *data_ptr)
void BLO_write_struct_array_at_address_by_id (BlendWriter *writer, const int struct_id, const int64_t array_size, const void *address, const void *data_ptr)
void BLO_write_struct_list_by_id (BlendWriter *writer, const int struct_id, const ListBase *list)
void BLO_write_struct_list_by_name (BlendWriter *writer, const char *struct_name, ListBase *list)
void blo_write_id_struct (BlendWriter *writer, const int struct_id, const void *id_address, const ID *id)
int BLO_get_struct_id_by_name (const BlendWriter *writer, const char *struct_name)
void BLO_write_char_array (BlendWriter *writer, const int64_t num, const char *data_ptr)
void BLO_write_int8_array (BlendWriter *writer, const int64_t num, const int8_t *data_ptr)
void BLO_write_int16_array (BlendWriter *writer, const int64_t num, const int16_t *data_ptr)
void BLO_write_uint8_array (BlendWriter *writer, const int64_t num, const uint8_t *data_ptr)
void BLO_write_int32_array (BlendWriter *writer, const int64_t num, const int32_t *data_ptr)
void BLO_write_uint32_array (BlendWriter *writer, const int64_t num, const uint32_t *data_ptr)
void BLO_write_float_array (BlendWriter *writer, const int64_t num, const float *data_ptr)
void BLO_write_double_array (BlendWriter *writer, const int64_t num, const double *data_ptr)
void BLO_write_pointer_array (BlendWriter *writer, const int64_t num, const void *data_ptr)
void BLO_write_float3_array (BlendWriter *writer, const int64_t num, const float *data_ptr)
void BLO_write_string (BlendWriter *writer, const char *data_ptr)
void BLO_write_shared_tag (BlendWriter *writer, const void *data)
void BLO_write_shared (BlendWriter *writer, const void *data, const size_t approximate_size_in_bytes, const blender::ImplicitSharingInfo *sharing_info, const blender::FunctionRef< void()> write_fn)
bool BLO_write_is_undo (BlendWriter *writer)

Variables

static CLG_LogRef LOG = {"blend.writefile"}

Generic DNA File Writing

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

Macro Definition Documentation

◆ DEBUG_BLEND_FILE_SUFFIX

#define DEBUG_BLEND_FILE_SUFFIX   ".debug.txt"

Definition at line 150 of file writefile.cc.

Referenced by 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)

Definition at line 87 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 149 of file writefile.cc.

◆ MEM_BUFFER_SIZE

#define MEM_BUFFER_SIZE   MEM_SIZE_OPTIMAL(1 << 17) /* 128kb */

Definition at line 155 of file writefile.cc.

Referenced by writedata_new().

◆ MEM_CHUNK_SIZE

#define MEM_CHUNK_SIZE   MEM_SIZE_OPTIMAL(1 << 15) /* ~32kb */

Definition at line 156 of file writefile.cc.

Referenced by writedata_new().

◆ U

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

Definition at line 143 of file writefile.cc.

◆ writestruct

#define writestruct ( wd,
filecode,
struct_id,
nr,
adr )
Value:
int sdna_struct_id_get()
static void writestruct_nr(WriteData *wd, const int filecode, const int struct_nr, const int64_t nr, const void *adr)

Definition at line 1127 of file writefile.cc.

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

◆ writestruct_at_address

#define writestruct_at_address ( wd,
filecode,
struct_id,
nr,
adr,
data )
Value:
BMesh const char void * data
static void writestruct_at_address_nr(WriteData *wd, const int filecode, const int struct_nr, const int64_t nr, const void *adr, const void *data)
Definition writefile.cc:931

Definition at line 1123 of file writefile.cc.

Referenced by write_id_placeholder().

◆ ZSTD_BUFFER_SIZE

#define ZSTD_BUFFER_SIZE   (1 << 21) /* 2mb */

Definition at line 158 of file writefile.cc.

Referenced by writedata_new().

◆ ZSTD_CHUNK_SIZE

#define ZSTD_CHUNK_SIZE   (1 << 20) /* 1mb */

Definition at line 159 of file writefile.cc.

Referenced by writedata_new().

◆ ZSTD_COMPRESSION_LEVEL

#define ZSTD_COMPRESSION_LEVEL   3

Definition at line 161 of file writefile.cc.

Function Documentation

◆ BLO_get_struct_id_by_name()

int BLO_get_struct_id_by_name ( const BlendWriter * writer,
const char * struct_name )

Mapping between names and ids.

Definition at line 2261 of file writefile.cc.

References DNA_struct_find_with_alias(), WriteData::sdna, and BlendWriter::wd.

Referenced by BLO_write_struct_array_by_name(), and BLO_write_struct_list_by_name().

◆ BLO_write_char_array()

void BLO_write_char_array ( BlendWriter * writer,
int64_t num,
const char * data_ptr )

Slightly 'safer' code to write arrays of basic types data.

Definition at line 2267 of file writefile.cc.

References BLO_write_raw(), and num.

Referenced by console_space_blend_write(), IDP_WriteString(), and object_blend_write().

◆ BLO_write_double_array()

void BLO_write_double_array ( BlendWriter * writer,
const int64_t num,
const double * data_ptr )

Definition at line 2302 of file writefile.cc.

References BLO_write_raw(), and num.

Referenced by IDP_WriteArray(), and write_ui_data().

◆ BLO_write_file()

bool BLO_write_file ( Main * mainvar,
const char * filepath,
int write_flags,
const BlendFileWriteParams * params,
ReportList * reports )

◆ BLO_write_file_impl()

◆ BLO_write_file_mem()

bool BLO_write_file_mem ( Main * mainvar,
MemFile * compare,
MemFile * current,
int write_flags )
Returns
Success.

Definition at line 2161 of file writefile.cc.

References write_file_handle().

Referenced by BKE_memfile_undo_encode().

◆ BLO_write_float3_array()

void BLO_write_float3_array ( BlendWriter * writer,
const int64_t num,
const float * data_ptr )

◆ BLO_write_float_array()

◆ blo_write_id_struct()

void blo_write_id_struct ( BlendWriter * writer,
int struct_id,
const void * id_address,
const ID * id )

Write id struct.

Definition at line 2253 of file writefile.cc.

References GS, ID::name, BlendWriter::wd, and writestruct_at_address_nr().

◆ BLO_write_int16_array()

void BLO_write_int16_array ( BlendWriter * writer,
const int64_t num,
const int16_t * data_ptr )

Definition at line 2277 of file writefile.cc.

References BLO_write_raw(), and num.

Referenced by blender::bke::write_array_data().

◆ BLO_write_int32_array()

void BLO_write_int32_array ( BlendWriter * writer,
const int64_t num,
const int32_t * data_ptr )

◆ BLO_write_int8_array()

void BLO_write_int8_array ( BlendWriter * writer,
const int64_t num,
const int8_t * data_ptr )

◆ BLO_write_is_undo()

◆ BLO_write_pointer_array()

◆ BLO_write_raw()

void BLO_write_raw ( BlendWriter * writer,
size_t size_in_bytes,
const void * data_ptr )

Write raw data.

Warning
Avoid using this function if possible. There are only a very few cases in current code where it is actually needed (e.g. the ShapeKey's data, since its items size varies depending on the type of geometry owning it, see shapekey_blend_write).
Data written with this call have no type information attached to them in the blend-file. The main consequence is that there will be no handling of endianness conversion for them in readfile code. Basic types array functions (like BLO_write_int8_array etc.) also use BLO_write_raw internally, but if their matching read function is used to load the data (like BLO_read_int8_array), the read function will take care of endianness conversion.

Definition at line 2175 of file writefile.cc.

References BLO_CODE_DATA, BlendWriter::wd, and writedata().

Referenced by BLO_write_char_array(), BLO_write_double_array(), BLO_write_float3_array(), BLO_write_float_array(), BLO_write_int16_array(), BLO_write_int32_array(), BLO_write_int8_array(), BLO_write_string(), BLO_write_uint32_array(), BLO_write_uint8_array(), and shapekey_blend_write().

◆ BLO_write_shared()

void BLO_write_shared ( BlendWriter * writer,
const void * data,
size_t approximate_size_in_bytes,
const blender::ImplicitSharingInfo * sharing_info,
blender::FunctionRef< void()> write_fn )

Check if the data can be written more efficiently by making use of implicit-sharing. If yes, the user count of the sharing-info is increased making the data immutable. The provided callback should serialize the potentially shared data. It is only called when necessary.

This should be called before the data is referenced in other written data (there is an assert that checks for this). If that's not possible, at least BLO_write_shared_tag needs to be called before the pointer is first written.

Parameters
approximate_size_in_bytesUsed to be able to approximate how large the undo step is in total.
write_fnUse the #BlendWrite to serialize the potentially shared data.

Definition at line 2347 of file writefile.cc.

References Map< Key, Value, InlineBufferCapacity, ProbingStrategy, Hash, IsEqual, Slot, Allocator >::add(), Set< Key, InlineBufferCapacity, ProbingStrategy, Hash, IsEqual, Slot, Allocator >::add(), blender::ImplicitSharingInfo::add_user(), BLO_write_is_undo(), BLO_write_shared_tag(), data, get_address_id_int(), WriteData::mem, WriteData::per_id_written_shared_addresses, MemFile::shared_storage, MemFileSharedStorage::sharing_info_by_address_id, MemFile::size, blender::ImplicitSharingInfo::strong_users(), BlendWriter::wd, and MemFileWriteData::written_memfile.

Referenced by BKE_packedfile_blend_write(), blend_write(), blend_write(), blend_write(), blend_write(), blender::bke::CurvesGeometry::blend_write(), CustomData_blend_write(), mesh_blend_write(), and blender::bke::write_shared_array().

◆ BLO_write_shared_tag()

◆ BLO_write_string()

void BLO_write_string ( BlendWriter * writer,
const char * data_ptr )

Write a null terminated string.

Definition at line 2325 of file writefile.cc.

References BLO_write_raw().

Referenced by BKE_animdata_blend_write(), BKE_asset_catalog_path_list_blend_write(), BKE_asset_metadata_write(), BKE_asset_weak_reference_write(), BKE_constraint_blend_write(), BKE_fcurve_blend_write_data(), BKE_id_blend_write(), BKE_keyingsets_blend_write(), BKE_paint_blend_write(), BKE_preferences_extension_repo_write_data(), BKE_viewer_path_blend_write(), blender::bke::AttributeStorage::blend_write(), blender::blend_write(), blender::nodes::BakeItemsAccessor::blend_write_item(), blender::nodes::CaptureAttributeItemsAccessor::blend_write_item(), blender::nodes::ClosureInputItemsAccessor::blend_write_item(), blender::nodes::ClosureOutputItemsAccessor::blend_write_item(), blender::nodes::CombineBundleItemsAccessor::blend_write_item(), blender::nodes::EvaluateClosureInputItemsAccessor::blend_write_item(), blender::nodes::EvaluateClosureOutputItemsAccessor::blend_write_item(), blender::nodes::FieldToGridItemsAccessor::blend_write_item(), blender::nodes::FileOutputItemsAccessor::blend_write_item(), blender::nodes::ForeachGeometryElementGenerationItemsAccessor::blend_write_item(), blender::nodes::ForeachGeometryElementInputItemsAccessor::blend_write_item(), blender::nodes::ForeachGeometryElementMainItemsAccessor::blend_write_item(), blender::nodes::FormatStringItemsAccessor::blend_write_item(), blender::nodes::GeoViewerItemsAccessor::blend_write_item(), blender::nodes::MenuSwitchItemsAccessor::blend_write_item(), blender::nodes::RepeatItemsAccessor::blend_write_item(), blender::nodes::SeparateBundleItemsAccessor::blend_write_item(), blender::nodes::SimulationItemsAccessor::blend_write_item(), camera_blend_write(), curve_blend_write(), curves_blend_write(), blender::bke::node_interface::item_types::item_write_data(), mesh_blend_write(), blender::nodes::node_composite_file_output_cc::node_blend_write(), blender::nodes::node_fn_input_string_cc::node_blend_write(), blender::bke::node_blend_write_storage(), blender::bke::node_tree_blend_write(), blender::ed::asset::shelf::settings_blend_write(), blender::ed::spreadsheet::spreadsheet_blend_write(), blender::ed::spreadsheet::spreadsheet_column_blend_write(), blender::ed::spreadsheet::spreadsheet_column_id_blend_write(), blender::ed::spreadsheet::spreadsheet_table_id_blend_write_content_geometry(), blender::seq::strip_write_data_cb(), text_blend_write(), write_layer(), write_layer_tree_group(), write_panel_list(), and write_ui_data().

◆ BLO_write_struct_array_at_address_by_id()

void BLO_write_struct_array_at_address_by_id ( BlendWriter * writer,
int struct_id,
int64_t array_size,
const void * address,
const void * data_ptr )

Write struct array at address.

Definition at line 2229 of file writefile.cc.

References BLO_CODE_DATA, BlendWriter::wd, and writestruct_at_address_nr().

◆ BLO_write_struct_array_by_id()

void BLO_write_struct_array_by_id ( BlendWriter * writer,
const int struct_id,
const int64_t array_size,
const void * data_ptr )

◆ BLO_write_struct_array_by_name()

void BLO_write_struct_array_by_name ( BlendWriter * writer,
const char * struct_name,
int64_t array_size,
const void * data_ptr )

◆ BLO_write_struct_at_address_by_id()

void BLO_write_struct_at_address_by_id ( BlendWriter * writer,
int struct_id,
const void * address,
const void * data_ptr )

Write single struct at address.

Definition at line 2203 of file writefile.cc.

References BLO_CODE_DATA, and BLO_write_struct_at_address_by_id_with_filecode().

◆ BLO_write_struct_at_address_by_id_with_filecode()

void BLO_write_struct_at_address_by_id_with_filecode ( BlendWriter * writer,
int filecode,
int struct_id,
const void * address,
const void * data_ptr )

Write single struct at address and specify a file-code.

Definition at line 2212 of file writefile.cc.

References BlendWriter::wd, and writestruct_at_address_nr().

Referenced by BLO_write_struct_at_address_by_id().

◆ BLO_write_struct_by_id()

void BLO_write_struct_by_id ( BlendWriter * writer,
const int struct_id,
const void * data_ptr )

◆ BLO_write_struct_by_name()

◆ BLO_write_struct_list_by_id()

void BLO_write_struct_list_by_id ( BlendWriter * writer,
const int struct_id,
const ListBase * list )

Definition at line 2238 of file writefile.cc.

References BLO_CODE_DATA, BlendWriter::wd, and writelist_nr().

Referenced by BLO_write_struct_list_by_name().

◆ BLO_write_struct_list_by_name()

void BLO_write_struct_list_by_name ( BlendWriter * writer,
const char * struct_name,
ListBase * list )

Write struct list.

Definition at line 2243 of file writefile.cc.

References BLO_get_struct_id_by_name(), BLO_write_struct_list_by_id(), CLOG_ERROR, LOG, and UNLIKELY.

◆ BLO_write_uint32_array()

void BLO_write_uint32_array ( BlendWriter * writer,
const int64_t num,
const uint32_t * data_ptr )

Definition at line 2292 of file writefile.cc.

References BLO_write_raw(), and num.

Referenced by BKE_previewimg_blend_write(), and BKE_ptcache_blend_write().

◆ BLO_write_uint8_array()

void BLO_write_uint8_array ( BlendWriter * writer,
const int64_t num,
const uint8_t * data_ptr )

Definition at line 2282 of file writefile.cc.

References BLO_write_raw(), and num.

Referenced by blend_write_layer_data(), and blender::bke::write_array_data().

◆ current_screen_compat()

void current_screen_compat ( Main * mainvar,
const bool use_active_win,
bScreen ** r_screen,
Scene ** r_scene,
ViewLayer ** r_view_layer )
static

Take care using 'use_active_win', since we won't want the currently active window to change which scene renders (currently only used for undo).

Definition at line 1142 of file writefile.cc.

References wmWindow::active, BKE_view_layer_find(), BKE_workspace_active_screen_get(), ListBase::first, wmWindow::next, wmWindow::scene, wmWindow::view_layer_name, wmWindowManager::windows, Main::wm, and wmWindow::workspace_hook.

Referenced by write_global(), and write_renderinfo().

◆ do_history()

bool do_history ( const char * filepath,
ReportList * reports )
static

Do reverse file history: .blend1 -> .blend2, .blend -> .blend1 ... etc.

Returns
True on success.

Definition at line 1887 of file writefile.cc.

References BKE_report(), BLI_exists(), BLI_rename_overwrite(), FILE_MAX, min_ii(), RPT_ERROR, SNPRINTF, and U.

Referenced by BLO_write_file_impl().

◆ gather_local_ids_to_write()

◆ get_address_id()

const void * get_address_id ( WriteData & wd,
const void * address )
static

Definition at line 926 of file writefile.cc.

References get_address_id_int().

Referenced by BLO_write_pointer_array(), writedata(), and writestruct_at_address_nr().

◆ get_address_id_for_implicit_sharing_data()

uint64_t get_address_id_for_implicit_sharing_data ( const void * data)
static

When writing an undo step, implicitly shared pointers do not use stable-pointers because that would lead to incorrect detection if a data-block has been changed between undo steps. That's because different shared data could be mapped to the same stable pointer, leading to #is_memchunk_identical to being true even if the referenced data is actually different.

Another way to look at it is that implicit-sharing is a system for stable pointers (at runtime) itself. So it does not need an additional layer of stable pointers on top.

Definition at line 903 of file writefile.cc.

References BLI_assert, data, and implicit_sharing_address_id_flag.

Referenced by BLO_write_shared_tag().

◆ get_address_id_int()

◆ get_blend_file_header()

◆ get_next_stable_address_id()

◆ get_stable_pointer_hint_for_id()

uint64_t get_stable_pointer_hint_for_id ( const ID & id,
const bool is_undo )
static

Definition at line 688 of file writefile.cc.

References id_hash, lib, and name.

Referenced by mywrite_id_begin(), and prepare_stable_data_block_ids().

◆ mywrite()

void mywrite ( WriteData * wd,
const void * adr,
size_t len )
static

Low level WRITE(2) wrapper that buffers data

Parameters
adrPointer to new chunk of data
lenLength of new chunk of data

Definition at line 597 of file writefile.cc.

References BLI_assert, WriteData::buf, WriteData::buffer, WriteData::chunk_size, WriteData::critical_error, len, WriteData::max_size, UNLIKELY, WriteData::used_len, WriteData::validation_data, and writedata_do_write().

Referenced by write_bhead(), write_blend_file_header(), writedata(), and writestruct_at_address_nr().

◆ mywrite_begin()

WriteData * mywrite_begin ( WriteWrap * ww,
MemFile * compare,
MemFile * current )
static

BeGiN initializer for mywrite

Parameters
wwFile write wrapper.
comparePrevious memory file (can be nullptr).
currentThe current memory file (can be nullptr).
Warning
Talks to other functions with global parameters

Definition at line 653 of file writefile.cc.

References BLO_memfile_write_init(), WriteData::mem, WriteData::use_memfile, and writedata_new().

Referenced by write_file_handle().

◆ mywrite_end()

bool mywrite_end ( WriteData * wd)
static

END the mywrite wrapper

Returns
True if write failed
unknown global variable otherwise
Warning
Talks to other functions with global parameters

Definition at line 671 of file writefile.cc.

References BLO_memfile_write_finalize(), WriteData::buf, WriteData::buffer, WriteData::critical_error, WriteData::mem, WriteData::use_memfile, WriteData::used_len, WriteData::validation_data, writedata_do_write(), and writedata_free().

Referenced by write_file_handle().

◆ mywrite_flush()

void mywrite_flush ( WriteData * wd)
static

Flush helps the de-duplicating memory for undo-save by logically segmenting data, so differences in one part of memory won't cause unrelated data to be duplicated.

Definition at line 584 of file writefile.cc.

References WriteData::buf, WriteData::buffer, WriteData::used_len, and writedata_do_write().

Referenced by mywrite_id_end(), write_file_handle(), and write_libraries().

◆ mywrite_id_begin()

◆ mywrite_id_end()

◆ prepare_stable_data_block_ids()

void prepare_stable_data_block_ids ( WriteData & wd,
Main & bmain )
static

◆ stable_id_from_hint()

uint64_t stable_id_from_hint ( const uint64_t hint)
static

Definition at line 866 of file writefile.cc.

References implicit_sharing_address_id_flag.

Referenced by get_next_stable_address_id().

◆ write_at_address_validate()

bool write_at_address_validate ( WriteData * wd,
const int filecode,
const void * address )
static

Return false if the given 'old' address is not valid in current context. The block should not be written in that case.

Note
Currently only checks that BLO_CODE_DATA blocks written as part of an ID data never match an already written one for the same ID.

Definition at line 808 of file writefile.cc.

References Set< Key, InlineBufferCapacity, ProbingStrategy, Hash, IsEqual, Slot, Allocator >::add(), BLO_CODE_DATA, CLOG_ERROR, WriteData::is_writing_id, LOG, WriteData::per_id_addresses_set, WriteData::use_memfile, and WriteData::validation_data.

Referenced by writedata(), and writestruct_at_address_nr().

◆ write_bhead()

◆ write_blend_file_header()

void write_blend_file_header ( WriteData * wd)
static

Definition at line 1659 of file writefile.cc.

References get_blend_file_header(), and mywrite().

Referenced by write_file_handle().

◆ write_file_handle()

◆ write_file_main_validate_post()

void write_file_main_validate_post ( Main * bmain,
ReportList * reports )
static

Definition at line 1952 of file writefile.cc.

References BKE_report(), BLO_main_validate_libraries(), G, G_DEBUG_IO, Main::lock, and RPT_DEBUG.

Referenced by BLO_write_file_impl().

◆ write_file_main_validate_pre()

void write_file_main_validate_pre ( Main * bmain,
ReportList * reports )
static

◆ write_global()

◆ write_id()

void write_id ( WriteData * wd,
ID * id )
static

Writes ID and all its direct data to the file.

Definition at line 1318 of file writefile.cc.

References BKE_idtype_get_info_from_id(), IDTypeInfo::blend_write, BLO_Write_IDBuffer::get(), mywrite_id_begin(), mywrite_id_end(), and WriteData::use_memfile.

Referenced by write_file_handle(), and write_libraries().

◆ write_id_direct_linked_data_process_cb()

◆ write_id_placeholder()

void write_id_placeholder ( WriteData * wd,
ID * id )
static

◆ write_keymapitem()

void write_keymapitem ( BlendWriter * writer,
const wmKeyMapItem * kmi )
static

Definition at line 1215 of file writefile.cc.

References BLO_write_struct, IDP_BlendWrite(), and wmKeyMapItem::properties.

Referenced by write_userdef().

◆ write_libraries()

◆ write_raw_data_in_debug_file()

void write_raw_data_in_debug_file ( WriteData * wd,
const size_t len,
const void * address_id,
const void * data )
static

Definition at line 1013 of file writefile.cc.

References data, WriteData::debug_dst, i, and len.

Referenced by writedata().

◆ write_renderinfo()

void write_renderinfo ( WriteData * wd,
Main * mainvar )
static

This was originally added for the historic render-daemon feature, now write because it can be easily extracted without reading the whole blend file.

See: scripts/modules/blend_render_info.py

Definition at line 1192 of file writefile.cc.

References BLO_CODE_REND, current_screen_compat(), data, ID_IS_LINKED, LISTBASE_FOREACH, R_BG_RENDER, Main::scenes, STRNCPY(), and writedata().

Referenced by write_file_handle().

◆ write_thumb()

void write_thumb ( WriteData * wd,
const BlendThumbnail * thumb )
static

Preview image, first 2 values are width and height second are an RGBA image (uchar).

Note
this uses 'TEST' since new types will segfault on file load for older blender versions.

Definition at line 1503 of file writefile.cc.

References BLEN_THUMB_MEMSIZE_FILE, BLO_CODE_TEST, BlendThumbnail::height, BlendThumbnail::width, and writedata().

Referenced by write_file_handle().

◆ write_userdef()

◆ writedata() [1/2]

void writedata ( WriteData * wd,
const int filecode,
const size_t len,
const void * adr )
static

Definition at line 1082 of file writefile.cc.

References len, and writedata().

◆ writedata() [2/2]

◆ writedata_do_write()

void writedata_do_write ( WriteData * wd,
const void * mem,
const size_t memlen )
static

◆ writedata_free()

void writedata_free ( WriteData * wd)
static

Definition at line 566 of file writefile.cc.

References WriteData::buf, WriteData::buffer, and MEM_freeN().

Referenced by mywrite_end().

◆ writedata_new()

◆ writelist_nr()

void writelist_nr ( WriteData * wd,
const int filecode,
const int struct_nr,
const ListBase * lb )
static

Use this to force writing of lists in same order as reading (using link_list).

Definition at line 1090 of file writefile.cc.

References ListBase::first, Link::next, and writestruct_nr().

Referenced by BLO_write_struct_list_by_id().

◆ writestruct_at_address_nr()

◆ writestruct_nr()

void writestruct_nr ( WriteData * wd,
const int filecode,
const int struct_nr,
const int64_t nr,
const void * adr )
static

Variable Documentation

◆ implicit_sharing_address_id_flag

uint64_t implicit_sharing_address_id_flag = uint64_t(1) << 63
constexpr

This bit is used to mark address ids that use implicit sharing during undo.

Definition at line 864 of file writefile.cc.

Referenced by get_address_id_for_implicit_sharing_data(), and stable_id_from_hint().

◆ LOG

CLG_LogRef LOG = {"blend.writefile"}
static

Definition at line 163 of file writefile.cc.