1

I am trying to create a triangulation (not sure if this is the right word) script? Basically what I am trying to achieve is create a polygon out of the selection of the locators (it must be 3 or more locators)

While I am able to do so, I am having this order of selection problem. Eg. In my scene, there are 4 locators - loc1, loc2, loc3, loc4

If I drag select from the loc1 upwards to loc4, instead of creating the polygon like in img01, it is created as shown in img02 ordered by the locator numbering sort of? Seems to me, it is sorting the selection based on its locator creation order, in which this is not what I am trying to do it

Am I missing something?

My code:

import maya.cmds as cmds

pts = []

cmds.select( cmds.listRelatives( type = 'locator', fullPath = True, allDescendents = True ) )
cmds.select( cmds.listRelatives( parent = True, fullPath = True ) )
sel = cmds.ls ( selection = True, type = 'transform' )

if not sel:
    cmds.warning( "Please select a locator / No locators in selection " )


for loc in sorted(sel):
    coords = cmds.xform (loc, query=True, worldSpace=True, pivots=True)[0:3]
    print coords
    pts.append(coords)

cmds.polyCreateFacet(p = pts)

img01 img02

yan
  • 631
  • 9
  • 30
  • 2
    possible duplicate of [Create non-intersecting polygon passing through all given points](http://stackoverflow.com/questions/14263284/create-non-intersecting-polygon-passing-through-all-given-points) – Basilevs Nov 03 '14 at 02:29

1 Answers1

0

A quick solution would be to give the order of selection to the user's discretion. To do this, in cmds.ls() use orderedSelection flag instead of just selection flag, and get rid of the sorted() method in the for loop definition. This way a polygon is created based on the user's selection order.

If you want to dig deeper into triangulation algorithms that will work no matter what, look at @Basilevs referred answer: Create non-intersecting polygon passing through all given points.

Community
  • 1
  • 1
kartikg3
  • 2,590
  • 1
  • 16
  • 23
  • Sorry for the late reply, I tried out the method as you have mentioned, it does not help in rectifying the issue on hand. It is still giving me the 'crossed' polygon as in my second picture. Nevertheless, thank you for the link you have provided. At the moment, doing such algorithms is slightly out of my reach :) – yan Dec 01 '14 at 07:46
  • The technique using orderedSelection just leaves it up to the user to select the locators in the right order. It cannot fail unless the user selected the locators randomly or just by an arbitrary drag select. – kartikg3 Dec 01 '14 at 12:41