Merge branch 'Digitigrade-legs-tool' of https://github.com/989onan/Avatar-Toolkit into Digitigrade-legs-tool
This commit is contained in:
@@ -4,6 +4,7 @@ from typing import List, Tuple, Optional
|
||||
from bpy.types import Material, Operator, Context, Object
|
||||
from ..core.common import clean_material_names
|
||||
from ..core.register import register_wrap
|
||||
from ..functions.translations import t
|
||||
|
||||
def textures_match(tex1: bpy.types.ImageTexture, tex2: bpy.types.ImageTexture) -> bool:
|
||||
return tex1.image == tex2.image and tex1.extension == tex2.extension
|
||||
@@ -58,8 +59,8 @@ def report_consolidated(self: Operator, num_combined: int) -> None:
|
||||
@register_wrap
|
||||
class CombineMaterials(Operator):
|
||||
bl_idname = "avatar_toolkit.combine_materials"
|
||||
bl_label = "Combine Materials"
|
||||
bl_description = "Combine similar materials to optimize the model"
|
||||
bl_label = t("Optimization.combine_materials.label")
|
||||
bl_description = t("Optimization.combine_materials.desc")
|
||||
bl_options = {'REGISTER', 'UNDO'}
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -3,12 +3,13 @@ from typing import List, Optional
|
||||
from bpy.types import Operator, Context, Object
|
||||
from ..core.register import register_wrap
|
||||
from ..core.common import fix_uv_coordinates
|
||||
from ..functions.translations import t
|
||||
|
||||
@register_wrap
|
||||
class JoinAllMeshes(Operator):
|
||||
bl_idname = "avatar_toolkit.join_all_meshes"
|
||||
bl_label = "Join All Meshes"
|
||||
bl_description = "Join all meshes in the scene"
|
||||
bl_label = t("Optimization.join_all_meshes.label")
|
||||
bl_description = t("Optimization.join_all_meshes.desc")
|
||||
bl_options = {'REGISTER', 'UNDO'}
|
||||
|
||||
@classmethod
|
||||
@@ -45,8 +46,8 @@ class JoinAllMeshes(Operator):
|
||||
@register_wrap
|
||||
class JoinSelectedMeshes(Operator):
|
||||
bl_idname = "avatar_toolkit.join_selected_meshes"
|
||||
bl_label = "Join Selected Meshes"
|
||||
bl_description = "Join selected meshes"
|
||||
bl_label = t("Optimization.join_selected_meshes.label")
|
||||
bl_description = t("Optimization.join_selected_meshes.desc")
|
||||
bl_options = {'REGISTER', 'UNDO'}
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
from ast import Dict
|
||||
from itertools import count
|
||||
import bpy
|
||||
import re
|
||||
from typing import List, Tuple, Optional, TypedDict
|
||||
from bpy.types import Material, Operator, Context, Object
|
||||
from ..core.register import register_wrap
|
||||
from ..core.common import get_armature
|
||||
|
||||
|
||||
class meshEntry(TypedDict):
|
||||
mesh: bpy.types.Object
|
||||
shapekeys: list[str]
|
||||
|
||||
@register_wrap
|
||||
class RemoveDoublesSafely(Operator):
|
||||
bl_idname = "avatar_toolkit.remove_doubles_safely"
|
||||
bl_label = "Remove Doubles Safely"
|
||||
bl_description = "Remove Doubles on all meshes, making sure to not fuse things like mouths together."
|
||||
bl_options = {'REGISTER', 'UNDO'}
|
||||
objects_to_do: list[meshEntry] = []
|
||||
merge_distance: bpy.props.FloatProperty(default=0.0001)
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context: Context) -> bool:
|
||||
return context.mode == 'OBJECT'
|
||||
|
||||
def execute(self, context: Context) -> set:
|
||||
if not bpy.data.objects:
|
||||
self.report({'INFO'}, "No objects in the scene")
|
||||
return
|
||||
|
||||
bpy.ops.object.mode_set(mode='OBJECT')
|
||||
bpy.ops.object.select_all(action='DESELECT')
|
||||
objects: List[Object] = get_armature(context).children if get_armature(context) else context.view_layer.objects
|
||||
|
||||
meshes: List[Object] = [obj for obj in objects if obj.type == 'MESH']
|
||||
|
||||
for mesh in meshes:
|
||||
if mesh.data.name not in [stored_object["mesh"].data.name for stored_object in self.objects_to_do]:
|
||||
mesh_shapekeys = {"mesh":mesh,"shapekeys":[]}
|
||||
mesh_data: bpy.types.Mesh = mesh.data
|
||||
shape: bpy.types.ShapeKey = None
|
||||
if mesh_data.shape_keys:
|
||||
for shape in mesh_data.shape_keys.key_blocks:
|
||||
mesh_shapekeys["shapekeys"].append(shape.name)
|
||||
self.objects_to_do.append(mesh_shapekeys)
|
||||
|
||||
|
||||
return {'FINISHED'}
|
||||
|
||||
def invoke(self, context: Context, event: bpy.types.Event) -> set:
|
||||
|
||||
self.execute(context)
|
||||
context.window_manager.modal_handler_add(self)
|
||||
return {'RUNNING_MODAL'}
|
||||
|
||||
def modify_mesh(self, context: Context, mesh: meshEntry):
|
||||
mesh["mesh"].select_set(True)
|
||||
context.view_layer.objects.active = mesh["mesh"]
|
||||
context.view_layer.objects.active = mesh["mesh"]
|
||||
mesh_data: bpy.types.Mesh = mesh["mesh"].data
|
||||
bpy.ops.object.mode_set(mode='EDIT')
|
||||
|
||||
|
||||
bpy.ops.object.mode_set(mode='OBJECT')
|
||||
for index, point in enumerate(mesh["mesh"].active_shape_key.points):
|
||||
if point.co.xyz != mesh_data.shape_keys.key_blocks[0].points[index].co.xyz:
|
||||
mesh_data.vertices[index].select = True
|
||||
print("shapekey has a moved vertex at index \""+str(index)+"\", excluding from double merging!")
|
||||
bpy.ops.object.mode_set(mode='EDIT')
|
||||
|
||||
|
||||
|
||||
|
||||
bpy.ops.object.mode_set(mode='OBJECT')
|
||||
mesh["mesh"].select_set(False)
|
||||
|
||||
|
||||
def modal(self, context: Context, event: bpy.types.Event) -> set:
|
||||
|
||||
|
||||
|
||||
|
||||
if len(self.objects_to_do) > 0:
|
||||
mesh = self.objects_to_do[0]
|
||||
mesh_data: bpy.types.Mesh = mesh["mesh"].data
|
||||
if len(mesh['shapekeys']) > 0:
|
||||
shapekeyname: str = mesh['shapekeys'].pop(0)
|
||||
|
||||
target_shapekey: int = mesh_data.shape_keys.key_blocks.find(shapekeyname)
|
||||
mesh["mesh"].active_shape_key_index = target_shapekey
|
||||
print("doing shapekey \""+shapekeyname+"\" on mesh \""+mesh['mesh'].name+"\".")
|
||||
self.modify_mesh(context, mesh)
|
||||
|
||||
elif not (mesh_data.shape_keys):
|
||||
print("doing mesh with no shapekeys named \""+mesh['mesh'].name+"\".")
|
||||
mesh["mesh"].select_set(True)
|
||||
context.view_layer.objects.active = mesh["mesh"]
|
||||
bpy.ops.object.mode_set(mode='EDIT')
|
||||
mesh_data.vertices.foreach_set("select",[False]*len(mesh_data.vertices))
|
||||
|
||||
bpy.ops.mesh.select_all(action="INVERT")
|
||||
bpy.ops.mesh.remove_doubles(threshold=self.merge_distance,use_unselected=False)
|
||||
|
||||
bpy.ops.object.mode_set(mode='OBJECT')
|
||||
mesh["mesh"].select_set(False)
|
||||
self.objects_to_do.pop(0)
|
||||
else:
|
||||
mesh["mesh"].select_set(True)
|
||||
context.view_layer.objects.active = mesh["mesh"]
|
||||
bpy.ops.object.mode_set(mode='EDIT')
|
||||
|
||||
bpy.ops.mesh.select_all(action="INVERT")
|
||||
bpy.ops.mesh.remove_doubles(threshold=self.merge_distance,use_unselected=False)
|
||||
|
||||
bpy.ops.object.mode_set(mode='OBJECT')
|
||||
mesh["mesh"].select_set(False)
|
||||
|
||||
self.objects_to_do.pop(0)
|
||||
if len(self.objects_to_do) > 0:
|
||||
mesh = self.objects_to_do[0]
|
||||
mesh["mesh"].select_set(True)
|
||||
context.view_layer.objects.active = mesh["mesh"]
|
||||
bpy.ops.object.mode_set(mode='EDIT')
|
||||
mesh_data.vertices.foreach_set("select",[False]*len(mesh_data.vertices))
|
||||
bpy.ops.object.mode_set(mode='OBJECT')
|
||||
mesh["mesh"].select_set(False)
|
||||
|
||||
else:
|
||||
return {'FINISHED'}
|
||||
|
||||
return {'RUNNING_MODAL'}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -5,12 +5,13 @@ import re
|
||||
from bpy.types import Operator, Context, Object
|
||||
from ..core.dictionaries import bone_names
|
||||
from ..core.common import get_armature, simplify_bonename
|
||||
from ..functions.translations import t
|
||||
|
||||
@register_wrap
|
||||
class ConvertToResonite(Operator):
|
||||
bl_idname = 'avatar_toolkit.convert_to_resonite'
|
||||
bl_label = "Convert to Resonite" #t('Tools.convert_to_resonite.label')
|
||||
bl_description = "Converts bone names on a model to names compatable with Resonite" #t('Tools.convert_to_resonite.desc')
|
||||
bl_label = t('Tools.convert_to_resonite.label')
|
||||
bl_description = t('Tools.convert_to_resonite.desc')
|
||||
bl_options = {'REGISTER', 'UNDO'}
|
||||
|
||||
@classmethod
|
||||
|
||||
+49
-17
@@ -2,63 +2,95 @@ import os
|
||||
import json
|
||||
import bpy
|
||||
from bpy.app.translations import locale
|
||||
from ..core.register import register_wrap
|
||||
from typing import Dict, List, Tuple
|
||||
from ..core.addon_preferences import save_preference, get_preference
|
||||
|
||||
main_dir: str = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
resources_dir: str = os.path.join(main_dir, "resources")
|
||||
translations_dir: str = os.path.join(resources_dir, "translations")
|
||||
# Use __file__ to get the current file's directory
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
main_dir = os.path.dirname(current_dir)
|
||||
resources_dir = os.path.join(main_dir, "resources")
|
||||
translations_dir = os.path.join(resources_dir, "translations")
|
||||
|
||||
dictionary: Dict[str, str] = dict()
|
||||
languages: List[str] = []
|
||||
verbose: bool = True
|
||||
|
||||
def load_translations() -> None:
|
||||
def load_translations() -> bool:
|
||||
global dictionary, languages
|
||||
|
||||
old_dictionary = dictionary.copy()
|
||||
|
||||
dictionary = dict()
|
||||
languages = ["auto"]
|
||||
|
||||
language: str = bpy.context.preferences.view.language
|
||||
|
||||
# Populate languages list
|
||||
for i in os.listdir(translations_dir):
|
||||
languages.append(i.split(".")[0])
|
||||
lang = i.split(".")[0]
|
||||
if lang != "auto":
|
||||
languages.append(lang)
|
||||
|
||||
language_index = get_preference("language", 0)
|
||||
print(f"Loading translations for language index: {language_index}") # Debug print
|
||||
|
||||
if language_index == 0: # "auto"
|
||||
language = bpy.context.preferences.view.language
|
||||
else:
|
||||
try:
|
||||
language = languages[language_index]
|
||||
except IndexError:
|
||||
language = bpy.context.preferences.view.language
|
||||
|
||||
print(f"Selected language: {language}") # Debug print
|
||||
|
||||
translation_file: str = os.path.join(translations_dir, language + ".json")
|
||||
if os.path.exists(translation_file):
|
||||
with open(translation_file, 'r', encoding='utf-8') as file:
|
||||
dictionary = json.load(file)["messages"]
|
||||
print(f"Loaded translations: {dictionary}") # Debug print
|
||||
else:
|
||||
custom_language: str = language.split("_")[0]
|
||||
custom_translation_file: str = os.path.join(translations_dir, custom_language + ".json")
|
||||
if os.path.exists(custom_translation_file):
|
||||
with open(custom_translation_file, 'r', encoding='utf-8') as file:
|
||||
dictionary = json.load(file)["messages"]
|
||||
print(f"Loaded custom translations: {dictionary}") # Debug print
|
||||
else:
|
||||
print(f"Translation file not found for language: {language}")
|
||||
default_file: str = os.path.join(translations_dir, "en_US.json")
|
||||
if os.path.exists(default_file):
|
||||
with open(default_file, 'r', encoding='utf-8') as file:
|
||||
dictionary = json.load(file)["messages"]
|
||||
print(f"Loaded default translations: {dictionary}") # Debug print
|
||||
else:
|
||||
print("Default translation file 'en_US.json' not found.")
|
||||
return dictionary != old_dictionary
|
||||
|
||||
def t(phrase: str, *args, **kwargs) -> str:
|
||||
output: str = dictionary.get(phrase)
|
||||
if output is None:
|
||||
if verbose:
|
||||
print('Warning: Unknown phrase: ' + phrase)
|
||||
return phrase
|
||||
return output.format(*args, **kwargs)
|
||||
print(f'Warning: Unknown phrase: {phrase}')
|
||||
return default if default is not None else phrase
|
||||
print(f"Translating '{phrase}' to '{output}'") # Debug print
|
||||
return output
|
||||
|
||||
def get_language_display_name(lang: str) -> str:
|
||||
if lang == "auto":
|
||||
return t("Language.auto", "Automatic")
|
||||
return t(f"Language.{lang}", lang)
|
||||
|
||||
def get_languages_list(self, context) -> List[Tuple[str, str, str]]:
|
||||
choices: List[Tuple[str, str, str]] = []
|
||||
for language in languages:
|
||||
choices.append((language, language, language))
|
||||
return choices
|
||||
return [(str(i), get_language_display_name(lang), f"Use {lang} language") for i, lang in enumerate(languages)]
|
||||
|
||||
def update_ui(self, context) -> None:
|
||||
def update_language(self, context):
|
||||
print(f"Updating language to: {self.avatar_toolkit_language}") # Debug print
|
||||
save_preference("language", int(self.avatar_toolkit_language))
|
||||
load_translations()
|
||||
bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)
|
||||
# Set a flag to indicate that a language change has occurred
|
||||
context.scene.avatar_toolkit_language_changed = True
|
||||
# Show popup after language change
|
||||
bpy.ops.avatar_toolkit.translation_restart_popup('INVOKE_DEFAULT')
|
||||
|
||||
# Initial load of translations
|
||||
print("Performing initial load of translations") # Debug print
|
||||
load_translations()
|
||||
|
||||
Reference in New Issue
Block a user