Skip to content

Commit

Permalink
chore: blender 4.2 blend_method migration warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
saturday06 committed Jul 13, 2024
1 parent f6cdea5 commit b848d57
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/io_scene_vrm/common/ops/vrm.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,18 @@ def model_draw(
)


def show_material_blender_4_2_warning(
execution_context: str = "EXEC_DEFAULT",
/,
*,
material_name_lines: str = "",
) -> set[str]:
return bpy.ops.vrm.show_material_blender_4_2_warning( # type: ignore[attr-defined, no-any-return]
execution_context,
material_name_lines=material_name_lines,
)


def refresh_mtoon1_outline(
execution_context: str = "EXEC_DEFAULT",
/,
Expand Down
41 changes: 38 additions & 3 deletions src/io_scene_vrm/editor/mtoon1/migration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import functools
from typing import Final, Optional

import bpy
from bpy.types import (
Context,
Image,
Expand All @@ -11,7 +12,7 @@
)
from idprop.types import IDPropertyGroup

from ...common import convert, shader
from ...common import convert, ops, shader, version
from ...common.gl import GL_LINEAR, GL_NEAREST
from ...common.logging import get_logger
from ..extension import get_material_extension
Expand All @@ -37,14 +38,45 @@
logger = get_logger(__name__)


def show_material_blender_4_2_warning_delay(material_name_lines: str) -> None:
ops.vrm.show_material_blender_4_2_warning(
"INVOKE_DEFAULT",
material_name_lines=material_name_lines,
)


def migrate(context: Context) -> None:
blender_4_2_migrated_material_names: list[str] = []
for material in context.blend_data.materials:
if not material:
continue
migrate_material(context, material)
migrate_material(context, material, blender_4_2_migrated_material_names)
if (
blender_4_2_migrated_material_names
and tuple(bpy.data.version) < (4, 2)
and bpy.app.version >= (4, 2)
):
logger.warning(
"Migrating Materials from blender version data=%s app=%s",
bpy.data.version,
bpy.app.version,
)

# Blender 4.2.0ではtimerで実行しないとダイアログが自動で消える
bpy.app.timers.register(
functools.partial(
show_material_blender_4_2_warning_delay,
"\n".join(blender_4_2_migrated_material_names),
),
first_interval=0,
)


def migrate_material(context: Context, material: Material) -> None:
def migrate_material(
context: Context,
material: Material,
blender_4_2_migrated_material_names: list[str],
) -> None:
if not material.use_nodes:
return
node_tree = material.node_tree
Expand Down Expand Up @@ -110,6 +142,7 @@ def migrate_material(context: Context, material: Material) -> None:
alpha_mode: Optional[str] = None
alpha_cutoff: Optional[float] = None
if addon_version < (2, 20, 55):
blender_4_2_migrated_material_names.append(material.name)
alpha_cutoff = material.alpha_threshold
blend_method = material.blend_method
if blend_method in ["BLEND", "HASHED"]:
Expand Down Expand Up @@ -371,6 +404,8 @@ def migrate_material(context: Context, material: Material) -> None:
uv_animation_rotation_speed_factor
)

typed_mtoon1.addon_version = version.addon_version()


def backup_texture_info(texture_info: object) -> Optional[TextureInfoBackup]:
if not isinstance(texture_info, IDPropertyGroup):
Expand Down
59 changes: 59 additions & 0 deletions src/io_scene_vrm/editor/mtoon1/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import TYPE_CHECKING, Optional

import bpy
from bpy.app.translations import pgettext
from bpy.props import BoolProperty, EnumProperty, StringProperty
from bpy.types import (
Context,
Expand Down Expand Up @@ -1075,3 +1076,61 @@ def execute(self, context: Context) -> set[str]:
# `poetry run python tools/property_typing.py`
material_name: str # type: ignore[no-redef]
create_modifier: bool # type: ignore[no-redef]


class VRM_OT_show_material_blender_4_2_warning(Operator):
bl_idname = "vrm.show_material_blender_4_2_warning"
bl_label = "Blender 4.2 Material Upgrade Warning"
bl_description = "Show Material Blender 4.2 Warning"
bl_options: AbstractSet[str] = {"REGISTER"}

material_name_lines: StringProperty( # type: ignore[valid-type]
options={"HIDDEN"}
)

def execute(self, _context: Context) -> set[str]:
return {"FINISHED"}

def invoke(self, context: Context, _event: Event) -> set[str]:
return context.window_manager.invoke_props_dialog(self, width=750)

def draw(self, _context: Context) -> None:
column = self.layout.row(align=True).column()
text = pgettext(
'Updating to Blender 4.2 may unintentionally change the "{alpha_mode}"'
+ ' of some MToon materials to "{transparent}".\n'
+ 'This was previously implemented using the material\'s "{blend_mode}"'
+ " but since that setting was removed in Blender 4.2\n"
+ 'In the current VRM add-on, the "{alpha_mode}" function has been'
+ " re-implemented using a different method. However, it\n"
+ "was not possible"
+ " to implement automatic migration of old settings values because those"
+ " values"
+ " could no longer be read.\n"
+ 'Please check the "{alpha_mode}" settings for materials that have'
+ " MToon enabled. "
+ "Alternatively, if you open and save the \ncurrent file using the latest"
+ " version of Blender 3.6 and the VRM add-on, the data for automatic"
+ " migration will be created \ninternally, so that file can be opened in"
+ " Blender 4.2 or later without losing the material settings.\n"
+ "Materials that may be affected are as follows:"
).format(
blend_mode=pgettext("Blend Mode"),
alpha_mode=pgettext("Alpha Mode"),
transparent=pgettext("Transparent"),
)
description_row = column.row()
description_row.emboss = "NONE"
description_column = description_row.box().column(align=True)
for i, line in enumerate(text.splitlines()):
icon = "ERROR" if i == 0 else "NONE"
description_column.label(text=line, translate=False, icon=icon)
material_column = column.box().column(align=True)
for line in self.material_name_lines.splitlines():
material_column.label(text=line, translate=False, icon="MATERIAL")
column.separator()

if TYPE_CHECKING:
# This code is auto generated.
# `poetry run python tools/property_typing.py`
material_name_lines: str # type: ignore[no-redef]
37 changes: 37 additions & 0 deletions src/io_scene_vrm/locale/ja_jp.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,4 +552,41 @@
+ " for humanoid avatars.",
): "アーマチュアは人型では無いリグでエクスポートされます。"
+ "人型アバター用のアニメーションが適用されません。",
(
"Operator",
"Blender 4.2 Material Upgrade Warning",
): "Blender 4.2でのマテリアルアップグレードの警告",
(
"*",
'Updating to Blender 4.2 may unintentionally change the "{alpha_mode}"'
+ ' of some MToon materials to "{transparent}".\n'
+ 'This was previously implemented using the material\'s "{blend_mode}"'
+ " but since that setting was removed in Blender 4.2\n"
+ 'In the current VRM add-on, the "{alpha_mode}" function has been'
+ " re-implemented using a different method. However, it\n"
+ "was not possible"
+ " to implement automatic migration of old settings values because those"
+ " values"
+ " could no longer be read.\n"
+ 'Please check the "{alpha_mode}" settings for materials that have'
+ " MToon enabled. "
+ "Alternatively, if you open and save the \ncurrent file using the latest"
+ " version of Blender 3.6 and the VRM add-on, the data for automatic"
+ " migration will be created \ninternally, so that file can be opened in"
+ " Blender 4.2 or later without losing the material settings.\n"
+ "Materials that may be affected are as follows:",
): "Blender 4.2へのアップデートに伴い、一部のMToonマテリアルの"
+ "「{alpha_mode}」設定が意図せず「{transparent}」に変化している\n"
+ "可能性があります。当該機能は今までマテリアルの「{blend_mode}」設定で"
+ "実装されていましたが、Blender 4.2からその設定が\n"
+ "削除されたためです。"
+ "現在のVRMアドオンでは「{alpha_mode}」の機能は別の方式で再実装"
+ "されましたが、古い設定値の自動移行は、\n"
+ "古い設定値自体がもう読めないため実装が不可能でした。"
+ "MToonが有効なマテリアルの「{alpha_mode}」設定の確認をお願いします。\n"
+ "あるいは、Blender 3.6とVRMアドオンの最新版で現在のファイルを"
+ "一度開いて保存すると、内部的に自動移行用のデータが作成\n"
+ "されるため、そのファイルはマテリアルの設定を失わずに"
+ "Blender 4.2以降でも開けるようになります。\n"
+ "影響のある可能性のあるマテリアルは次の通りです。",
}
1 change: 1 addition & 0 deletions src/io_scene_vrm/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ def save_pre(_unused: object) -> None:
mtoon1_ops.VRM_OT_reset_mtoon1_material_shader_node_tree,
mtoon1_ops.VRM_OT_import_mtoon1_texture_image_file,
mtoon1_ops.VRM_OT_refresh_mtoon1_outline,
mtoon1_ops.VRM_OT_show_material_blender_4_2_warning,
detail_mesh_maker.ICYP_OT_detail_mesh_maker,
glsl_drawer.ICYP_OT_draw_model,
glsl_drawer.ICYP_OT_remove_draw_model,
Expand Down

0 comments on commit b848d57

Please sign in to comment.