Skip to content

Commit

Permalink
Refactor property callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
UuuNyaa committed Dec 19, 2023
1 parent 1eea6ab commit 704f905
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 109 deletions.
4 changes: 2 additions & 2 deletions mmd_tools/core/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import logging
import os
from typing import Optional, Tuple
from typing import Optional, Tuple, cast

import bpy

Expand Down Expand Up @@ -80,7 +80,7 @@ def swap_materials(mesh_object: bpy.types.Object, mat1_ref: str | int, mat2_ref:
Raises:
MaterialNotFoundError: If one of the materials is not found
"""
mesh: bpy.types.Mesh = mesh_object.data
mesh = cast(bpy.types.Mesh, mesh_object.data)
try:
# Try to find the materials
mat1 = mesh.materials[mat1_ref]
Expand Down
26 changes: 25 additions & 1 deletion mmd_tools/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import itertools
import logging
import time
from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, Iterator, List, Optional, Set, TypeGuard, Union
from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, Iterator, List, Optional, Set, TypeGuard, Union, cast

import bpy
import idprop
Expand Down Expand Up @@ -77,6 +77,17 @@ def find_bone_order_mesh_object(root_object: bpy.types.Object) -> Optional[bpy.t
# TODO consistency issue
return next(filter(lambda o: o.type == "MESH" and "mmd_bone_order_override" in o.modifiers, armature_object.children), None)

@staticmethod
def find_mesh_by_name(root_object: bpy.types.Object, name: str) -> Optional[bpy.types.Object]:
armature_object = FnModel.find_armature(root_object)
if armature_object is None:
return None
for o in FnModel.child_meshes(armature_object):
if o.name != name:
continue
return o
return None

@staticmethod
def all_children(obj: bpy.types.Object) -> Iterator[bpy.types.Object]:
child: bpy.types.Object
Expand Down Expand Up @@ -124,6 +135,19 @@ def iterate_temporary_objects(root_object: bpy.types.Object, rigid_track_only: b
return rigid_body_objects
return itertools.chain(rigid_body_objects, FnModel.filtered_children(FnModel.is_temporary_object, temporary_group_object))

@staticmethod
def iterate_materials(root_object: bpy.types.Object) -> Iterable[bpy.types.Material]:
armature_object = FnModel.find_armature(root_object)
if armature_object is None:
return []
return (material for mesh_object in FnModel.child_meshes(armature_object) for material in cast(bpy.types.Mesh, mesh_object.data).materials if material is not None)

@staticmethod
def iterate_unique_materials(root_object: bpy.types.Object) -> Iterable[bpy.types.Material]:
materials: Dict[bpy.types.Material, None] = {} # use dict because set does not guarantee the order
materials.update((material, None) for material in FnModel.iterate_materials(root_object))
return materials.keys()

@staticmethod
def is_root_object(obj: Optional[bpy.types.Object]) -> TypeGuard[bpy.types.Object]:
return obj is not None and obj.mmd_type == "ROOT"
Expand Down
2 changes: 1 addition & 1 deletion mmd_tools/core/pmx/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ def __translateBoneNames(self):
def __fixRepeatedMorphName(self):
used_names = set()
for m in self.__model.morphs:
m.name = utils.uniqueName(m.name or "Morph", used_names)
m.name = utils.unique_name(m.name or "Morph", used_names)
used_names.add(m.name)

def execute(self, **args):
Expand Down
82 changes: 40 additions & 42 deletions mmd_tools/properties/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,80 +7,78 @@
from mmd_tools import utils
from mmd_tools.core import material
from mmd_tools.core.material import FnMaterial
from mmd_tools.core.model import Model
from mmd_tools.core.model import FnModel
from mmd_tools.properties import patch_library_overridable


def _updateAmbientColor(prop, context):
def _mmd_material_update_ambient_color(prop: "MMDMaterial", _context):
FnMaterial(prop.id_data).update_ambient_color()


def _updateDiffuseColor(prop, context):
def _mmd_material_update_diffuse_color(prop: "MMDMaterial", _context):
FnMaterial(prop.id_data).update_diffuse_color()


def _updateAlpha(prop, context):
def _mmd_material_update_alpha(prop: "MMDMaterial", _context):
FnMaterial(prop.id_data).update_alpha()


def _updateSpecularColor(prop, context):
def _mmd_material_update_specular_color(prop: "MMDMaterial", _context):
FnMaterial(prop.id_data).update_specular_color()


def _updateShininess(prop, context):
def _mmd_material_update_shininess(prop: "MMDMaterial", _context):
FnMaterial(prop.id_data).update_shininess()


def _updateIsDoubleSided(prop, context):
def _mmd_material_update_is_double_sided(prop: "MMDMaterial", _context):
FnMaterial(prop.id_data).update_is_double_sided()


def _updateSphereMapType(prop, context):
def _mmd_material_update_sphere_texture_type(prop: "MMDMaterial", context):
FnMaterial(prop.id_data).update_sphere_texture_type(context.active_object)


def _updateToonTexture(prop, context):
def _mmd_material_update_toon_texture(prop: "MMDMaterial", _context):
FnMaterial(prop.id_data).update_toon_texture()


def _updateDropShadow(prop, context):
def _mmd_material_update_enabled_drop_shadow(prop: "MMDMaterial", _context):
FnMaterial(prop.id_data).update_drop_shadow()


def _updateSelfShadowMap(prop, context):
def _mmd_material_update_enabled_self_shadow_map(prop: "MMDMaterial", _context):
FnMaterial(prop.id_data).update_self_shadow_map()


def _updateSelfShadow(prop, context):
def _mmd_material_update_enabled_self_shadow(prop: "MMDMaterial", _context):
FnMaterial(prop.id_data).update_self_shadow()


def _updateEnabledToonEdge(prop, context):
def _mmd_material_update_enabled_toon_edge(prop: "MMDMaterial", _context):
FnMaterial(prop.id_data).update_enabled_toon_edge()


def _updateEdgeColor(prop, context):
def _mmd_material_update_edge_color(prop: "MMDMaterial", _context):
FnMaterial(prop.id_data).update_edge_color()


def _updateEdgeWeight(prop, context):
def _mmd_material_update_edge_weight(prop: "MMDMaterial", _context):
FnMaterial(prop.id_data).update_edge_weight()


def _getNameJ(prop):
def _mmd_material_get_name_j(prop: "MMDMaterial"):
return prop.get("name_j", "")


def _setNameJ(prop, value):
old_value = prop.get("name_j")
def _mmd_material_set_name_j(prop: "MMDMaterial", value: str):
prop_value = value
if prop_value and prop_value != old_value:
root = Model.findRoot(bpy.context.active_object)
if root:
rig = Model(root)
prop_value = utils.uniqueName(value, [mat.mmd_material.name_j for mat in rig.materials() if mat])
if prop_value and prop_value != prop.get("name_j"):
root = FnModel.find_root(bpy.context.active_object)
if root is None:
prop_value = utils.unique_name(value, {mat.mmd_material.name_j for mat in bpy.data.materials})
else:
prop_value = utils.uniqueName(value, [mat.mmd_material.name_j for mat in bpy.data.materials])
prop_value = utils.unique_name(value, {mat.mmd_material.name_j for mat in FnModel.iterate_materials(root)})

prop["name_j"] = prop_value

Expand All @@ -97,8 +95,8 @@ class MMDMaterial(bpy.types.PropertyGroup):
name="Name",
description="Japanese Name",
default="",
set=_setNameJ,
get=_getNameJ,
set=_mmd_material_set_name_j,
get=_mmd_material_get_name_j,
)

name_e: bpy.props.StringProperty(
Expand All @@ -124,7 +122,7 @@ class MMDMaterial(bpy.types.PropertyGroup):
precision=3,
step=0.1,
default=[0.4, 0.4, 0.4],
update=_updateAmbientColor,
update=_mmd_material_update_ambient_color,
)

diffuse_color: bpy.props.FloatVectorProperty(
Expand All @@ -137,7 +135,7 @@ class MMDMaterial(bpy.types.PropertyGroup):
precision=3,
step=0.1,
default=[0.8, 0.8, 0.8],
update=_updateDiffuseColor,
update=_mmd_material_update_diffuse_color,
)

alpha: bpy.props.FloatProperty(
Expand All @@ -148,7 +146,7 @@ class MMDMaterial(bpy.types.PropertyGroup):
precision=3,
step=0.1,
default=1.0,
update=_updateAlpha,
update=_mmd_material_update_alpha,
)

specular_color: bpy.props.FloatVectorProperty(
Expand All @@ -161,7 +159,7 @@ class MMDMaterial(bpy.types.PropertyGroup):
precision=3,
step=0.1,
default=[0.625, 0.625, 0.625],
update=_updateSpecularColor,
update=_mmd_material_update_specular_color,
)

shininess: bpy.props.FloatProperty(
Expand All @@ -171,42 +169,42 @@ class MMDMaterial(bpy.types.PropertyGroup):
soft_max=512,
step=100.0,
default=50.0,
update=_updateShininess,
update=_mmd_material_update_shininess,
)

is_double_sided: bpy.props.BoolProperty(
name="Double Sided",
description="Both sides of mesh should be rendered",
default=False,
update=_updateIsDoubleSided,
update=_mmd_material_update_is_double_sided,
)

enabled_drop_shadow: bpy.props.BoolProperty(
name="Ground Shadow",
description="Display ground shadow",
default=True,
update=_updateDropShadow,
update=_mmd_material_update_enabled_drop_shadow,
)

enabled_self_shadow_map: bpy.props.BoolProperty(
name="Self Shadow Map",
description="Object can become shadowed by other objects",
default=True,
update=_updateSelfShadowMap,
update=_mmd_material_update_enabled_self_shadow_map,
)

enabled_self_shadow: bpy.props.BoolProperty(
name="Self Shadow",
description="Object can cast shadows",
default=True,
update=_updateSelfShadow,
update=_mmd_material_update_enabled_self_shadow,
)

enabled_toon_edge: bpy.props.BoolProperty(
name="Toon Edge",
description="Use toon edge",
default=False,
update=_updateEnabledToonEdge,
update=_mmd_material_update_enabled_toon_edge,
)

edge_color: bpy.props.FloatVectorProperty(
Expand All @@ -219,7 +217,7 @@ class MMDMaterial(bpy.types.PropertyGroup):
precision=3,
step=0.1,
default=[0, 0, 0, 1],
update=_updateEdgeColor,
update=_mmd_material_update_edge_color,
)

edge_weight: bpy.props.FloatProperty(
Expand All @@ -230,7 +228,7 @@ class MMDMaterial(bpy.types.PropertyGroup):
soft_max=2,
step=1.0,
default=1.0,
update=_updateEdgeWeight,
update=_mmd_material_update_edge_weight,
)

sphere_texture_type: bpy.props.EnumProperty(
Expand All @@ -242,22 +240,22 @@ class MMDMaterial(bpy.types.PropertyGroup):
(str(material.SPHERE_MODE_ADD), "Add", "", 3),
(str(material.SPHERE_MODE_SUBTEX), "SubTexture", "", 4),
],
update=_updateSphereMapType,
update=_mmd_material_update_sphere_texture_type,
)

is_shared_toon_texture: bpy.props.BoolProperty(
name="Use Shared Toon Texture",
description="Use shared toon texture or custom toon texture",
default=False,
update=_updateToonTexture,
update=_mmd_material_update_toon_texture,
)

toon_texture: bpy.props.StringProperty(
name="Toon Texture",
subtype="FILE_PATH",
description="The file path of custom toon texture",
default="",
update=_updateToonTexture,
update=_mmd_material_update_toon_texture,
)

shared_toon_texture: bpy.props.IntProperty(
Expand All @@ -266,7 +264,7 @@ class MMDMaterial(bpy.types.PropertyGroup):
default=0,
min=0,
max=9,
update=_updateToonTexture,
update=_mmd_material_update_toon_texture,
)

comment: bpy.props.StringProperty(
Expand Down
Loading

0 comments on commit 704f905

Please sign in to comment.