Fixes and edits to studio light operators
- Remove pathlib use (was converting to/from string with no real advantage). - Use user_resource(..., create=True) to ensure the path exists. - Pass full path to BKE_studiolight_create, don't add extension after. - Fix 'sl' filtering glob and move from ui code to operator. - Fix string copy length.
This commit is contained in:
		@@ -2451,33 +2451,24 @@ class WM_OT_studiolight_install(Operator):
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    def execute(self, context):
 | 
			
		||||
        import traceback
 | 
			
		||||
        import os
 | 
			
		||||
        import shutil
 | 
			
		||||
        import pathlib
 | 
			
		||||
        prefs = context.preferences
 | 
			
		||||
 | 
			
		||||
        filepaths = [pathlib.Path(self.directory, e.name) for e in self.files]
 | 
			
		||||
        path_studiolights = bpy.utils.user_resource('DATAFILES')
 | 
			
		||||
 | 
			
		||||
        filepaths = [os.path.join(self.directory, e.name) for e in self.files]
 | 
			
		||||
        path_studiolights = bpy.utils.user_resource('DATAFILES', "studiolights", create=True)
 | 
			
		||||
        if not path_studiolights:
 | 
			
		||||
            self.report({'ERROR'}, "Failed to get Studio Light path")
 | 
			
		||||
            self.report({'ERROR'}, "Failed to create Studio Light path")
 | 
			
		||||
            return {'CANCELLED'}
 | 
			
		||||
 | 
			
		||||
        path_studiolights = pathlib.Path(path_studiolights, "studiolights", self.type.lower())
 | 
			
		||||
        if not path_studiolights.exists():
 | 
			
		||||
            try:
 | 
			
		||||
                path_studiolights.mkdir(parents=True, exist_ok=True)
 | 
			
		||||
            except:
 | 
			
		||||
                traceback.print_exc()
 | 
			
		||||
 | 
			
		||||
        for filepath in filepaths:
 | 
			
		||||
            shutil.copy(str(filepath), str(path_studiolights))
 | 
			
		||||
            prefs.studio_lights.load(str(path_studiolights.joinpath(filepath.name)), self.type)
 | 
			
		||||
            shutil.copy(filepath, path_studiolights)
 | 
			
		||||
            prefs.studio_lights.load(os.path.join(path_studiolights, filepath), self.type)
 | 
			
		||||
 | 
			
		||||
        # print message
 | 
			
		||||
        msg = (
 | 
			
		||||
            tip_("StudioLight Installed %r into %r") %
 | 
			
		||||
            (", ".join(str(x.name) for x in self.files), str(path_studiolights))
 | 
			
		||||
            (", ".join(e.name for e in self.files), path_studiolights)
 | 
			
		||||
        )
 | 
			
		||||
        print(msg)
 | 
			
		||||
        self.report({'INFO'}, msg)
 | 
			
		||||
@@ -2485,6 +2476,10 @@ class WM_OT_studiolight_install(Operator):
 | 
			
		||||
 | 
			
		||||
    def invoke(self, context, event):
 | 
			
		||||
        wm = context.window_manager
 | 
			
		||||
 | 
			
		||||
        if self.type == 'STUDIO':
 | 
			
		||||
            self.filter_glob = "*.sl"
 | 
			
		||||
 | 
			
		||||
        wm.fileselect_add(self)
 | 
			
		||||
        return {'RUNNING_MODAL'}
 | 
			
		||||
 | 
			
		||||
@@ -2502,34 +2497,27 @@ class WM_OT_studiolight_new(Operator):
 | 
			
		||||
    ask_overide = False
 | 
			
		||||
 | 
			
		||||
    def execute(self, context):
 | 
			
		||||
        import pathlib
 | 
			
		||||
        import os
 | 
			
		||||
        prefs = context.preferences
 | 
			
		||||
        wm = context.window_manager
 | 
			
		||||
        filename = bpy.path.ensure_ext(self.filename, ".sl")
 | 
			
		||||
 | 
			
		||||
        path_studiolights = bpy.utils.user_resource('DATAFILES')
 | 
			
		||||
 | 
			
		||||
        path_studiolights = bpy.utils.user_resource('DATAFILES', os.path.join("studiolights", "studio"), create=True)
 | 
			
		||||
        if not path_studiolights:
 | 
			
		||||
            self.report({'ERROR'}, "Failed to get Studio Light path")
 | 
			
		||||
            return {'CANCELLED'}
 | 
			
		||||
 | 
			
		||||
        path_studiolights = pathlib.Path(path_studiolights, "studiolights", "studio")
 | 
			
		||||
        if not path_studiolights.exists():
 | 
			
		||||
            try:
 | 
			
		||||
                path_studiolights.mkdir(parents=True, exist_ok=True)
 | 
			
		||||
            except:
 | 
			
		||||
                traceback.print_exc()
 | 
			
		||||
 | 
			
		||||
        finalpath = str(path_studiolights.joinpath(self.filename))
 | 
			
		||||
        if pathlib.Path(finalpath + ".sl").is_file():
 | 
			
		||||
        filepath_final = os.path.join(path_studiolights, filename)
 | 
			
		||||
        if os.path.isfile(filepath_final):
 | 
			
		||||
            if not self.ask_overide:
 | 
			
		||||
                self.ask_overide = True
 | 
			
		||||
                return wm.invoke_props_dialog(self, width=600)
 | 
			
		||||
            else:
 | 
			
		||||
                for studio_light in prefs.studio_lights:
 | 
			
		||||
                    if studio_light.name == self.filename + ".sl":
 | 
			
		||||
                    if studio_light.name == filename:
 | 
			
		||||
                        bpy.ops.wm.studiolight_uninstall(index=studio_light.index)
 | 
			
		||||
 | 
			
		||||
        prefs.studio_lights.new(path=finalpath)
 | 
			
		||||
        prefs.studio_lights.new(path=filepath_final)
 | 
			
		||||
 | 
			
		||||
        # print message
 | 
			
		||||
        msg = (
 | 
			
		||||
@@ -2558,21 +2546,18 @@ class WM_OT_studiolight_uninstall(Operator):
 | 
			
		||||
    bl_label = "Uninstall Studio Light"
 | 
			
		||||
    index: bpy.props.IntProperty()
 | 
			
		||||
 | 
			
		||||
    def _remove_path(self, path):
 | 
			
		||||
        if path.exists():
 | 
			
		||||
            path.unlink()
 | 
			
		||||
 | 
			
		||||
    def execute(self, context):
 | 
			
		||||
        import pathlib
 | 
			
		||||
        import os
 | 
			
		||||
        prefs = context.preferences
 | 
			
		||||
        for studio_light in prefs.studio_lights:
 | 
			
		||||
            if studio_light.index == self.index:
 | 
			
		||||
                if studio_light.path:
 | 
			
		||||
                    self._remove_path(pathlib.Path(studio_light.path))
 | 
			
		||||
                if studio_light.path_irr_cache:
 | 
			
		||||
                    self._remove_path(pathlib.Path(studio_light.path_irr_cache))
 | 
			
		||||
                if studio_light.path_sh_cache:
 | 
			
		||||
                    self._remove_path(pathlib.Path(studio_light.path_sh_cache))
 | 
			
		||||
                for filepath in (
 | 
			
		||||
                        studio_light.path,
 | 
			
		||||
                        studio_light.path_irr_cache,
 | 
			
		||||
                        studio_light.path_sh_cache,
 | 
			
		||||
                ):
 | 
			
		||||
                    if filepath and os.path.exists(filepath):
 | 
			
		||||
                        os.unlink(filepath)
 | 
			
		||||
                prefs.studio_lights.remove(studio_light)
 | 
			
		||||
                return {'FINISHED'}
 | 
			
		||||
        return {'CANCELLED'}
 | 
			
		||||
 
 | 
			
		||||
@@ -45,11 +45,9 @@ class USERPREF_HT_header(Header):
 | 
			
		||||
            layout.operator("wm.addon_refresh", icon='FILE_REFRESH')
 | 
			
		||||
            layout.menu("USERPREF_MT_addons_online_resources")
 | 
			
		||||
        elif prefs.active_section == 'LIGHTS':
 | 
			
		||||
            layout.operator("wm.studiolight_install", text="Add MatCap").type = 'MATCAP'
 | 
			
		||||
            layout.operator("wm.studiolight_install", text="Add LookDev HDRI").type = 'WORLD'
 | 
			
		||||
            op = layout.operator("wm.studiolight_install", text="Add Studio Light")
 | 
			
		||||
            op.type = 'STUDIO'
 | 
			
		||||
            op.filter_glob = ".sl"
 | 
			
		||||
            layout.operator("wm.studiolight_install", text="Install MatCap").type = 'MATCAP'
 | 
			
		||||
            layout.operator("wm.studiolight_install", text="Install LookDev HDRI").type = 'WORLD'
 | 
			
		||||
            layout.operator("wm.studiolight_install", text="Install Studio Light").type = 'STUDIO'
 | 
			
		||||
        elif prefs.active_section == 'THEMES':
 | 
			
		||||
            layout.operator("wm.theme_install", icon='FILEBROWSER')
 | 
			
		||||
            layout.operator("ui.reset_default_theme", icon='LOOP_BACK')
 | 
			
		||||
 
 | 
			
		||||
@@ -1391,8 +1391,8 @@ StudioLight *BKE_studiolight_create(const char *path, const SolidLight light[4],
 | 
			
		||||
 | 
			
		||||
	char filename[FILE_MAXFILE];
 | 
			
		||||
	BLI_split_file_part(path, filename, FILE_MAXFILE);
 | 
			
		||||
	BLI_snprintf(sl->path, FILE_MAXFILE, "%s%s", path, ".sl");
 | 
			
		||||
	BLI_snprintf(sl->name, FILE_MAXFILE, "%s%s", filename, ".sl");
 | 
			
		||||
	STRNCPY(sl->path, path);
 | 
			
		||||
	STRNCPY(sl->name, filename);
 | 
			
		||||
 | 
			
		||||
	memcpy(sl->light, light, sizeof(*light) * 4);
 | 
			
		||||
	memcpy(sl->light_ambient, light_ambient, sizeof(*light_ambient) * 3);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user