Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
saturday06 committed Jun 18, 2024
1 parent 1308f13 commit ca3af69
Show file tree
Hide file tree
Showing 47 changed files with 699 additions and 501 deletions.
6 changes: 4 additions & 2 deletions src/io_scene_vrm/common/shader.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ def load_mtoon1_node_group(
*,
reset_node_groups: bool,
) -> None:
from ..editor.extension import get_node_tree_extension

start_time = time.perf_counter()

if not blend_file_path.exists():
Expand All @@ -246,7 +248,7 @@ def load_mtoon1_node_group(
checking_node_group = context.blend_data.node_groups.get(node_group_name)
if checking_node_group:
if (
tuple(checking_node_group.vrm_addon_extension.addon_version)
tuple(get_node_tree_extension(checking_node_group).addon_version)
>= LAST_MODIFIED_VERSION
):
return
Expand Down Expand Up @@ -299,7 +301,7 @@ def load_mtoon1_node_group(
)
clear_node_tree(node_group, clear_inputs_outputs=True)
copy_node_tree(context, template_node_group, node_group)
node_group.vrm_addon_extension.addon_version = addon_version()
get_node_tree_extension(node_group).addon_version = addon_version()
finally:
if template_node_group and template_node_group.users <= 1:
context.blend_data.node_groups.remove(template_node_group)
Expand Down
61 changes: 59 additions & 2 deletions src/io_scene_vrm/editor/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@
PointerProperty,
StringProperty,
)
from bpy.types import Context, PropertyGroup
from bpy.types import (
Armature,
Bone,
Context,
Material,
NodeTree,
Object,
PropertyGroup,
Scene,
)
from mathutils import Matrix, Quaternion

from ..common.logging import get_logger
Expand Down Expand Up @@ -42,7 +51,7 @@ def update_vrm0_material_property_names(context: Context, scene_name: str) -> No
if not scene:
logger.error(f'No scene "{scene_name}"')
return
ext = scene.vrm_addon_extension
ext = get_scene_extension(scene)

# Unity 2022.3.4 + UniVRM 0.112.0
gltf_property_names = [
Expand Down Expand Up @@ -428,3 +437,51 @@ class VrmAddonNodeTreeExtensionPropertyGroup(PropertyGroup):
# This code is auto generated.
# `poetry run python tools/property_typing.py`
addon_version: Sequence[int] # type: ignore[no-redef]


def get_material_extension(
material: Material,
) -> VrmAddonMaterialExtensionPropertyGroup:
extension = getattr(material, "vrm_addon_extension", None)
if not isinstance(extension, VrmAddonMaterialExtensionPropertyGroup):
raise TypeError
return extension


def get_armature_extension(
armature: Armature,
) -> VrmAddonArmatureExtensionPropertyGroup:
extension = getattr(armature, "vrm_addon_extension", None)
if not isinstance(extension, VrmAddonArmatureExtensionPropertyGroup):
raise TypeError
return extension


def get_node_tree_extension(
node_tree: NodeTree,
) -> VrmAddonNodeTreeExtensionPropertyGroup:
extension = getattr(node_tree, "vrm_addon_extension", None)
if not isinstance(extension, VrmAddonNodeTreeExtensionPropertyGroup):
raise TypeError
return extension


def get_scene_extension(scene: Scene) -> VrmAddonSceneExtensionPropertyGroup:
extension = getattr(scene, "vrm_addon_extension", None)
if not isinstance(extension, VrmAddonSceneExtensionPropertyGroup):
raise TypeError
return extension


def get_bone_extension(bone: Bone) -> VrmAddonBoneExtensionPropertyGroup:
extension = getattr(bone, "vrm_addon_extension", None)
if not isinstance(extension, VrmAddonBoneExtensionPropertyGroup):
raise TypeError
return extension


def get_object_extension(obj: Object) -> VrmAddonObjectExtensionPropertyGroup:
extension = getattr(obj, "vrm_addon_extension", None)
if not isinstance(extension, VrmAddonObjectExtensionPropertyGroup):
raise TypeError
return extension
23 changes: 13 additions & 10 deletions src/io_scene_vrm/editor/make_armature.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from ..common.vrm0.human_bone import HumanBoneSpecifications
from ..common.workspace import save_workspace
from . import migration
from .extension import get_armature_extension
from .vrm0.property_group import (
Vrm0BlendShapeGroupPropertyGroup,
Vrm0HumanoidPropertyGroup,
Expand Down Expand Up @@ -177,7 +178,7 @@ def make_armature(self, context: Context) -> tuple[Object, dict[str, str]]:
if not isinstance(armature_data, Armature):
message = "armature data is not an Armature"
raise TypeError(message)
armature_data.vrm_addon_extension.addon_version = addon_version()
get_armature_extension(armature_data).addon_version = addon_version()

bone_dict: dict[str, EditBone] = {}

Expand Down Expand Up @@ -577,9 +578,9 @@ def setup_as_vrm(
armature_data = armature.data
if isinstance(armature_data, Armature) and not self.skip_heavy_armature_setup:
for vrm_bone_name, bpy_bone_name in compare_dict.items():
for (
human_bone
) in armature_data.vrm_addon_extension.vrm0.humanoid.human_bones:
for human_bone in get_armature_extension(
armature_data
).vrm0.humanoid.human_bones:
if human_bone.bone == vrm_bone_name:
human_bone.node.set_bone_name(bpy_bone_name)
break
Expand All @@ -599,8 +600,8 @@ def make_extension_setting_and_metas(
armature_data = armature.data
if not isinstance(armature_data, Armature):
return
vrm0 = armature_data.vrm_addon_extension.vrm0
vrm1 = armature_data.vrm_addon_extension.vrm1
vrm0 = get_armature_extension(armature_data).vrm0
vrm1 = get_armature_extension(armature_data).vrm1
vrm0.first_person.first_person_bone.set_bone_name("head")
vrm0.first_person.first_person_bone_offset = (0, 0, 0.06)
vrm1.look_at.offset_from_head_bone = offset_from_head_bone
Expand Down Expand Up @@ -706,13 +707,15 @@ def get_humanoid_bone(self, bone: str) -> Bone:
tmp_dict = {
v.bone: i
for i, v in enumerate(
armature_data.vrm_addon_extension.vrm0.humanoid.human_bones
get_armature_extension(armature_data).vrm0.humanoid.human_bones
)
}

bone_name: str = armature_data.vrm_addon_extension.vrm0.humanoid.human_bones[
tmp_dict[bone]
].node.bone_name
bone_name: str = (
get_armature_extension(armature_data)
.vrm0.humanoid.human_bones[tmp_dict[bone]]
.node.bone_name
)
humanoid_bone = armature_data.bones[bone_name]
return humanoid_bone

Expand Down
5 changes: 3 additions & 2 deletions src/io_scene_vrm/editor/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from .extension import (
VrmAddonArmatureExtensionPropertyGroup,
VrmAddonSceneExtensionPropertyGroup,
get_armature_extension,
)
from .mtoon1 import migration as mtoon1_migration
from .mtoon1 import ops as mtoon1_ops
Expand All @@ -25,7 +26,7 @@


def is_unnecessary(armature_data: Armature) -> bool:
ext = armature_data.vrm_addon_extension
ext = get_armature_extension(armature_data)
return (
tuple(ext.addon_version) >= addon_version()
and armature_data.name == ext.armature_data_name
Expand Down Expand Up @@ -73,7 +74,7 @@ def migrate(context: Optional[Context], armature_object_name: str) -> bool:
if is_unnecessary(armature_data):
return True

ext = armature_data.vrm_addon_extension
ext = get_armature_extension(armature_data)
ext.armature_data_name = armature_data.name

for bone_property_group in BonePropertyGroup.get_all_bone_property_groups(armature):
Expand Down
5 changes: 3 additions & 2 deletions src/io_scene_vrm/editor/mtoon1/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from bpy.types import Mesh

from ...common.logging import get_logger
from ..extension import get_material_extension
from .ops import VRM_OT_refresh_mtoon1_outline

logger = get_logger(__name__)
Expand All @@ -25,9 +26,9 @@ def update_mtoon1_outline() -> Optional[float]:
[
(
material_slot.material.name,
material_slot.material.vrm_addon_extension.mtoon1.get_enabled_in_material(
get_material_extension(
material_slot.material
),
).mtoon1.get_enabled_in_material(material_slot.material),
has_auto_smooth and obj.data.use_auto_smooth,
)
if material_slot.material
Expand Down
3 changes: 2 additions & 1 deletion src/io_scene_vrm/editor/mtoon1/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from ...common import convert, native, shader
from ...common.gl import GL_LINEAR, GL_NEAREST
from ...common.logging import get_logger
from ..extension import get_material_extension
from .property_group import (
GL_LINEAR_IMAGE_INTERPOLATIONS,
IMAGE_INTERPOLATION_CLOSEST,
Expand Down Expand Up @@ -113,7 +114,7 @@ def migrate_material(context: Context, material: Material) -> None:
)

# ここから先は、シェーダーノードが最新の状態になっている想定のコードを書ける
typed_mtoon1 = material.vrm_addon_extension.mtoon1
typed_mtoon1 = get_material_extension(material).mtoon1
if alpha_mode is not None:
typed_mtoon1.alpha_mode = alpha_mode
if alpha_cutoff is not None:
Expand Down
49 changes: 25 additions & 24 deletions src/io_scene_vrm/editor/mtoon1/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from ...common import convert, shader
from ...common.logging import get_logger
from .. import search
from ..extension import get_material_extension
from .property_group import (
Mtoon0ReceiveShadowTexturePropertyGroup,
Mtoon0ShadingGradeTexturePropertyGroup,
Expand Down Expand Up @@ -152,7 +153,7 @@ def convert_material_to_mtoon1(self, context: Context, material: Material) -> No
reset_node_groups=False,
)

gltf = material.vrm_addon_extension.mtoon1
gltf = get_material_extension(material).mtoon1
gltf.pbr_metallic_roughness.base_color_factor = base_color_factor
if base_color_texture_image:
gltf.pbr_metallic_roughness.base_color_texture.index.source = (
Expand Down Expand Up @@ -356,7 +357,7 @@ def convert_mtoon_unversioned_to_mtoon1(

shader.load_mtoon1_shader(context, material, reset_node_groups=True)

gltf = material.vrm_addon_extension.mtoon1
gltf = get_material_extension(material).mtoon1
mtoon = gltf.extensions.vrmc_materials_mtoon

gltf.alpha_mode = alpha_mode
Expand Down Expand Up @@ -513,7 +514,7 @@ def convert_mtoon1_to_bsdf_principled(self, material: Material) -> None:
return

principled_bsdf = PrincipledBSDFWrapper(material, is_readonly=False)
gltf = material.vrm_addon_extension.mtoon1
gltf = get_material_extension(material).mtoon1

principled_bsdf.base_color = gltf.pbr_metallic_roughness.base_color_factor[:3]
base_color_texture_image = (
Expand Down Expand Up @@ -653,7 +654,7 @@ def execute(self, context: Context) -> set[str]:
if not isinstance(material, Material):
return {"FINISHED"}

gltf = material.vrm_addon_extension.mtoon1
gltf = get_material_extension(material).mtoon1
mtoon = gltf.extensions.vrmc_materials_mtoon

for texture in [
Expand Down Expand Up @@ -771,7 +772,7 @@ def assign(
)
if not node_group:
return
mtoon = material.vrm_addon_extension.mtoon1.extensions.vrmc_materials_mtoon
mtoon = get_material_extension(material).mtoon1.extensions.vrmc_materials_mtoon
outline_width_mode_value = next(
(
value
Expand Down Expand Up @@ -831,7 +832,7 @@ def assign(
outline_material_name = f"MToon Outline ({material.name})"
modifier_name = f"MToon Outline ({material.name})"

outline_material = material.vrm_addon_extension.mtoon1.outline_material
outline_material = get_material_extension(material).mtoon1.outline_material
reset_outline_material = not outline_material
if reset_outline_material:
outline_material = context.blend_data.materials.new(
Expand All @@ -846,28 +847,28 @@ def assign(
shader.load_mtoon1_shader(
context, outline_material, reset_node_groups=False
)
outline_material.vrm_addon_extension.mtoon1.is_outline_material = True
material.vrm_addon_extension.mtoon1.outline_material = outline_material
get_material_extension(outline_material).mtoon1.is_outline_material = True
get_material_extension(material).mtoon1.outline_material = outline_material
if outline_material.name != outline_material_name:
outline_material.name = outline_material_name
if not outline_material.use_nodes:
outline_material.use_nodes = True
if not outline_material.vrm_addon_extension.mtoon1.is_outline_material:
outline_material.vrm_addon_extension.mtoon1.is_outline_material = True
if not get_material_extension(outline_material).mtoon1.is_outline_material:
get_material_extension(outline_material).mtoon1.is_outline_material = True
if (
outline_material.vrm_addon_extension.mtoon1.alpha_cutoff
!= material.vrm_addon_extension.mtoon1.alpha_cutoff
get_material_extension(outline_material).mtoon1.alpha_cutoff
!= get_material_extension(material).mtoon1.alpha_cutoff
):
outline_material.vrm_addon_extension.mtoon1.alpha_cutoff = (
material.vrm_addon_extension.mtoon1.alpha_cutoff
)
get_material_extension(
outline_material
).mtoon1.alpha_cutoff = get_material_extension(material).mtoon1.alpha_cutoff
if (
outline_material.vrm_addon_extension.mtoon1.alpha_mode
!= material.vrm_addon_extension.mtoon1.alpha_mode
get_material_extension(outline_material).mtoon1.alpha_mode
!= get_material_extension(material).mtoon1.alpha_mode
):
outline_material.vrm_addon_extension.mtoon1.alpha_mode = (
material.vrm_addon_extension.mtoon1.alpha_mode
)
get_material_extension(
outline_material
).mtoon1.alpha_mode = get_material_extension(material).mtoon1.alpha_mode
if outline_material.shadow_method != "NONE":
outline_material.shadow_method = "NONE"
if not outline_material.use_backface_culling:
Expand Down Expand Up @@ -995,9 +996,9 @@ def refresh_object(context: Context, obj: Object) -> None:
material = context.blend_data.materials.get(material_slot.material.name)
if not material:
continue
if not material.vrm_addon_extension.mtoon1.enabled:
if not get_material_extension(material).mtoon1.enabled:
continue
if material.vrm_addon_extension.mtoon1.is_outline_material:
if get_material_extension(material).mtoon1.is_outline_material:
continue

VRM_OT_refresh_mtoon1_outline.assign(
Expand Down Expand Up @@ -1028,9 +1029,9 @@ def refresh(
material = context.blend_data.materials.get(material_slot.material.name)
if not material:
continue
if not material.vrm_addon_extension.mtoon1.enabled:
if not get_material_extension(material).mtoon1.enabled:
continue
if material.vrm_addon_extension.mtoon1.is_outline_material:
if get_material_extension(material).mtoon1.is_outline_material:
continue

VRM_OT_refresh_mtoon1_outline.assign(
Expand Down
5 changes: 3 additions & 2 deletions src/io_scene_vrm/editor/mtoon1/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from ...common.logging import get_logger
from .. import search
from ..extension import get_material_extension
from ..ops import VRM_OT_open_url_in_web_browser, layout_operator
from .ops import (
VRM_OT_import_mtoon1_texture_image_file,
Expand Down Expand Up @@ -162,7 +163,7 @@ def draw_mtoon1_material(context: Context, layout: UILayout) -> None:
material = context.material
if not material:
return
ext = material.vrm_addon_extension
ext = get_material_extension(material)
layout = layout.column()

layout.prop(ext.mtoon1, "enabled")
Expand Down Expand Up @@ -368,7 +369,7 @@ def draw_material(context: Context, layout: UILayout) -> None:
material = context.material
if not material:
return
ext = material.vrm_addon_extension
ext = get_material_extension(material)
if ext.mtoon1.is_outline_material:
layout.box().label(icon="INFO", text="This is a MToon Outline material")
return
Expand Down
Loading

0 comments on commit ca3af69

Please sign in to comment.