StudioLight: Better API

In stead of a single refresh function that re-init the whole system. The
API now supports adding and removing. Which will be much faster and less
flickering of missing icons when adding/removing lights
This commit is contained in:
Jeroen Bakker 2018-06-22 14:48:23 +02:00
parent f4d6e66b25
commit dfca352294
Notes: blender-bot 2023-02-14 06:00:51 +01:00
Referenced by issue #55646, crash when right click in dope sheet delete key menu
4 changed files with 91 additions and 43 deletions

View File

@ -2456,7 +2456,7 @@ class WM_OT_studiolight_install(Operator):
for filepath in filepaths:
shutil.copy(str(filepath), str(path_studiolights))
userpref.studio_lights_refresh()
userpref.studio_lights.new(str(path_studiolights.joinpath(filepath.name)), self.orientation)
# print message
msg = (
@ -2490,26 +2490,11 @@ class WM_OT_studiolight_uninstall(Operator):
self._remove_path(pathlib.Path(studio_light.path))
self._remove_path(pathlib.Path(studio_light.path_irr_cache))
self._remove_path(pathlib.Path(studio_light.path_sh_cache))
userpref.studio_lights_refresh()
userpref.studio_lights.remove(studio_light)
return {'FINISHED'}
return {'CANCELLED'}
class WM_OT_studiolight_expand(Operator):
bl_idname = "wm.studiolight_expand"
bl_label = "Expand Studio Light"
index = bpy.props.IntProperty()
def execute(self, context):
userpref = context.user_preferences
for studio_light in userpref.studio_lights:
if studio_light.index == self.index:
studio_light.show_expanded = not studio_light.show_expanded
break
return {'FINISHED'}
class WM_OT_studiolight_userpref_show(Operator):
"""Show light user preferences"""
bl_idname = "wm.studiolight_userpref_show"
@ -2576,7 +2561,6 @@ classes = (
WM_OT_owner_disable,
WM_OT_owner_enable,
WM_OT_url_open,
WM_OT_studiolight_expand,
WM_OT_studiolight_install,
WM_OT_studiolight_uninstall,
WM_OT_studiolight_userpref_show,

View File

@ -145,6 +145,8 @@ void BKE_studiolight_preview(uint* icon_buffer, StudioLight *sl, int icon_id_typ
struct ListBase *BKE_studiolight_listbase(void);
void BKE_studiolight_ensure_flag(StudioLight *sl, int flag);
void BKE_studiolight_refresh(void);
StudioLight *BKE_studiolight_new(const char* path, int orientation);
void BKE_studiolight_remove(StudioLight *sl);
void BKE_studiolight_set_free_function(StudioLight *sl, StudioLightFreeFunction *free_function, void *data);
void BKE_studiolight_unset_icon_id(StudioLight *sl, int icon_id);

View File

@ -57,6 +57,7 @@
/* Statics */
static ListBase studiolights;
static int last_studiolight_id = 0;
#define STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE 128
#define STUDIOLIGHT_IRRADIANCE_EQUIRECTANGULAR_HEIGHT 32
#define STUDIOLIGHT_IRRADIANCE_EQUIRECTANGULAR_WIDTH (STUDIOLIGHT_IRRADIANCE_EQUIRECTANGULAR_HEIGHT * 2)
@ -141,7 +142,7 @@ static struct StudioLight *studiolight_create(int flag)
sl->path_sh_cache = NULL;
sl->free_function = NULL;
sl->flag = flag;
sl->index = BLI_listbase_count(&studiolights);
sl->index = ++last_studiolight_id;
if (flag & STUDIOLIGHT_ORIENTATION_VIEWNORMAL) {
sl->icon_id_matcap = BKE_icon_ensure_studio_light(sl, STUDIOLIGHT_ICON_ID_TYPE_MATCAP);
sl->icon_id_matcap_flipped = BKE_icon_ensure_studio_light(sl, STUDIOLIGHT_ICON_ID_TYPE_MATCAP_FLIPPED);
@ -822,9 +823,24 @@ static void studiolight_calculate_light_direction(StudioLight *sl)
sl->flag |= STUDIOLIGHT_LIGHT_DIRECTION_CALCULATED;
}
static StudioLight* studiolight_add_file(const char *path, int flag)
{
char filename[FILE_MAXFILE];
BLI_split_file_part(path, filename, FILE_MAXFILE);
if (BLI_path_extension_check_array(filename, imb_ext_image)) {
StudioLight *sl = studiolight_create(STUDIOLIGHT_EXTERNAL_FILE | flag);
BLI_strncpy(sl->name, filename, FILE_MAXFILE);
BLI_strncpy(sl->path, path, FILE_MAXFILE);
sl->path_irr_cache = BLI_string_joinN(path, ".irr");
sl->path_sh_cache = BLI_string_joinN(path, ".sh2");
BLI_addtail(&studiolights, sl);
return sl;
}
return NULL;
}
static void studiolight_add_files_from_datafolder(const int folder_id, const char *subfolder, int flag)
{
StudioLight *sl;
struct direntry *dir;
const char *folder = BKE_appdir_folder_id(folder_id, subfolder);
if (folder) {
@ -832,16 +848,7 @@ static void studiolight_add_files_from_datafolder(const int folder_id, const cha
int i;
for (i = 0; i < totfile; i++) {
if ((dir[i].type & S_IFREG)) {
const char *filename = dir[i].relname;
const char *path = dir[i].path;
if (BLI_path_extension_check_array(filename, imb_ext_image)) {
sl = studiolight_create(STUDIOLIGHT_EXTERNAL_FILE | flag);
BLI_strncpy(sl->name, filename, FILE_MAXFILE);
BLI_strncpy(sl->path, path, FILE_MAXFILE);
sl->path_irr_cache = BLI_string_joinN(path, ".irr");
sl->path_sh_cache = BLI_string_joinN(path, ".sh2");
BLI_addtail(&studiolights, sl);
}
studiolight_add_file(dir[i].path, flag);
}
}
BLI_filelist_free(dir, totfile);
@ -1158,6 +1165,24 @@ void BKE_studiolight_ensure_flag(StudioLight *sl, int flag)
}
}
/*
* Python API Functions
*/
void BKE_studiolight_remove(StudioLight *sl)
{
if (sl->flag & STUDIOLIGHT_USER_DEFINED)
{
BLI_remlink(&studiolights, sl);
studiolight_free(sl);
}
}
StudioLight *BKE_studiolight_new(const char* path, int orientation)
{
StudioLight * sl = studiolight_add_file(path, orientation | STUDIOLIGHT_USER_DEFINED);
return sl;
}
void BKE_studiolight_refresh(void)
{
BKE_studiolight_free();

View File

@ -86,6 +86,14 @@ static const EnumPropertyItem rna_enum_language_default_items[] = {
};
#endif
static const EnumPropertyItem rna_enum_studio_light_orientation_items[] = {
{STUDIOLIGHT_ORIENTATION_CAMERA, "CAMERA", 0, "Camera", ""},
{STUDIOLIGHT_ORIENTATION_WORLD, "WORLD", 0, "World", ""},
{STUDIOLIGHT_ORIENTATION_VIEWNORMAL, "MATCAP", 0, "MatCap", ""},
{0, NULL, 0, NULL, NULL}
};
#ifdef RNA_RUNTIME
#include "DNA_object_types.h"
@ -661,11 +669,21 @@ static void rna_UserDef_studiolight_begin(CollectionPropertyIterator *iter, Poin
rna_iterator_listbase_begin(iter, BKE_studiolight_listbase(), NULL);
}
static void rna_UserDef_studiolight_refresh(UserDef *UNUSED(userdef))
static void rna_StudioLights_refresh(UserDef *UNUSED(userdef))
{
BKE_studiolight_refresh();
}
static void rna_StudioLights_remove(UserDef *UNUSED(userdef), StudioLight *studio_light)
{
BKE_studiolight_remove(studio_light);
}
static StudioLight* rna_StudioLights_new(UserDef *UNUSED(userdef), const char* path, int orientation)
{
return BKE_studiolight_new(path, orientation);
}
/* StudioLight.name */
static void rna_UserDef_studiolight_name_get(PointerRNA *ptr, char *value)
{
@ -3253,18 +3271,39 @@ static void rna_def_userdef_addon(BlenderRNA *brna)
RNA_def_property_pointer_funcs(prop, "rna_Addon_preferences_get", NULL, NULL, NULL);
}
static void rna_def_userdef_studiolights(BlenderRNA *brna)
{
StructRNA *srna;
FunctionRNA *func;
PropertyRNA *parm;
srna = RNA_def_struct(brna, "StudioLights", NULL);
RNA_def_struct_sdna(srna, "UserDef");
RNA_def_struct_ui_text(srna, "Studio Lights", "Collection of studio lights");
func = RNA_def_function(srna, "new", "rna_StudioLights_new");
RNA_def_function_ui_description(func, "Create a new studiolight");
parm = RNA_def_string(func, "path", NULL, 0, "File Path", "File path where the studio light file can be found");
RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
parm = RNA_def_enum(func, "orientation", rna_enum_studio_light_orientation_items, STUDIOLIGHT_ORIENTATION_WORLD, "Orientation", "The orientation for the new studio light");
RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
parm = RNA_def_pointer(func, "studio_light", "StudioLight", "", "Newly created StudioLight");
RNA_def_function_return(func, parm);
func = RNA_def_function(srna, "remove", "rna_StudioLights_remove");
RNA_def_function_ui_description(func, "Remove a studio light");
parm = RNA_def_pointer(func, "studio_light", "StudioLight", "", "The studio light to remove");
RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
func = RNA_def_function(srna, "refresh", "rna_StudioLights_refresh");
RNA_def_function_ui_description(func, "Refresh Studio Lights from disk");
}
static void rna_def_userdef_studiolight(BlenderRNA *brna)
{
StructRNA *srna;
PropertyRNA *prop;
static const EnumPropertyItem rna_enum_studio_light_orientation_items[] = {
{STUDIOLIGHT_ORIENTATION_CAMERA, "CAMERA", 0, "Camera", ""},
{STUDIOLIGHT_ORIENTATION_WORLD, "WORLD", 0, "World", ""},
{STUDIOLIGHT_ORIENTATION_VIEWNORMAL, "MATCAP", 0, "MatCap", ""},
{0, NULL, 0, NULL, NULL}
};
RNA_define_verify_sdna(false);
srna = RNA_def_struct(brna, "StudioLight", NULL);
RNA_def_struct_clear_flag(srna, STRUCT_UNDO);
@ -4833,7 +4872,6 @@ void RNA_def_userdef(BlenderRNA *brna)
{
StructRNA *srna;
PropertyRNA *prop;
FunctionRNA *func;
static const EnumPropertyItem user_pref_sections[] = {
{USER_SECTION_INTERFACE, "INTERFACE", 0, "Interface", ""},
@ -4927,16 +4965,14 @@ void RNA_def_userdef(BlenderRNA *brna)
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
RNA_def_property_flag(prop, PROP_THICK_WRAP);
/* StudioLight Collection */
prop = RNA_def_property(srna, "studio_lights", PROP_COLLECTION, PROP_NONE);
RNA_def_property_struct_type(prop, "StudioLight");
RNA_def_property_srna(prop, "StudioLights");
RNA_def_property_collection_funcs(
prop, "rna_UserDef_studiolight_begin", "rna_iterator_listbase_next",
"rna_iterator_listbase_end", "rna_iterator_listbase_get",
NULL, NULL, NULL, NULL);
func = RNA_def_function(srna, "studio_lights_refresh", "rna_UserDef_studiolight_refresh");
RNA_def_function_ui_description(func, "Refresh Studio Lights");
RNA_def_property_ui_text(prop, "Studio Lights", "");
rna_def_userdef_view(brna);
@ -4946,6 +4982,7 @@ void RNA_def_userdef(BlenderRNA *brna)
rna_def_userdef_system(brna);
rna_def_userdef_addon(brna);
rna_def_userdef_addon_pref(brna);
rna_def_userdef_studiolights(brna);
rna_def_userdef_studiolight(brna);
rna_def_userdef_pathcompare(brna);