Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

309 lines
9.9 KiB
Python
Raw Permalink Normal View History

# SPDX-FileCopyrightText: 2015-2023 Blender Authors
#
# SPDX-License-Identifier: GPL-2.0-or-later
import bpy
from bpy.types import (
Operator,
OperatorFileListElement,
)
from bpy.props import (
BoolProperty,
CollectionProperty,
StringProperty,
)
from bpy.app.translations import pgettext_rpt as rpt_
# ########## Datablock previews... ##########
2015-09-01 03:51:50 +10:00
class WM_OT_previews_batch_generate(Operator):
"""Generate selected .blend file's previews"""
bl_idname = "wm.previews_batch_generate"
bl_label = "Batch-Generate Previews"
bl_options = {'REGISTER'}
# -----------
# File props.
files: CollectionProperty(
type=OperatorFileListElement,
2018-06-26 19:41:37 +02:00
options={'HIDDEN', 'SKIP_SAVE'},
name="",
description="Collection of file paths with common ``directory`` root",
2018-06-26 19:41:37 +02:00
)
directory: StringProperty(
2018-06-26 19:41:37 +02:00
maxlen=1024,
subtype='FILE_PATH',
options={'HIDDEN', 'SKIP_SAVE'},
name="",
description="Root path of all files listed in ``files`` collection",
2018-06-26 19:41:37 +02:00
)
# Show only images/videos, and directories!
filter_blender: BoolProperty(
2018-06-26 19:41:37 +02:00
default=True,
options={'HIDDEN', 'SKIP_SAVE'},
name="",
description="Show Blender files in the File Browser",
2018-06-26 19:41:37 +02:00
)
filter_folder: BoolProperty(
2018-06-26 19:41:37 +02:00
default=True,
options={'HIDDEN', 'SKIP_SAVE'},
name="",
description="Show folders in the File Browser",
2018-06-26 19:41:37 +02:00
)
# -----------
# Own props.
use_scenes: BoolProperty(
2018-06-26 19:41:37 +02:00
default=True,
name="Scenes",
description="Generate scenes' previews",
)
use_collections: BoolProperty(
2018-06-26 19:41:37 +02:00
default=True,
2018-06-26 22:56:39 +02:00
name="Collections",
description="Generate collections' previews",
2018-06-26 19:41:37 +02:00
)
use_objects: BoolProperty(
2018-06-26 19:41:37 +02:00
default=True,
name="Objects",
description="Generate objects' previews",
)
use_intern_data: BoolProperty(
2018-06-26 19:41:37 +02:00
default=True,
name="Materials & Textures",
2018-06-26 19:41:37 +02:00
description="Generate 'internal' previews (materials, textures, images, etc.)",
)
use_trusted: BoolProperty(
2018-06-26 19:41:37 +02:00
default=False,
name="Trusted Blend Files",
description="Enable Python evaluation for selected files",
2018-06-26 19:41:37 +02:00
)
use_backups: BoolProperty(
2018-06-26 19:41:37 +02:00
default=True,
name="Save Backups",
description="Keep a backup (.blend1) version of the files when saving with generated previews",
)
def invoke(self, context, _event):
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def execute(self, context):
import os
import subprocess
from bl_previews_utils import bl_previews_render as preview_render
context.window_manager.progress_begin(0, len(self.files))
context.window_manager.progress_update(0)
for i, fn in enumerate(self.files):
blen_path = os.path.join(self.directory, fn.name)
cmd = [
bpy.app.binary_path,
"--background",
"--factory-startup",
]
if self.use_trusted:
cmd.append("--enable-autoexec")
cmd.extend([
blen_path,
"--python",
os.path.join(os.path.dirname(preview_render.__file__), "bl_previews_render.py"),
"--",
])
if not self.use_scenes:
2024-03-14 10:38:16 +11:00
cmd.append("--no_scenes")
Collections and groups unification OVERVIEW * In 2.7 terminology, all layers and groups are now collection datablocks. * These collections are nestable, linkable, instanceable, overrideable, .. which opens up new ways to set up scenes and link + override data. * Viewport/render visibility and selectability are now a part of the collection and shared across all view layers and linkable. * View layers define which subset of the scene collection hierarchy is excluded for each. For many workflows one view layer can be used, these are more of an advanced feature now. OUTLINER * The outliner now has a "View Layer" display mode instead of "Collections", which can display the collections and/or objects in the view layer. * In this display mode, collections can be excluded with the right click menu. These will then be greyed out and their objects will be excluded. * To view collections not linked to any scene, the "Blender File" display mode can be used, with the new filtering option to just see Colleciton datablocks. * The outliner right click menus for collections and objects were reorganized. * Drag and drop still needs to be improved. Like before, dragging the icon or text gives different results, we'll unify this later. LINKING AND OVERRIDES * Collections can now be linked into the scene without creating an instance, with the link/append operator or from the collections view in the outliner. * Collections can get static overrides with the right click menu in the outliner, but this is rather unreliable and not clearly communicated at the moment. * We still need to improve the make override operator to turn collection instances into collections with overrides directly in the scene. PERFORMANCE * We tried to make performance not worse than before and improve it in some cases. The main thing that's still a bit slower is multiple scenes, we have to change the layer syncing to only updated affected scenes. * Collections keep a list of their parent collections for faster incremental updates in syncing and caching. * View layer bases are now in a object -> base hash to avoid quadratic time lookups internally and in API functions like visible_get(). VERSIONING * Compatibility with 2.7 files should be improved due to the new visibility controls. Of course users may not want to set up their scenes differently now to avoid having separate layers and groups. * Compatibility with 2.8 is mostly there, and was tested on Eevee demo and Hero files. There's a few things which are know to be not quite compatible, like nested layer collections inside groups. * The versioning code for 2.8 files is quite complicated, and isolated behind #ifdef so it can be removed at the end of the release cycle. KNOWN ISSUES * The G-key group operators in the 3D viewport were left mostly as is, they need to be modified still to fit better. * Same for the groups panel in the object properties. This needs to be updated still, or perhaps replaced by something better. * Collections must all have a unique name. Less restrictive namespacing is to be done later, we'll have to see how important this is as all objects within the collections must also have a unique name anyway. * Full scene copy and delete scene are exactly doing the right thing yet. Differential Revision: https://developer.blender.org/D3383 https://code.blender.org/2018/05/collections-and-groups/
2018-04-30 15:57:22 +02:00
if not self.use_collections:
2024-03-14 10:38:16 +11:00
cmd.append("--no_collections")
if not self.use_objects:
2024-03-14 10:38:16 +11:00
cmd.append("--no_objects")
if not self.use_intern_data:
2024-03-14 10:38:16 +11:00
cmd.append("--no_data_intern")
if not self.use_backups:
cmd.append("--no_backups")
if subprocess.call(cmd):
self.report({'ERROR'}, rpt_("Previews generation process failed for file '{:s}'!").format(blen_path))
context.window_manager.progress_end()
return {'CANCELLED'}
context.window_manager.progress_update(i + 1)
context.window_manager.progress_end()
return {'FINISHED'}
class WM_OT_previews_batch_clear(Operator):
"""Clear selected .blend file's previews"""
bl_idname = "wm.previews_batch_clear"
bl_label = "Batch-Clear Previews"
bl_options = {'REGISTER'}
# -----------
# File props.
files: CollectionProperty(
type=OperatorFileListElement,
2018-06-26 19:41:37 +02:00
options={'HIDDEN', 'SKIP_SAVE'},
)
directory: StringProperty(
2018-06-26 19:41:37 +02:00
maxlen=1024,
subtype='FILE_PATH',
options={'HIDDEN', 'SKIP_SAVE'},
)
# Show only images/videos, and directories!
filter_blender: BoolProperty(
2018-06-26 19:41:37 +02:00
default=True,
options={'HIDDEN', 'SKIP_SAVE'},
)
filter_folder: BoolProperty(
2018-06-26 19:41:37 +02:00
default=True,
options={'HIDDEN', 'SKIP_SAVE'},
)
# -----------
# Own props.
use_scenes: BoolProperty(
2018-06-26 19:41:37 +02:00
default=True,
name="Scenes",
description="Clear scenes' previews",
)
use_collections: BoolProperty(
2018-06-26 22:56:39 +02:00
default=True,
name="Collections",
description="Clear collections' previews",
)
use_objects: BoolProperty(
2018-06-26 19:41:37 +02:00
default=True,
name="Objects",
description="Clear objects' previews",
)
use_intern_data: BoolProperty(
2018-06-26 19:41:37 +02:00
default=True,
name="Materials & Textures",
2018-06-26 19:41:37 +02:00
description="Clear 'internal' previews (materials, textures, images, etc.)",
)
use_trusted: BoolProperty(
2018-06-26 19:41:37 +02:00
default=False,
name="Trusted Blend Files",
description="Enable Python evaluation for selected files",
2018-06-26 19:41:37 +02:00
)
use_backups: BoolProperty(
2018-06-26 19:41:37 +02:00
default=True,
name="Save Backups",
description="Keep a backup (.blend1) version of the files when saving with cleared previews",
)
def invoke(self, context, _event):
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def execute(self, context):
import os
import subprocess
from bl_previews_utils import bl_previews_render as preview_render
context.window_manager.progress_begin(0, len(self.files))
context.window_manager.progress_update(0)
for i, fn in enumerate(self.files):
blen_path = os.path.join(self.directory, fn.name)
cmd = [
bpy.app.binary_path,
"--background",
"--factory-startup",
]
if self.use_trusted:
cmd.append("--enable-autoexec")
cmd.extend([
blen_path,
"--python",
os.path.join(os.path.dirname(preview_render.__file__), "bl_previews_render.py"),
"--",
"--clear",
])
if not self.use_scenes:
2024-03-14 10:38:16 +11:00
cmd.append("--no_scenes")
Collections and groups unification OVERVIEW * In 2.7 terminology, all layers and groups are now collection datablocks. * These collections are nestable, linkable, instanceable, overrideable, .. which opens up new ways to set up scenes and link + override data. * Viewport/render visibility and selectability are now a part of the collection and shared across all view layers and linkable. * View layers define which subset of the scene collection hierarchy is excluded for each. For many workflows one view layer can be used, these are more of an advanced feature now. OUTLINER * The outliner now has a "View Layer" display mode instead of "Collections", which can display the collections and/or objects in the view layer. * In this display mode, collections can be excluded with the right click menu. These will then be greyed out and their objects will be excluded. * To view collections not linked to any scene, the "Blender File" display mode can be used, with the new filtering option to just see Colleciton datablocks. * The outliner right click menus for collections and objects were reorganized. * Drag and drop still needs to be improved. Like before, dragging the icon or text gives different results, we'll unify this later. LINKING AND OVERRIDES * Collections can now be linked into the scene without creating an instance, with the link/append operator or from the collections view in the outliner. * Collections can get static overrides with the right click menu in the outliner, but this is rather unreliable and not clearly communicated at the moment. * We still need to improve the make override operator to turn collection instances into collections with overrides directly in the scene. PERFORMANCE * We tried to make performance not worse than before and improve it in some cases. The main thing that's still a bit slower is multiple scenes, we have to change the layer syncing to only updated affected scenes. * Collections keep a list of their parent collections for faster incremental updates in syncing and caching. * View layer bases are now in a object -> base hash to avoid quadratic time lookups internally and in API functions like visible_get(). VERSIONING * Compatibility with 2.7 files should be improved due to the new visibility controls. Of course users may not want to set up their scenes differently now to avoid having separate layers and groups. * Compatibility with 2.8 is mostly there, and was tested on Eevee demo and Hero files. There's a few things which are know to be not quite compatible, like nested layer collections inside groups. * The versioning code for 2.8 files is quite complicated, and isolated behind #ifdef so it can be removed at the end of the release cycle. KNOWN ISSUES * The G-key group operators in the 3D viewport were left mostly as is, they need to be modified still to fit better. * Same for the groups panel in the object properties. This needs to be updated still, or perhaps replaced by something better. * Collections must all have a unique name. Less restrictive namespacing is to be done later, we'll have to see how important this is as all objects within the collections must also have a unique name anyway. * Full scene copy and delete scene are exactly doing the right thing yet. Differential Revision: https://developer.blender.org/D3383 https://code.blender.org/2018/05/collections-and-groups/
2018-04-30 15:57:22 +02:00
if not self.use_collections:
2024-03-14 10:38:16 +11:00
cmd.append("--no_collections")
if not self.use_objects:
2024-03-14 10:38:16 +11:00
cmd.append("--no_objects")
if not self.use_intern_data:
2024-03-14 10:38:16 +11:00
cmd.append("--no_data_intern")
if not self.use_backups:
cmd.append("--no_backups")
if subprocess.call(cmd):
self.report({'ERROR'}, rpt_("Previews clear process failed for file '{:s}'!").format(blen_path))
context.window_manager.progress_end()
return {'CANCELLED'}
context.window_manager.progress_update(i + 1)
context.window_manager.progress_end()
return {'FINISHED'}
class WM_OT_blend_strings_utf8_validate(Operator):
2019-01-30 09:03:37 +11:00
"""Check and fix all strings in current .blend file to be valid UTF-8 Unicode """ \
"""(needed for some old, 2.4x area files)"""
bl_idname = "wm.blend_strings_utf8_validate"
bl_label = "Validate .blend strings"
bl_options = {'REGISTER'}
def validate_strings(self, item, done_items):
if item is None:
return False
if item in done_items:
return False
done_items.add(item)
2018-12-18 15:01:03 +11:00
if getattr(item, "library", None) is not None:
return False # No point in checking library data, we cannot fix it anyway...
changed = False
for prop in item.bl_rna.properties:
2018-12-18 15:01:03 +11:00
if prop.identifier in {"bl_rna", "rna_type"}:
continue # Or we'd recurse 'till Hell freezes.
if prop.is_readonly:
continue
if prop.type == 'STRING':
val_bytes = item.path_resolve(prop.identifier, False).as_bytes()
val_utf8 = val_bytes.decode("utf-8", "replace")
val_bytes_valid = val_utf8.encode("utf-8")
if val_bytes_valid != val_bytes:
print("found bad utf8 encoded string {!r}, fixing to {!r} ({!r})...".format(
val_bytes, val_bytes_valid, val_utf8,
))
setattr(item, prop.identifier, val_utf8)
changed = True
elif prop.type == 'POINTER':
it = getattr(item, prop.identifier)
changed |= self.validate_strings(it, done_items)
elif prop.type == 'COLLECTION':
for it in getattr(item, prop.identifier):
changed |= self.validate_strings(it, done_items)
return changed
def execute(self, _context):
changed = False
done_items = set()
for prop in bpy.data.bl_rna.properties:
if prop.type == 'COLLECTION':
for it in getattr(bpy.data, prop.identifier):
changed |= self.validate_strings(it, done_items)
if changed:
self.report(
{'WARNING'},
"Some strings were fixed, don't forget to save the .blend file to keep those changes",
)
return {'FINISHED'}
classes = (
WM_OT_previews_batch_clear,
WM_OT_previews_batch_generate,
WM_OT_blend_strings_utf8_validate,
)