|
| 1 | +""" |
| 2 | +Python script to create a shelf button for the 2D Texture Rigger in Maya. |
| 3 | +This script is executed by install.mel. |
| 4 | +Copyright 2025 by Hasan Çivili. All Rights Reserved. (Adapted for 2D Texture Rigger) |
| 5 | +""" |
| 6 | +import os |
| 7 | +import sys |
| 8 | +import maya.cmds as cmds |
| 9 | +import maya.mel as mel |
| 10 | + |
| 11 | +def create_texture_rigger_shelf_button(install_script_path): |
| 12 | + """Creates a shelf button for the 2D Texture Rigger.""" |
| 13 | + try: |
| 14 | + if not install_script_path: |
| 15 | + cmds.warning("2D Texture Rigger Installer: Script path not provided to create_texture_rigger_shelf_button.") |
| 16 | + return |
| 17 | + |
| 18 | + # script_dir is the directory where install.py (and uv_locator_tool.py) are located. |
| 19 | + script_dir = os.path.dirname(install_script_path) |
| 20 | + script_dir_norm = os.path.normpath(script_dir) |
| 21 | + |
| 22 | + # The main tool script is TextureRiggerTool.py |
| 23 | + tool_module_name = "TextureRiggerTool" # Changed from "uv_locator_tool" |
| 24 | + tool_script_file = tool_module_name + ".py" |
| 25 | + tool_script_path_full = os.path.join(script_dir_norm, tool_script_file) |
| 26 | + |
| 27 | + if not os.path.exists(tool_script_path_full): |
| 28 | + cmds.warning(f"2D Texture Rigger Installer: Main tool script '{tool_script_file}' not found at: {tool_script_path_full}") |
| 29 | + return |
| 30 | + |
| 31 | + icon_name = "texture_rigger_icon.png" # Ensure this icon exists in the same directory |
| 32 | + icon_path = os.path.join(script_dir_norm, icon_name) |
| 33 | + icon_path_norm = os.path.normpath(icon_path) |
| 34 | + |
| 35 | + if not os.path.exists(icon_path_norm): |
| 36 | + cmds.warning(f"2D Texture Rigger Installer: Icon file '{icon_name}' not found at: {icon_path_norm}. Using default Maya icon.") |
| 37 | + icon_path_for_shelf = "pythonFamily.png" # Default Maya icon |
| 38 | + else: |
| 39 | + icon_path_for_shelf = icon_path_norm |
| 40 | + |
| 41 | + shelf_name = cmds.optionVar(q="currentShelf") # Get current active shelf |
| 42 | + if not shelf_name: |
| 43 | + # Fallback if no shelf is active, or create a specific one |
| 44 | + shelf_name = "Custom" # Or your preferred shelf name like "TextureTools" |
| 45 | + if not cmds.shelfLayout(shelf_name, exists=True): |
| 46 | + cmds.shelfLayout(shelf_name, parent="ShelfLayout") # Maya's main shelf area |
| 47 | + |
| 48 | + # Ensure script_dir_norm uses forward slashes for the Python command string, or is a raw string |
| 49 | + escaped_tool_dir = script_dir_norm.replace("\\\\", "/").replace("\\", "/") |
| 50 | + |
| 51 | + # This is the command that the shelf button will execute. |
| 52 | + # It correctly imports 'TextureRiggerTool' and calls its 'show_ui' function. |
| 53 | + shelf_command = f""" |
| 54 | +import sys |
| 55 | +import os |
| 56 | +import maya.cmds as cmds |
| 57 | +import importlib |
| 58 | +
|
| 59 | +tool_dir = r'{escaped_tool_dir}' |
| 60 | +tool_module_name = '{tool_module_name}' |
| 61 | +
|
| 62 | +if tool_dir not in sys.path: |
| 63 | + sys.path.append(tool_dir) |
| 64 | +
|
| 65 | +try: |
| 66 | + # Dynamically import the module |
| 67 | + module_to_run = importlib.import_module(tool_module_name) |
| 68 | + # Reload for development convenience |
| 69 | + importlib.reload(module_to_run) |
| 70 | + # Call the UI function |
| 71 | + module_to_run.show_ui() |
| 72 | +except ImportError as e_import: |
| 73 | + error_msg = f"2D Texture Rigger: IMPORT ERROR - {{e_import}}. Could not import '{{tool_module_name}}'. Ensure it is in: {{tool_dir}}." |
| 74 | + print(error_msg) |
| 75 | + cmds.warning(error_msg) |
| 76 | +except AttributeError as e_attr: |
| 77 | + error_msg = f"2D Texture Rigger: ATTRIBUTE ERROR - {{e_attr}}. Could not find 'show_ui' in '{{tool_module_name}}'." |
| 78 | + print(error_msg) |
| 79 | + cmds.warning(error_msg) |
| 80 | +except Exception as e_runtime: |
| 81 | + error_msg = f"2D Texture Rigger: RUNTIME ERROR - {{e_runtime}}." |
| 82 | + print(error_msg) |
| 83 | + cmds.warning(error_msg) |
| 84 | +""" |
| 85 | + |
| 86 | + # Check if a button with this label already exists on the shelf |
| 87 | + shelf_buttons = cmds.shelfLayout(shelf_name, query=True, childArray=True) or [] |
| 88 | + existing_button = None |
| 89 | + for btn in shelf_buttons: |
| 90 | + try: |
| 91 | + if cmds.shelfButton(btn, query=True, label=True) == "2D Texture Rigger": # Check for old or default label |
| 92 | + existing_button = btn |
| 93 | + break |
| 94 | + if cmds.shelfButton(btn, query=True, command=True) == shelf_command: # More robust check by command |
| 95 | + existing_button = btn |
| 96 | + break |
| 97 | + except RuntimeError: # Button might not be a shelfButton or might be deleted |
| 98 | + pass |
| 99 | + |
| 100 | + button_label = "Texture Rigger" # <<< YENİ ETİKETİNİZİ BURAYA GİRİN |
| 101 | + |
| 102 | + if existing_button: |
| 103 | + print(f"2D Texture Rigger Installer: Updating existing shelf button '{button_label}' on shelf '{shelf_name}'.") |
| 104 | + cmds.shelfButton( |
| 105 | + existing_button, |
| 106 | + edit=True, |
| 107 | + label=button_label, |
| 108 | + command=shelf_command, |
| 109 | + image=icon_path_for_shelf, |
| 110 | + imageOverlayLabel="TexRig", # Kısa overlay etiketi |
| 111 | + annotation="Runs the 2D Texture Rigger tool.", |
| 112 | + sourceType="python" |
| 113 | + ) |
| 114 | + else: |
| 115 | + print(f"2D Texture Rigger Installer: Creating new shelf button '{button_label}' on shelf '{shelf_name}'.") |
| 116 | + cmds.shelfButton( |
| 117 | + parent=shelf_name, |
| 118 | + label=button_label, |
| 119 | + command=shelf_command, |
| 120 | + image=icon_path_for_shelf, |
| 121 | + imageOverlayLabel="TexRig", # Kısa overlay etiketi |
| 122 | + annotation="Runs the 2D Texture Rigger tool.", |
| 123 | + sourceType="python" |
| 124 | + ) |
| 125 | + |
| 126 | + cmds.confirmDialog( |
| 127 | + title="2D Texture Rigger Installer", |
| 128 | + message=f"2D Texture Rigger shelf button installed/updated on shelf '{shelf_name}' successfully!", |
| 129 | + button="OK" |
| 130 | + ) |
| 131 | + |
| 132 | + except Exception as e: |
| 133 | + error_message = f"2D Texture Rigger Installer: Failed to create shelf button. Error: {e}" |
| 134 | + print(error_message) |
| 135 | + import traceback |
| 136 | + traceback.print_exc() |
| 137 | + cmds.warning(error_message) |
| 138 | + |
| 139 | +if __name__ == "__main__": |
| 140 | + # This block is executed when install.mel runs `exec(code_to_exec)` |
| 141 | + # TEXTURERIGGER_SCRIPT_PATH is globally defined by install.mel's python execution context. |
| 142 | + if 'TEXTURERIGGER_SCRIPT_PATH' in globals(): |
| 143 | + create_texture_rigger_shelf_button(globals()['TEXTURERIGGER_SCRIPT_PATH']) |
| 144 | + else: |
| 145 | + # This case should ideally not happen if install.mel is used. |
| 146 | + cmds.error("2D Texture Rigger Installer: TEXTURERIGGER_SCRIPT_PATH global variable not found. Cannot determine script path for installation.") |
| 147 | + |
0 commit comments