Created
January 22, 2016 07:34
-
-
Save michaeltrainor/11dd891ed05e82bd3c1b to your computer and use it in GitHub Desktop.
Spot Locator WIP
This file contains 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
import pymel.core as pm | |
selection = pm.ls(sl=True) | |
for index, node in enumerate(selection): | |
position = None | |
if isinstance(node, pm.Component): | |
if isinstance(node, pm.MeshVertex): | |
position = node.getPosition(space="world") | |
elif isinstance(node, (pm.MeshEdge, pm.MeshFace)): | |
def get_average_position(n): | |
x = float(sum([point.x for point in n]))/len(n) if len(n) > 0 else float("nan") | |
y = float(sum([point.y for point in n]))/len(n) if len(n) > 0 else float("nan") | |
z = float(sum([point.z for point in n]))/len(n) if len(n) > 0 else float("nan") | |
return [x, y, z] | |
count = node.numVertices() if isinstance(node, pm.MeshFace) else 2 | |
points = [node.getPoint(index, space="world") for index in range(count)] | |
position = get_average_position(points) | |
if isinstance(node, pm.nt.Transform): | |
position = node.getTranslation(space="world") | |
if position: | |
locator = pm.spaceLocator() | |
locator.rename("{0}_spot_locator{1}".format(node.split("Shape")[0], index)) | |
locator.setPosition(position) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment