Skip to content

Instantly share code, notes, and snippets.

@nrtkbb
Created May 17, 2015 13:58
Show Gist options
  • Save nrtkbb/819e16b0dca589a3dc44 to your computer and use it in GitHub Desktop.
Save nrtkbb/819e16b0dca589a3dc44 to your computer and use it in GitHub Desktop.
OpenMaya版 2つのメッシュのバーテックスから、それぞれ一番近いやつを検索してきてフィットさせるやつ
# -*- coding: utf-8 -*
import maya.OpenMaya as om
import pymel.core as pm
def is_mesh(trans):
if type(trans.getShape()) == pm.nodetypes.Mesh:
return True
return False
def check_selection_list(selection_list):
"""
:return: bool
"""
if 2 != len(selection_list):
raise Exception(u'Please select two mesh.')
(first, last) = selection_list[0], selection_list[1]
if is_mesh(first) and is_mesh(last):
return True
raise Exception(u'Please select two mesh.')
def get_points(index, selection):
"""
:param index: int
:param selection: om.MSelectionList
:return: om.MPointArray
"""
node = om.MDagPath()
component = om.MObject()
selection.getDagPath(index, node, component)
meshfn = om.MFnMesh(node)
points = om.MPointArray(meshfn.numVertices())
meshfn.getPoints(points, om.MSpace.kWorld)
return points
def get_variables(index, selection):
"""
:param index: int
:param selection: om.MSelectionList
:return: om.MFnMesh, om.MIntArray
"""
node = om.MDagPath()
component = om.MObject()
selection.getDagPath(index, node, component)
meshfn = om.MFnMesh(node)
vertex_count = om.MIntArray()
vertex_list = om.MIntArray()
meshfn.getVertices(vertex_count, vertex_list)
return meshfn, vertex_list
def get_nearest(point, search_points):
"""
:param point: om.MPoint
:param search_points: om.MPointArray
:return: om.MPoint
"""
if not search_points:
return
nearest = search_points[0]
distance = nearest.distanceTo(point)
if search_points.length() > 2:
for idx in xrange(1, search_points.length()):
p = search_points[idx]
new_distance = p.distanceTo(point)
if new_distance < distance:
nearest = p
distance = new_distance
return nearest
def execute():
if not check_selection_list(pm.ls(sl=True)):
return
selection = om.MSelectionList()
om.MGlobal.getActiveSelectionList(selection)
first_meshfn, first_vertex_list = get_variables(0, selection)
second_points = get_points(1, selection)
for first_vertex in first_vertex_list:
first_point = om.MPoint()
first_meshfn.getPoint(first_vertex, first_point, om.MSpace.kWorld)
nearest = get_nearest(first_point, second_points)
first_meshfn.setPoint(first_vertex, nearest, om.MSpace.kWorld)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment