I want to know myself path with maya python like “file”.
Following code, getScriptPath() will do this.
This code saved to /document/maya/2020/script/working/getScriptPath.py and called from shelf.
from PySide2 import QtCore
from PySide2 import QtWidgets
from shiboken2 import wrapInstance
import os
import sys
import maya.OpenMayaUI as omui
def maya_main_window():
"""
Return the Maya main window widget as a Python object
"""
main_window_ptr = omui.MQtUtil.mainWindow()
return wrapInstance(long(main_window_ptr), QtWidgets.QWidget)
class TestDialog(QtWidgets.QDialog):
def __init__(self, parent=maya_main_window()):
super(TestDialog, self).__init__(parent)
self.setWindowTitle("Test Dialog")
self.setMinimumWidth(200)
# Remove the ? from the dialog on Windows
self.setWindowFlags(self.windowFlags() ^ QtCore.Qt.WindowContextHelpButtonHint)
self.create_testbtn()
def create_testbtn(self):
main_lo = QtWidgets.QVBoxLayout(self)
test_button = QtWidgets.QPushButton("Test it")
main_lo.addWidget(test_button)
test_button.clicked.connect(self.getScriptPath)
def getScriptPath(self):
print os.path.abspath(__file__)
if __name__ == "__main__":
d = TestDialog()
d.show()
I tried…
- os.path.abspath(file)
- os.path.dirname(sys.argv[0])
- os.getcwd()
These seems to returned maya’s path.
This is my first post, so if there’s anything I’m missing, I’d appreciate it if you could let me know.
Thanks to your help.