A new version of Cineversity has been launched. This legacy site and its tutorials will remain accessible for a limited transition period

Visit the New Cineversity


 ·   Wiki Home
 ·   Categories
 ·   Title List
 ·   Uncategorized Pages
 ·   Random Page
 ·   Recent Changes
 ·   RSS
 ·   Atom
 ·   What Links Here



Create or Find Page:


View Python: Getting Selected Point Index

Getting the ID’s of selected points


Something that’s incredibly useful is getting the position of a point, or getting selected points. The following function will return a list of ID’s of selected points.


def GetSelectedPointIDs(PointObj):
    if PointObj is None:
        c4d.gui.MessageDialog("Please Select one Point Based Object")
    elif not PointObj.CheckType(c4d.Opoint):
        c4d.gui.MessageDialog("Please Select one Point Based Object")
        for index, selected in enumerate(bs.GetAll(maxEl)):
            if not selected: continue
        return listy


Now for some explanation. This is a custom function. This DOES NOT go into the “def main():” function. It belongs outside of it. See the image below to see what I mean.


So the way this works, is in your main() function you can call this function. From that image you can see on line 28 it says “List=GetSelectedPointIDs(op)” This means I’m running the custom function, and the object I’m passing is op(which is the selected object by default). Because this function returns a list, I’m creating a variable called “List” that will receive the list that the function returns.

As for the function itself, Line 7-12 are some error checks. Basically it first checks to make sure that the object you pass into the function(in line 28 this is op) is not a None type. So it’s first checking to make sure there’s something there. Then, because this is looking at selected points, it has to be a point object. So line 10 does a check of the type of object that is passed, and says if it’s NOT a point object, then display a message saying you have to select 1 point object.

Lines 14-23 are the meat and potatoes of the script. First we create an empty list in line 14. It needs to be initialized first so we know it’s a list and can add to it later. So to do this you just do listName=[]. Line 15 creates a variable called maxEl(short for MaxElements) and it gets set to be an integer that equals the total number of points in an object. Line 16 creates a BaseSelect object called bs, that is made from the GetPointS() function which returns the selected points through a base select object. Manipulating Base Selects is a little bit odd, so we want to take the information from the Base Select and put it into a list instead because lists are better and easier to mess around with.

So then in Line 17 there’s a for loop started. It is saying For each element in bs.GetAll(maxEl) assign it to the variable selected and give it an ID called index. This line is very powerful throughout python. It’s like using a While loop but without the risk(or frequent risk at least) of creating an infinite loop. Then we do a check to see if selected is 0 or 1(true or false). The GetPointS() function gave us the selected points set to 1, and all unselected points are 0. So the if check says at this index(point number) is it 0 or 1? If it’s 0, it says “continue” which means just move on to the next selected/index. Line 20 says else which is essientially saying “if selected==1” but much neater, it then takes use to Line 21 which says listy.append(index). What this is doing is using a python function for lists called append. This basically just adds whatevery you set between the parenthesis’ at last position of the list. Now, since our list was empty at first, the first time we run listy.append(index) it will take the index(which is just the point’s id, same as what you’d see in the Structure menu) and add it to the first spot since there’s nothing else. The next time it runs(if there’s more than 1 selected point that is) the append will put it in slot 2, then slot 3, etc. etc.

The last bit for this function is line 23 which says return listy which means ok, now return this list. When you do List=GetSelectedPointIDs(op) on line 28, it’s basically saying List=listy, but doing all that fancy checking and stuff as well.

Then to see which point ID’s are selected you can just print List or do:

for x in List:

  print x

Hope this helps.