-
Notifications
You must be signed in to change notification settings - Fork 8
Refactor #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Wiechciu
wants to merge
4
commits into
RaidTheory:main
Choose a base branch
from
Wiechciu:refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Refactor #5
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| @tool | ||
| extends EditorContextMenuPlugin | ||
|
|
||
|
|
||
| const ICON = preload("res://addons/mixamo_animation_retargeter/icon.png") | ||
|
|
||
|
|
||
| func _popup_menu(paths: PackedStringArray) -> void: | ||
| if not _validate_paths(paths): | ||
| return | ||
|
|
||
| add_context_menu_item("Retarget Mixamo Animations", _retarget, ICON) | ||
|
|
||
|
|
||
| func _retarget(paths: Array) -> void: | ||
| for path: String in paths: | ||
| if path.get_extension() == "fbx": | ||
| _process_fbx_file(path) | ||
|
|
||
|
|
||
| func _validate_paths(paths: Array) -> bool: | ||
| for path: String in paths: | ||
| if path.get_extension() == "fbx": | ||
| return true | ||
|
|
||
| return false | ||
|
|
||
|
|
||
| func _process_fbx_file(fbx_path: String) -> void: | ||
| var dir_path: String = fbx_path.get_base_dir() | ||
| print("Exporting animations from ", fbx_path) | ||
|
|
||
| var import_file_path: String = fbx_path + ".import" | ||
| var config := ConfigFile.new() | ||
| var err := config.load(import_file_path) | ||
| if err == OK: | ||
| var subresources: Dictionary = config.get_value("params", "_subresources", {}) | ||
| if "nodes" not in subresources: | ||
| subresources["nodes"] = {} | ||
| if "PATH:Skeleton3D" not in subresources["nodes"]: | ||
| subresources["nodes"]["PATH:Skeleton3D"] = {} | ||
|
|
||
| # Update the specific settings for Skeleton3D | ||
| subresources["nodes"]["PATH:Skeleton3D"]["retarget/bone_map"] = load("res://addons/mixamo_animation_retargeter/mixamo_bone_map.tres") | ||
| subresources["nodes"]["PATH:Skeleton3D"]["retarget/bone_renamer/unique_node/skeleton_name"] = "Skeleton" | ||
| subresources["nodes"]["PATH:Skeleton3D"]["retarget/remove_tracks/unmapped_bones"] = true | ||
|
|
||
| # Add or update the animations section | ||
| if not "animations" in subresources: | ||
| subresources["animations"] = {} | ||
| if not "mixamo_com" in subresources["animations"]: | ||
| subresources["animations"]["mixamo_com"] = {} | ||
|
|
||
| # Get the FBX file name and convert it to snake case | ||
| var fbx_file_name = fbx_path.get_file().get_basename() | ||
| var snake_case_name = fbx_file_name.to_snake_case() | ||
|
|
||
| # Create the relative path (already starts with res://) | ||
| var relative_res_path = dir_path.path_join(snake_case_name + ".res") | ||
|
|
||
| # Update the save to file settings for mixamo_com animation | ||
| subresources["animations"]["mixamo_com"]["save_to_file/enabled"] = true | ||
| subresources["animations"]["mixamo_com"]["save_to_file/keep_custom_tracks"] = "" | ||
| subresources["animations"]["mixamo_com"]["save_to_file/path"] = relative_res_path | ||
| subresources["animations"]["mixamo_com"]["settings/loop_mode"] = 0 | ||
|
|
||
| # Save the updated subresources back to the config | ||
| config.set_value("params", "_subresources", subresources) | ||
|
|
||
| # Save the changes to the .import file | ||
| err = config.save(import_file_path) | ||
| if err == OK: | ||
| print("Import settings updated successfully for ", fbx_path) | ||
| # Trigger reimport immediately after saving | ||
| _trigger_reimport(fbx_path) | ||
| else: | ||
| print("Failed to save import settings for ", fbx_path) | ||
| else: | ||
| print("Failed to load import file for editing: ", fbx_path) | ||
|
|
||
|
|
||
| func _trigger_reimport(fbx_path: String) -> void: | ||
| # Trigger reimport of the FBX file | ||
| var file_system = EditorInterface.get_resource_filesystem() | ||
| file_system.reimport_files([fbx_path]) | ||
| print("Triggered reimport of FBX file") | ||
211 changes: 8 additions & 203 deletions
211
addons/mixamo_animation_retargeter/mixamo_animation_retargeter.gd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,211 +1,16 @@ | ||
| @tool | ||
| extends EditorPlugin | ||
|
|
||
| var filesystem = get_editor_interface().get_resource_filesystem() | ||
|
|
||
| var popupFilesystem : PopupMenu | ||
| var context_menu_plugin_instance: EditorContextMenuPlugin | ||
|
|
||
| var retarget_menu_id = 10002 | ||
|
|
||
| func _enter_tree(): | ||
| FindFilesystemPopup() | ||
| func _enter_tree() -> void: | ||
| context_menu_plugin_instance = preload("res://addons/mixamo_animation_retargeter/context_menu.gd").new() | ||
| add_context_menu_plugin(EditorContextMenuPlugin.CONTEXT_SLOT_FILESYSTEM, context_menu_plugin_instance) | ||
| print("Mixamo Animation Retargeter loaded.") | ||
|
|
||
| func _exit_tree(): | ||
| if popupFilesystem: | ||
| popupFilesystem.disconnect("about_to_popup", Callable(self, "AddItemToPopup")) | ||
| popupFilesystem.disconnect("id_pressed", Callable(self, "RetargetMixamoAnimation")) | ||
|
|
||
| func FindFilesystemPopup(): | ||
| var file_system:FileSystemDock = get_editor_interface().get_file_system_dock() | ||
|
|
||
| for child in file_system.get_children(): | ||
| var pop:PopupMenu = child as PopupMenu | ||
| if not pop: continue | ||
|
|
||
| popupFilesystem = pop | ||
| popupFilesystem.connect("about_to_popup", Callable(self, "AddItemToPopup")) | ||
| popupFilesystem.connect("id_pressed", Callable(self, "RetargetMixamoAnimation")) | ||
|
|
||
| func AddItemToPopup(): | ||
| popupFilesystem.add_separator("Mixamo Animation Retargeter") | ||
| popupFilesystem.add_item("Retarget Mixamo Animation(s)", retarget_menu_id) | ||
|
|
||
| func RetargetMixamoAnimation(id : int): | ||
| if id == retarget_menu_id: | ||
| var selected_paths = get_selected_paths(get_filesystem_tree(self)) | ||
| var fbx_files = selected_paths.filter(func(path): return path.ends_with(".fbx")) | ||
| if fbx_files.size() > 0: | ||
| _show_save_dialog(fbx_files) | ||
|
|
||
| func _show_save_dialog(fbx_paths: Array) -> void: | ||
| var file_dialog = EditorFileDialog.new() | ||
| file_dialog.file_mode = EditorFileDialog.FILE_MODE_OPEN_DIR | ||
| file_dialog.access = EditorFileDialog.ACCESS_RESOURCES | ||
| file_dialog.title = "Select Export Folder for Animations" | ||
|
|
||
| file_dialog.connect("dir_selected", Callable(self, "_on_export_folder_selected").bind(fbx_paths)) | ||
| file_dialog.connect("canceled", Callable(self, "_on_file_dialog_closed")) | ||
|
|
||
| get_editor_interface().get_base_control().add_child(file_dialog) | ||
| file_dialog.popup_centered_ratio(0.6) | ||
|
|
||
| func _on_export_folder_selected(dir_path: String, fbx_paths: Array) -> void: | ||
| for fbx_path in fbx_paths: | ||
| _process_fbx_file(fbx_path, dir_path) | ||
|
|
||
| _on_file_dialog_closed() | ||
|
|
||
| func _process_fbx_file(fbx_path: String, dir_path: String) -> void: | ||
| print("Exporting animations from ", fbx_path, " to ", dir_path) | ||
|
|
||
| var import_file_path: String = fbx_path + ".import" | ||
| var config := ConfigFile.new() | ||
| var err := config.load(import_file_path) | ||
| if err == OK: | ||
| var subresources: Dictionary = config.get_value("params", "_subresources", {}) | ||
| if "nodes" not in subresources: | ||
| subresources["nodes"] = {} | ||
| if "PATH:Skeleton3D" not in subresources["nodes"]: | ||
| subresources["nodes"]["PATH:Skeleton3D"] = {} | ||
|
|
||
| # Update the specific settings for Skeleton3D | ||
| subresources["nodes"]["PATH:Skeleton3D"]["retarget/bone_map"] = load("res://addons/mixamo_animation_retargeter/mixamo_bone_map.tres") | ||
| subresources["nodes"]["PATH:Skeleton3D"]["retarget/bone_renamer/unique_node/skeleton_name"] = "Skeleton" | ||
| subresources["nodes"]["PATH:Skeleton3D"]["retarget/remove_tracks/unmapped_bones"] = true | ||
|
|
||
| # Add or update the animations section | ||
| if not "animations" in subresources: | ||
| subresources["animations"] = {} | ||
| if not "mixamo_com" in subresources["animations"]: | ||
| subresources["animations"]["mixamo_com"] = {} | ||
|
|
||
| # Get the FBX file name and convert it to snake case | ||
| var fbx_file_name = fbx_path.get_file().get_basename() | ||
| var snake_case_name = to_snake_case(fbx_file_name) | ||
|
|
||
| # Create the relative path (already starts with res://) | ||
| var relative_res_path = dir_path.path_join(snake_case_name + ".res") | ||
|
|
||
| # Update the save to file settings for mixamo_com animation | ||
| subresources["animations"]["mixamo_com"]["save_to_file/enabled"] = true | ||
| subresources["animations"]["mixamo_com"]["save_to_file/keep_custom_tracks"] = "" | ||
| subresources["animations"]["mixamo_com"]["save_to_file/path"] = relative_res_path | ||
| subresources["animations"]["mixamo_com"]["settings/loop_mode"] = 0 | ||
|
|
||
| # Save the updated subresources back to the config | ||
| config.set_value("params", "_subresources", subresources) | ||
|
|
||
| # Save the changes to the .import file | ||
| err = config.save(import_file_path) | ||
| if err == OK: | ||
| print("Import settings updated successfully for ", fbx_path) | ||
| # Trigger reimport immediately after saving | ||
| _trigger_reimport(fbx_path) | ||
| else: | ||
| print("Failed to save import settings for ", fbx_path) | ||
| else: | ||
| print("Failed to load import file for editing: ", fbx_path) | ||
|
|
||
| # Helper function to convert string to snake_case | ||
| func to_snake_case(string: String) -> String: | ||
| var result = "" | ||
| var prev_is_lowercase = false | ||
| for i in range(string.length()): | ||
| var c = string[i] | ||
| if c == ' ': | ||
| if result and result[-1] != '_': | ||
| result += '_' | ||
| elif c >= 'A' and c <= 'Z': | ||
| if prev_is_lowercase and result and result[-1] != '_': | ||
| result += '_' | ||
| result += c.to_lower() | ||
| prev_is_lowercase = false | ||
| else: | ||
| result += c.to_lower() | ||
| prev_is_lowercase = true | ||
| return result | ||
|
|
||
| func _trigger_reimport(fbx_path: String) -> void: | ||
| # Trigger reimport of the FBX file | ||
| var file_system = get_editor_interface().get_resource_filesystem() | ||
| file_system.reimport_files([fbx_path]) | ||
| print("Triggered reimport of FBX file") | ||
|
|
||
| func _get_import_settings(file_path: String) -> ConfigFile: | ||
| var import_settings = ConfigFile.new() | ||
| var err = import_settings.load(file_path + ".import") | ||
| if err == OK: | ||
| return import_settings | ||
| else: | ||
| print("Failed to load import settings for: ", file_path) | ||
| return null | ||
|
|
||
| # Helper function to find Skeleton3D in the scene | ||
| func find_skeleton(node: Node) -> Skeleton3D: | ||
| if node is Skeleton3D: | ||
| return node | ||
| for child in node.get_children(): | ||
| var result = find_skeleton(child) | ||
| if result: | ||
| return result | ||
| return null | ||
|
|
||
| func _on_file_dialog_closed() -> void: | ||
| var file_dialog = get_editor_interface().get_base_control().get_node_or_null("FileDialog") | ||
| if file_dialog: | ||
| file_dialog.queue_free() | ||
|
|
||
| # Helper functions from AutoMat | ||
| static func get_selected_paths(fs_tree:Tree)->Array: | ||
| var sel_items: = tree_get_selected_items(fs_tree) | ||
| var result: = [] | ||
| for i in sel_items: | ||
| i = i as TreeItem | ||
| result.push_back(i.get_metadata(0)) | ||
| return result | ||
|
|
||
| static func get_filesystem_tree(plugin:EditorPlugin)->Tree: | ||
| var dock = plugin.get_editor_interface().get_file_system_dock() | ||
| return find_node_by_class_path(dock, ['SplitContainer','Tree']) as Tree | ||
|
|
||
| static func tree_get_selected_items(tree:Tree)->Array: | ||
| var res = [] | ||
| var item = tree.get_next_selected(tree.get_root()) | ||
| while true: | ||
| if item == null: break | ||
| res.push_back(item) | ||
| item = tree.get_next_selected(item) | ||
| return res | ||
|
|
||
| static func find_node_by_class_path(node:Node, class_path:Array)->Node: | ||
| var res:Node | ||
|
|
||
| var stack = [] | ||
| var depths = [] | ||
|
|
||
| var first = class_path[0] | ||
| for c in node.get_children(): | ||
| if c.get_class() == first: | ||
| stack.push_back(c) | ||
| depths.push_back(0) | ||
|
|
||
| if stack == null: return res | ||
|
|
||
| var max_ = class_path.size()-1 | ||
|
|
||
| while stack: | ||
| var d = depths.pop_back() | ||
| var n = stack.pop_back() | ||
|
|
||
| if d>max_: | ||
| continue | ||
| if n.get_class() == class_path[d]: | ||
| if d == max_: | ||
| res = n | ||
| return res | ||
|
|
||
| for c in n.get_children(): | ||
| stack.push_back(c) | ||
| depths.push_back(d+1) | ||
|
|
||
| return res | ||
| func _exit_tree() -> void: | ||
| remove_context_menu_plugin(context_menu_plugin_instance) | ||
| print("Mixamo Animation Retargeter unloaded.") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will return true if the first file is fbx, even if the rest is not. I think, this is what you were looking for:
Oh, I see that this is only for the context menu to appear. Still, I think that the retarget option should only be shown if only fbx files are selected. But it also works as it is 👍