Last active
July 6, 2016 21:12
-
-
Save nrtkbb/29109ec58fe9f37b97a6 to your computer and use it in GitHub Desktop.
[Maya] 選択したものをMeshの表面に吸着させるやつ(Meshは最後に選択する)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import pymel.core as pm | |
def divide_select_list(select_list): | |
movers = select_list[0:-1] | |
trans = select_list[-1] | |
mesh = trans.getShape() | |
if not type(mesh) == pm.nodetypes.Mesh: | |
raise Exception(u'Finally select the mesh.') | |
return movers, mesh | |
class NearestPointOnMesh(object): | |
pluginName = 'nearestPointOnMesh' | |
def __init__(self, mesh): | |
if not pm.system.pluginInfo(NearestPointOnMesh.pluginName, query=True, loaded=True): | |
pm.system.loadPlugin(NearestPointOnMesh.pluginName) | |
self.node = pm.createNode(NearestPointOnMesh.pluginName) | |
mesh.worldMesh.connect(self.node.inMesh) | |
def getNearestPoint(self, pos): | |
self.node.attr('inPosition').set(pos) | |
return self.node.attr('position').get() | |
def cleanup(self): | |
pm.delete(self.node) | |
self.node = None | |
def __enter__(self): | |
return self | |
def __exit__(self, type, value, trackback): | |
self.cleanup() | |
def is_movable_component(mover): | |
t = type(mover) | |
movables = [pm.MeshVertex, | |
pm.MeshEdge, | |
pm.MeshFace, | |
pm.NurbsCurveCV, | |
pm.NurbsCurveEP, | |
pm.NurbsSurfaceCV, | |
pm.SubdVertex, | |
pm.SubdEdge, | |
pm.SubdFace] | |
if t in movables: | |
return True | |
unmovables = [pm.MeshUV, | |
pm.MeshVertexFace, | |
pm.NurbsCurveParameter, | |
pm.NurbsSurfaceIsoparm, | |
pm.NurbsSurfaceFace, | |
pm.SubdUV] | |
if t in unmovables: | |
return False | |
return None | |
def do_move_component(component, nearest): | |
trans = pm.xform(component, query=True, worldSpace=True, translation=True) | |
pos = nearest.getNearestPoint(trans) | |
pm.xform(component, worldSpace=True, translation=(pos)) | |
def do_move(mover, nearest): | |
trans = pm.xform(mover, query=True, worldSpace=True, translation=True) | |
pivot = pm.xform(mover, query=True, worldSpace=True, rotatePivot=True) | |
pos = nearest.getNearestPoint(pivot) | |
pm.xform(mover, worldSpace=True, translation=(pos - pivot + trans)) | |
def moveNearestMesh(): | |
""" | |
move selection list on nearest point on mesh (select mesh on last) | |
How to use | |
1. select anythings. | |
2. select add transform what has meshShape. | |
3. do script. | |
import moveNearestMesh as mnm | |
mnm.moveNearestMesh() | |
:raise Exception: | |
""" | |
sels = pm.ls(sl=True) | |
if 2 > len(sels): | |
raise Exception(u'Please select two or more.') | |
movers, mesh = divide_select_list(sels) | |
with NearestPointOnMesh(mesh) as nearest: | |
for mover in movers: | |
movable = is_movable_component(mover) | |
if movable: | |
# mover is movable component. | |
for component in mover: | |
do_move_component(component, nearest) | |
elif not movable: | |
# mover is not movable. | |
pass | |
elif movable is None: | |
# mover is movable but not component. | |
do_move(mover, nearest) | |
pm.select(sels) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment