-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.py
More file actions
143 lines (120 loc) · 4.23 KB
/
Copy pathbrowser.py
File metadata and controls
143 lines (120 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from PySide2 import QtCore, QtWidgets, QtWebEngineWidgets, QtWebChannel
import json
import os
from pathlib import Path
from typing import List
from maya import cmds
import maya.OpenMayaUI as omui
from shiboken2 import wrapInstance
SOURCE_FOLDER = Path(r"\Users\bertp\Desktop\maya-webui-basic-example")
class Backend(QtCore.QObject):
@QtCore.Slot(int)
def openAsset(self, asset_id: int) -> None:
print(f"open Asset id: {asset_id}")
sg_asset = get_asset_from_id(asset_id)
cmds.file(
sg_asset.get("latestVersionPath"),
open=True,
)
@QtCore.Slot(int)
def importAsset(self, asset_id: int) -> None:
print(f"import Asset id: {asset_id}")
sg_asset = get_asset_from_id(asset_id)
cmds.file(
sg_asset.get("latestVersionPath"),
i=True,
)
@QtCore.Slot(int)
def referenceAsset(self, asset_id: int) -> None:
print(f"reference Asset id: {asset_id}")
sg_asset = get_asset_from_id(asset_id)
cmds.file(
sg_asset.get("latestVersionPath"),
reference=True,
)
@QtCore.Slot(result=str)
def getAssetList(self) -> str:
asset_list = get_all_assets()
return json.dumps(asset_list)
class MayaBrowser(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MayaBrowser, self).__init__(parent)
# NOTE: backend and channel MUST be a class or global variables
self.backend = Backend()
self.channel = QtWebChannel.QWebChannel()
self.channel.registerObject("backend", self.backend)
view = QtWebEngineWidgets.QWebEngineView()
view.page().setWebChannel(self.channel)
filepath = Path.joinpath(SOURCE_FOLDER, "static/index.html")
url = QtCore.QUrl.fromLocalFile(str(filepath))
view.load(url)
self.setCentralWidget(view)
# NOTE: this fully stops the instance from running after the window is closed
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
def get_asset_from_id(asset_id: int) -> dict:
# your shotgrid api connection here
return dict(
id=1,
name="asset01",
thumbnailPath=r"images\thumbnail.png",
latestVersionPath=Path.joinpath(SOURCE_FOLDER, "assets/example_asset.ma"),
latestVersionNumber=1,
lastEditedDate="08-03-1999",
status="apr",
)
def get_all_assets() -> List[dict]:
# your shotgrid api connection here
return [
dict(
id=1,
name="asset01",
thumbnailPath=r"images\thumbnail.png",
latestVersionPath=os.path.join(SOURCE_FOLDER, "assets/example_asset.ma"),
latestVersionNumber=1,
lastEditedDate="14-03-2019",
status="apr",
),
dict(
id=2,
name="asset02",
thumbnailPath=r"images\thumbnail.png",
latestVersionPath=os.path.join(SOURCE_FOLDER, "assets/example_asset.ma"),
latestVersionNumber=1,
lastEditedDate="14-03-2019",
status="ip",
),
dict(
id=3,
name="asset03",
thumbnailPath=r"images\thumbnail.png",
latestVersionPath=os.path.join(SOURCE_FOLDER, "assets/example_asset.ma"),
latestVersionNumber=1,
lastEditedDate="14-03-2019",
status="ip",
),
dict(
id=4,
name="asset04",
thumbnailPath=r"images\thumbnail.png",
latestVersionPath=os.path.join(SOURCE_FOLDER, "assets/example_asset.ma"),
latestVersionNumber=1,
lastEditedDate="14-03-2019",
status="ip",
),
dict(
id=5,
name="asset05",
thumbnailPath=r"images\thumbnail.png",
latestVersionPath=os.path.join(SOURCE_FOLDER, "assets/example_asset.ma"),
latestVersionNumber=1,
lastEditedDate="14-03-2019",
status="ip",
),
]
def get_maya_main_window():
main_window_ptr = omui.MQtUtil.mainWindow()
return wrapInstance(int(main_window_ptr), QtWidgets.QMainWindow)
if __name__ == "__main__":
app = get_maya_main_window()
browser = MayaBrowser(parent=app)
browser.show()