generated from hannesdelbeke/maya-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snap_closest_uv_plugin.py
162 lines (112 loc) · 4.34 KB
/
snap_closest_uv_plugin.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# -*- coding: utf-8 -*-
""" Snap selected UVs to closest target UVs
"""
from PySide2 import QtWidgets, QtCore
from maya.api import OpenMaya
from maya import OpenMayaUI
from maya import cmds
import shiboken2
import time
import re
import kdtree # kdtree: Copyright (c) Stefan Kögl <stefan@skoegl.net> https://github.com/stefankoegl/kdtree
def createTree(mesh):
# type: (OpenMaya.MFnMesh) -> kdtree.KDNode
uArray, vArray = mesh.getUVs()
numUVs = mesh.numUVs()
points = [(uArray[i], vArray[i]) for i in range(numUVs)]
tree = kdtree.create(points, dimensions=2)
return tree
def getMesh(path):
# type: (str) -> OpenMaya.MFnMesh
sel = OpenMaya.MSelectionList()
sel.add(path)
dagPath = sel.getDagPath(0)
mesh = OpenMaya.MFnMesh(dagPath)
return mesh
def getMayaWindow():
ptr = OpenMayaUI.MQtUtil.mainWindow()
return shiboken2.wrapInstance(int(ptr), QtWidgets.QMainWindow)
class Window(QtWidgets.QWidget):
def __init__(self, parent=None, *args, **kwargs):
super(Window, self).__init__(parent, *args, **kwargs)
self.setWindowTitle("UV snap")
self.setWindowFlags(QtCore.Qt.Window)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.LE = QtWidgets.QLineEdit()
self.LE.setEnabled(False)
self.setBtn = QtWidgets.QPushButton("Set")
self.setBtn.clicked.connect(self.setObject)
self.snapBtn = QtWidgets.QPushButton("Snap")
self.snapBtn.clicked.connect(self.snapIt)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.LE)
layout.addWidget(self.setBtn)
layout.addWidget(self.snapBtn)
self.setLayout(layout)
def setObject(self):
sel = cmds.ls(sl=True, fl=True, long=True)
if sel:
self.LE.setText(sel[0])
def snapIt(self):
sel = cmds.ls(sl=True, fl=True)
if not sel:
cmds.warning("Nothing is selected")
return
componentType = sel[0].split(".")[-1][0:3]
if not componentType == "map":
cmds.warning("UVs are not selected")
return
snapTargetMeshPath = self.LE.text()
sourceMesh = getMesh(snapTargetMeshPath)
sourceTree = createTree(sourceMesh)
sel = OpenMaya.MGlobal.getActiveSelectionList()
dagPath = sel.getDagPath(0)
mesh = OpenMaya.MFnMesh(dagPath)
uArray, vArray = mesh.getUVs()
sel = cmds.ls(sl=True, fl=True, long=True)
for i in sel:
index = int(re.findall(r'\d+', i)[-1])
uv = cmds.polyEditUV(i, q=True)
nn = sourceTree.search_nn(uv)
data = nn[0].data
uArray[index] = data[0]
vArray[index] = data[1]
mesh.setUVs(uArray, vArray)
def show(*args):
t = time.time()
w = Window(getMayaWindow())
w.show()
et = time.time() - t
print(et)
return w
if __name__ == "__main__":
show()
import sys
import maya.api.OpenMaya as om
import maya.cmds as cmds
MENU_NAME = "ToolsMenu" # no spaces in names, use CamelCase
MENU_LABEL = "Tools" # spaces are fine in labels
MENU_ENTRY_LABEL = "Snap to closest UV Tool"
MENU_PARENT = "MayaWindow" # do not change
def maya_useNewAPI(): # noqa
pass # dummy method to tell Maya this plugin uses Maya Python API 2.0
# =============================== Menu ===========================================
def loadMenu():
if not cmds.menu(f"{MENU_PARENT}|{MENU_NAME}", exists=True):
cmds.menu(MENU_NAME, label=MENU_LABEL, parent=MENU_PARENT)
cmds.menuItem(label=MENU_ENTRY_LABEL, command=show, parent=MENU_NAME)
def unloadMenuItem():
if cmds.menu(f"{MENU_PARENT}|{MENU_NAME}", exists=True):
menu_long_name = f"{MENU_PARENT}|{MENU_NAME}"
menu_item_long_name = f"{menu_long_name}|{MENU_ENTRY_LABEL}"
# Check if the menu item exists; if it does, delete it
if cmds.menuItem(menu_item_long_name, exists=True):
cmds.deleteUI(menu_item_long_name, menuItem=True)
# Check if the menu is now empty; if it is, delete the menu
if not cmds.menu(menu_long_name, query=True, itemArray=True):
cmds.deleteUI(menu_long_name, menu=True)
# =============================== Plugin (un)load ===========================================
def initializePlugin(plugin):
loadMenu()
def uninitializePlugin(plugin):
unloadMenuItem()