From 9f15bcb218ef32d5f15e1e13235d2d7fa667e04a Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 6 Oct 2015 19:40:15 +1100 Subject: RNA: Add check_existing arg to other load() funcs Note: movieclip was doing this already by default, now split into 2 functions, matching image behavior. --- source/blender/blenkernel/BKE_font.h | 4 ++- source/blender/blenkernel/BKE_movieclip.h | 2 ++ source/blender/blenkernel/BKE_sound.h | 4 ++- source/blender/blenkernel/intern/font.c | 45 ++++++++++++++++++++++---- source/blender/blenkernel/intern/image.c | 2 +- source/blender/blenkernel/intern/movieclip.c | 46 +++++++++++++++++++-------- source/blender/blenkernel/intern/sound.c | 43 +++++++++++++++++++++---- source/blender/editors/space_clip/clip_ops.c | 2 +- source/blender/makesrna/intern/rna_main_api.c | 39 +++++++++++++++++------ 9 files changed, 147 insertions(+), 40 deletions(-) diff --git a/source/blender/blenkernel/BKE_font.h b/source/blender/blenkernel/BKE_font.h index 137670215cc..f20c166aa6d 100644 --- a/source/blender/blenkernel/BKE_font.h +++ b/source/blender/blenkernel/BKE_font.h @@ -80,7 +80,9 @@ void BKE_vfont_builtin_register(void *mem, int size); void BKE_vfont_free_data(struct VFont *vfont); void BKE_vfont_free(struct VFont *sc); struct VFont *BKE_vfont_builtin_get(void); -struct VFont *BKE_vfont_load(struct Main *bmain, const char *name); +struct VFont *BKE_vfont_load(struct Main *bmain, const char *filepath); +struct VFont *BKE_vfont_load_exists_ex(struct Main *bmain, const char *filepath, bool *r_exists); +struct VFont *BKE_vfont_load_exists(struct Main *bmain, const char *filepath); bool BKE_vfont_to_curve_ex(struct Main *bmain, struct Object *ob, int mode, struct ListBase *r_nubase, diff --git a/source/blender/blenkernel/BKE_movieclip.h b/source/blender/blenkernel/BKE_movieclip.h index 7d7675270de..afca326c727 100644 --- a/source/blender/blenkernel/BKE_movieclip.h +++ b/source/blender/blenkernel/BKE_movieclip.h @@ -43,6 +43,8 @@ void BKE_movieclip_free(struct MovieClip *clip); void BKE_movieclip_unlink(struct Main *bmain, struct MovieClip *clip); struct MovieClip *BKE_movieclip_file_add(struct Main *bmain, const char *name); +struct MovieClip *BKE_movieclip_file_add_exists_ex(struct Main *bmain, const char *name, bool *r_exists); +struct MovieClip *BKE_movieclip_file_add_exists(struct Main *bmain, const char *name); void BKE_movieclip_reload(struct MovieClip *clip); void BKE_movieclip_clear_cache(struct MovieClip *clip); void BKE_movieclip_clear_proxy_cache(struct MovieClip *clip); diff --git a/source/blender/blenkernel/BKE_sound.h b/source/blender/blenkernel/BKE_sound.h index e68be701b61..67db2537c8f 100644 --- a/source/blender/blenkernel/BKE_sound.h +++ b/source/blender/blenkernel/BKE_sound.h @@ -61,7 +61,9 @@ void BKE_sound_exit(void); void BKE_sound_force_device(const char *device); -struct bSound *BKE_sound_new_file(struct Main *main, const char *filename); +struct bSound *BKE_sound_new_file(struct Main *main, const char *filepath); +struct bSound *BKE_sound_new_file_exists_ex(struct Main *bmain, const char *filepath, bool *r_exists); +struct bSound *BKE_sound_new_file_exists(struct Main *bmain, const char *filepath); // XXX unused currently #if 0 diff --git a/source/blender/blenkernel/intern/font.c b/source/blender/blenkernel/intern/font.c index 23261b63486..e3ebb7f908c 100644 --- a/source/blender/blenkernel/intern/font.c +++ b/source/blender/blenkernel/intern/font.c @@ -203,7 +203,7 @@ static VFontData *vfont_get_data(Main *bmain, VFont *vfont) return vfont->data; } -VFont *BKE_vfont_load(Main *bmain, const char *name) +VFont *BKE_vfont_load(Main *bmain, const char *filepath) { char filename[FILE_MAXFILE]; VFont *vfont = NULL; @@ -211,16 +211,16 @@ VFont *BKE_vfont_load(Main *bmain, const char *name) PackedFile *temp_pf = NULL; bool is_builtin; - if (STREQ(name, FO_BUILTIN_NAME)) { - BLI_strncpy(filename, name, sizeof(filename)); + if (STREQ(filepath, FO_BUILTIN_NAME)) { + BLI_strncpy(filename, filepath, sizeof(filename)); pf = get_builtin_packedfile(); is_builtin = true; } else { - BLI_split_file_part(name, filename, sizeof(filename)); - pf = newPackedFile(NULL, name, bmain->name); - temp_pf = newPackedFile(NULL, name, bmain->name); + BLI_split_file_part(filepath, filename, sizeof(filename)); + pf = newPackedFile(NULL, filepath, bmain->name); + temp_pf = newPackedFile(NULL, filepath, bmain->name); is_builtin = false; } @@ -237,7 +237,7 @@ VFont *BKE_vfont_load(Main *bmain, const char *name) if (vfd->name[0] != '\0') { BLI_strncpy(vfont->id.name + 2, vfd->name, sizeof(vfont->id.name) - 2); } - BLI_strncpy(vfont->name, name, sizeof(vfont->name)); + BLI_strncpy(vfont->name, filepath, sizeof(vfont->name)); /* if autopack is on store the packedfile in de font structure */ if (!is_builtin && (G.fileflags & G_AUTOPACK)) { @@ -259,6 +259,37 @@ VFont *BKE_vfont_load(Main *bmain, const char *name) return vfont; } +VFont *BKE_vfont_load_exists_ex(struct Main *bmain, const char *filepath, bool *r_exists) +{ + VFont *vfont; + char str[FILE_MAX], strtest[FILE_MAX]; + + BLI_strncpy(str, filepath, sizeof(str)); + BLI_path_abs(str, bmain->name); + + /* first search an identical filepath */ + for (vfont = bmain->vfont.first; vfont; vfont = vfont->id.next) { + BLI_strncpy(strtest, vfont->name, sizeof(vfont->name)); + BLI_path_abs(strtest, ID_BLEND_PATH(bmain, &vfont->id)); + + if (BLI_path_cmp(strtest, str) == 0) { + vfont->id.us++; /* officially should not, it doesn't link here! */ + if (r_exists) + *r_exists = true; + return vfont; + } + } + + if (r_exists) + *r_exists = false; + return BKE_vfont_load(bmain, filepath); +} + +VFont *BKE_vfont_load_exists(struct Main *bmain, const char *filepath) +{ + return BKE_vfont_load_exists_ex(bmain, filepath, NULL); +} + static VFont *which_vfont(Curve *cu, CharInfo *info) { switch (info->flag & (CU_CHINFO_BOLD | CU_CHINFO_ITALIC)) { diff --git a/source/blender/blenkernel/intern/image.c b/source/blender/blenkernel/intern/image.c index 4a76c704130..c314131e246 100644 --- a/source/blender/blenkernel/intern/image.c +++ b/source/blender/blenkernel/intern/image.c @@ -746,7 +746,7 @@ Image *BKE_image_load_exists_ex(const char *filepath, bool *r_exists) BLI_strncpy(str, filepath, sizeof(str)); BLI_path_abs(str, G.main->name); - /* first search an identical image */ + /* first search an identical filepath */ for (ima = G.main->image.first; ima; ima = ima->id.next) { if (ima->source != IMA_SRC_VIEWER && ima->source != IMA_SRC_GENERATED) { BLI_strncpy(strtest, ima->name, sizeof(ima->name)); diff --git a/source/blender/blenkernel/intern/movieclip.c b/source/blender/blenkernel/intern/movieclip.c index 7a8c4ad4564..ed92c942b57 100644 --- a/source/blender/blenkernel/intern/movieclip.c +++ b/source/blender/blenkernel/intern/movieclip.c @@ -605,7 +605,7 @@ MovieClip *BKE_movieclip_file_add(Main *bmain, const char *name) MovieClip *clip; int file, len; const char *libname; - char str[FILE_MAX], strtest[FILE_MAX]; + char str[FILE_MAX]; BLI_strncpy(str, name, sizeof(str)); BLI_path_abs(str, bmain->name); @@ -616,19 +616,6 @@ MovieClip *BKE_movieclip_file_add(Main *bmain, const char *name) return NULL; close(file); - /* ** first search an identical clip ** */ - for (clip = bmain->movieclip.first; clip; clip = clip->id.next) { - BLI_strncpy(strtest, clip->name, sizeof(clip->name)); - BLI_path_abs(strtest, G.main->name); - - if (STREQ(strtest, str)) { - BLI_strncpy(clip->name, name, sizeof(clip->name)); /* for stringcode */ - clip->id.us++; /* officially should not, it doesn't link here! */ - - return clip; - } - } - /* ** add new movieclip ** */ /* create a short library name */ @@ -655,6 +642,37 @@ MovieClip *BKE_movieclip_file_add(Main *bmain, const char *name) return clip; } +MovieClip *BKE_movieclip_file_add_exists_ex(Main *bmain, const char *filepath, bool *r_exists) +{ + MovieClip *clip; + char str[FILE_MAX], strtest[FILE_MAX]; + + BLI_strncpy(str, filepath, sizeof(str)); + BLI_path_abs(str, bmain->name); + + /* first search an identical filepath */ + for (clip = bmain->movieclip.first; clip; clip = clip->id.next) { + BLI_strncpy(strtest, clip->name, sizeof(clip->name)); + BLI_path_abs(strtest, ID_BLEND_PATH(bmain, &clip->id)); + + if (BLI_path_cmp(strtest, str) == 0) { + clip->id.us++; /* officially should not, it doesn't link here! */ + if (r_exists) + *r_exists = true; + return clip; + } + } + + if (r_exists) + *r_exists = false; + return BKE_movieclip_file_add(bmain, filepath); +} + +MovieClip *BKE_movieclip_file_add_exists(Main *bmain, const char *filepath) +{ + return BKE_movieclip_file_add_exists_ex(bmain, filepath, NULL); +} + static void real_ibuf_size(MovieClip *clip, MovieClipUser *user, ImBuf *ibuf, int *width, int *height) { *width = ibuf->x; diff --git a/source/blender/blenkernel/intern/sound.c b/source/blender/blenkernel/intern/sound.c index 0b89931aa75..a800d8f3f38 100644 --- a/source/blender/blenkernel/intern/sound.c +++ b/source/blender/blenkernel/intern/sound.c @@ -71,7 +71,7 @@ static int sound_cfra; static char **audio_device_names = NULL; #endif -bSound *BKE_sound_new_file(struct Main *bmain, const char *filename) +bSound *BKE_sound_new_file(struct Main *bmain, const char *filepath) { bSound *sound; @@ -80,18 +80,18 @@ bSound *BKE_sound_new_file(struct Main *bmain, const char *filename) size_t len; - BLI_strncpy(str, filename, sizeof(str)); + BLI_strncpy(str, filepath, sizeof(str)); path = /*bmain ? bmain->name :*/ G.main->name; BLI_path_abs(str, path); - len = strlen(filename); - while (len > 0 && filename[len - 1] != '/' && filename[len - 1] != '\\') + len = strlen(filepath); + while (len > 0 && filepath[len - 1] != '/' && filepath[len - 1] != '\\') len--; - sound = BKE_libblock_alloc(bmain, ID_SO, filename + len); - BLI_strncpy(sound->name, filename, FILE_MAX); + sound = BKE_libblock_alloc(bmain, ID_SO, filepath + len); + BLI_strncpy(sound->name, filepath, FILE_MAX); /* sound->type = SOUND_TYPE_FILE; */ /* XXX unused currently */ BKE_sound_load(bmain, sound); @@ -99,6 +99,37 @@ bSound *BKE_sound_new_file(struct Main *bmain, const char *filename) return sound; } +bSound *BKE_sound_new_file_exists_ex(struct Main *bmain, const char *filepath, bool *r_exists) +{ + bSound *sound; + char str[FILE_MAX], strtest[FILE_MAX]; + + BLI_strncpy(str, filepath, sizeof(str)); + BLI_path_abs(str, bmain->name); + + /* first search an identical filepath */ + for (sound = bmain->sound.first; sound; sound = sound->id.next) { + BLI_strncpy(strtest, sound->name, sizeof(sound->name)); + BLI_path_abs(strtest, ID_BLEND_PATH(bmain, &sound->id)); + + if (BLI_path_cmp(strtest, str) == 0) { + sound->id.us++; /* officially should not, it doesn't link here! */ + if (r_exists) + *r_exists = true; + return sound; + } + } + + if (r_exists) + *r_exists = false; + return BKE_sound_new_file(bmain, filepath); +} + +bSound *BKE_sound_new_file_exists(struct Main *bmain, const char *filepath) +{ + return BKE_sound_new_file_exists_ex(bmain, filepath, NULL); +} + void BKE_sound_free(bSound *sound) { if (sound->packedfile) { diff --git a/source/blender/editors/space_clip/clip_ops.c b/source/blender/editors/space_clip/clip_ops.c index 55805e0b907..af3d460fe0f 100644 --- a/source/blender/editors/space_clip/clip_ops.c +++ b/source/blender/editors/space_clip/clip_ops.c @@ -213,7 +213,7 @@ static int open_exec(bContext *C, wmOperator *op) errno = 0; - clip = BKE_movieclip_file_add(bmain, str); + clip = BKE_movieclip_file_add_exists(bmain, str); if (!clip) { if (op->customdata) diff --git a/source/blender/makesrna/intern/rna_main_api.c b/source/blender/makesrna/intern/rna_main_api.c index dc7987c77e6..5a0da0b7651 100644 --- a/source/blender/makesrna/intern/rna_main_api.c +++ b/source/blender/makesrna/intern/rna_main_api.c @@ -441,12 +441,17 @@ static void rna_Main_metaballs_remove(Main *bmain, ReportList *reports, PointerR } } -static VFont *rna_Main_fonts_load(Main *bmain, ReportList *reports, const char *filepath) +static VFont *rna_Main_fonts_load(Main *bmain, ReportList *reports, const char *filepath, int check_existing) { VFont *font; - errno = 0; - font = BKE_vfont_load(bmain, filepath); + + if (check_existing) { + font = BKE_vfont_load_exists(bmain, filepath); + } + else { + font = BKE_vfont_load(bmain, filepath); + } if (!font) BKE_reportf(reports, RPT_ERROR, "Cannot read '%s': %s", filepath, @@ -558,9 +563,17 @@ static void rna_Main_speakers_remove(Main *bmain, ReportList *reports, PointerRN } } -static bSound *rna_Main_sounds_load(Main *bmain, const char *name) +static bSound *rna_Main_sounds_load(Main *bmain, const char *name, int check_existing) { - bSound *sound = BKE_sound_new_file(bmain, name); + bSound *sound; + + if (check_existing) { + sound = BKE_sound_new_file_exists(bmain, name); + } + else { + sound = BKE_sound_new_file(bmain, name); + } + id_us_min(&sound->id); return sound; } @@ -680,12 +693,18 @@ static void rna_Main_palettes_remove(Main *bmain, ReportList *reports, PointerRN } } -static MovieClip *rna_Main_movieclip_load(Main *bmain, ReportList *reports, const char *filepath) +static MovieClip *rna_Main_movieclip_load(Main *bmain, ReportList *reports, const char *filepath, int check_existing) { MovieClip *clip; errno = 0; - clip = BKE_movieclip_file_add(bmain, filepath); + + if (check_existing) { + clip = BKE_movieclip_file_add_exists(bmain, filepath); + } + else { + clip = BKE_movieclip_file_add(bmain, filepath); + } if (!clip) BKE_reportf(reports, RPT_ERROR, "Cannot read '%s': %s", filepath, @@ -1212,8 +1231,7 @@ void RNA_def_main_images(BlenderRNA *brna, PropertyRNA *cprop) RNA_def_function_ui_description(func, "Load a new image into the main database"); parm = RNA_def_string_file_path(func, "filepath", "File Path", 0, "", "path of the file to load"); RNA_def_property_flag(parm, PROP_REQUIRED); - RNA_def_boolean(func, "check_existing", false, "", - "Check whether this image filepath is already used, and return existing datablock in this case"); + RNA_def_boolean(func, "check_existing", false, "", "Using existing data-block if this file is already loaded"); /* return type */ parm = RNA_def_pointer(func, "image", "Image", "", "New image datablock"); RNA_def_function_return(func, parm); @@ -1358,6 +1376,7 @@ void RNA_def_main_fonts(BlenderRNA *brna, PropertyRNA *cprop) RNA_def_function_ui_description(func, "Load a new font into the main database"); parm = RNA_def_string_file_path(func, "filepath", "File Path", 0, "", "path of the font to load"); RNA_def_property_flag(parm, PROP_REQUIRED); + RNA_def_boolean(func, "check_existing", false, "", "Using existing data-block if this file is already loaded"); /* return type */ parm = RNA_def_pointer(func, "vfont", "VectorFont", "", "New font datablock"); RNA_def_function_return(func, parm); @@ -1621,6 +1640,7 @@ void RNA_def_main_sounds(BlenderRNA *brna, PropertyRNA *cprop) RNA_def_function_ui_description(func, "Add a new sound to the main database from a file"); parm = RNA_def_string_file_path(func, "filepath", "Path", FILE_MAX, "", "path for the datablock"); RNA_def_property_flag(parm, PROP_REQUIRED); + RNA_def_boolean(func, "check_existing", false, "", "Using existing data-block if this file is already loaded"); /* return type */ parm = RNA_def_pointer(func, "sound", "Sound", "", "New text datablock"); RNA_def_function_return(func, parm); @@ -1844,6 +1864,7 @@ void RNA_def_main_movieclips(BlenderRNA *brna, PropertyRNA *cprop) RNA_def_function_ui_description(func, "Add a new movie clip to the main database from a file"); parm = RNA_def_string_file_path(func, "filepath", "Path", FILE_MAX, "", "path for the datablock"); RNA_def_property_flag(parm, PROP_REQUIRED); + RNA_def_boolean(func, "check_existing", false, "", "Using existing data-block if this file is already loaded"); /* return type */ parm = RNA_def_pointer(func, "clip", "MovieClip", "", "New movie clip datablock"); RNA_def_function_return(func, parm); -- cgit v1.2.3