0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

RRname

Last updated at Posted at 2024-06-05

# -*- coding: utf-8 -*-
from maya import OpenMayaUI
from PySide2 import QtWidgets, QtCore
import shiboken2 as shiboken
import maya.cmds as cmds

ptr = OpenMayaUI.MQtUtil.mainWindow()
parent = shiboken.wrapInstance(int(ptr), QtWidgets.QMainWindow)

class RenameTool(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(RenameTool, self).__init__(parent)
        self.setWindowTitle("RRname")
        self.resize(400, 200)

        self.central_widget = QtWidgets.QWidget()
        self.setCentralWidget(self.central_widget)

        self.main_layout = QtWidgets.QVBoxLayout(self.central_widget)

        # TabWidgetを作成
        self.tab_widget = QtWidgets.QTabWidget()
        self.main_layout.addWidget(self.tab_widget)

        # タブ1: オブジェクト名称変更
        self.rename_tab = QtWidgets.QWidget()
        self.rename_layout = QtWidgets.QVBoxLayout(self.rename_tab)
        self.text_field = QtWidgets.QLineEdit(self.rename_tab)
        self.text_field.setPlaceholderText("new name")
        self.rename_button = QtWidgets.QPushButton("change name", self.rename_tab)
        self.rename_button.clicked.connect(self.rename_selected_objects)
        self.rename_layout.addWidget(self.text_field)
        self.rename_layout.addWidget(self.rename_button)
        self.tab_widget.addTab(self.rename_tab, "object name")

        # タブ2: 設定
        self.settings_tab = QtWidgets.QWidget()
        self.settings_layout = QtWidgets.QVBoxLayout(self.settings_tab)
        self.number_checkbox = QtWidgets.QCheckBox("末尾に番号を付ける", self.settings_tab)
        self.number_checkbox.setChecked(True)
        self.top_layer_checkbox = QtWidgets.QCheckBox("トップレイヤーから適用", self.settings_tab)
        self.top_layer_checkbox.setChecked(False)
        self.settings_layout.addWidget(self.number_checkbox)
        self.settings_layout.addWidget(self.top_layer_checkbox)
        self.tab_widget.addTab(self.settings_tab, "設定")

        # スタイルシートの設定
        self.setStyleSheet(
            '''
            QGroupBox {
                border: 2px solid #256d7b;
                border-radius: 6px;
                margin-top: 1em;
                font-size: 12px;
            }
            QGroupBox::title {
                color: #fff;
                background-color: #256d7b;
                subcontrol-origin: margin;
                padding: 0.5em 0.7em;
                border-top-left-radius: 6px;
                border-top-right-radius: 6px;
            }
            QTabWidget::pane {
                border: 1px solid #256d7b;
                border-radius: 6px;
            }
            QTabWidget::tab-bar {
                alignment: center;
            }
            QTabBar::tab {
                background: #256d7b;
                color: #fff;
                padding: 5px 10px;
                border-top-left-radius: 6px;
                border-top-right-radius: 6px;
                margin-right: 1px;
            }
            QTabBar::tab:selected {
                background: #1e555f;
            }
            '''
        )

    def rename_selected_objects(self):
        base_name = self.text_field.text()
        add_number = self.number_checkbox.isChecked()
        top_layer_only = self.top_layer_checkbox.isChecked()

        selected_objects = cmds.ls(selection=True)
        if not selected_objects:
            cmds.warning(u"オブジェクトが選択されていません。")
            return

        if top_layer_only:
            new_selected_objects = []
            for obj in selected_objects:
                if cmds.listRelatives(obj, parent=True) is None:
                    new_selected_objects.append(obj)
                    descendants = cmds.listRelatives(obj, allDescendents=True) or []
                    new_selected_objects.extend(descendants)
            selected_objects = new_selected_objects

        selected_objects = sorted(selected_objects, key=lambda x: cmds.ls(x, long=True)[0])

        for index, obj in enumerate(selected_objects):
            new_name = base_name
            if add_number:
                new_name = f"{base_name}{index + 1}"
            cmds.rename(obj, new_name)

        cmds.inform(u"選択されたオブジェクトの名前を変更しました。")

def main():
    global rename_tool
    try:
        rename_tool.close()  # 既存のウィンドウを閉じる
    except:
        pass
    rename_tool = RenameTool(parent)
    rename_tool.show()

main()


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?