selecting multiple consecutive faces with api

37 views
Skip to first unread message

s...@weacceptyou.com

unread,
Nov 9, 2015, 6:21:57 PM11/9/15
to Python Programming for Autodesk Maya
i have a piece of code that selects a long list of faces on a mesh and deletes them

###########################################
mesh = ls('handSurface1')[0].getShape()
faceIndexes = om.MIntArray()

map(faceIndexes.append, [55,68,69,70,72,75,76,77,78,79,80,81,82,84,85,86,87,88,89,90,91,92,93,254])

makeMayaComponent()

delete()
###########################################

however if there are groups of consecutive faces i need to select how can i add these to the list so that i dont have to write each individual face.
like this:

map(faceIndexes.append, [55,68:70,72,75:82,84:93,254])

#however this is invalid syntax..

thanks,
Sam

Justin Israel

unread,
Nov 9, 2015, 6:41:28 PM11/9/15
to python_in...@googlegroups.com
Are you trying to populate your MIntArray all in one line? Normally it is bad practice to use stuff like map, list comprehension, and the like, just for its side effects. Because basically you are telling python to build a big list of None values and toss it away, so that you can just get the side effect of applying the function. So if you are trying to be able to use that pattern, to achieve it all in one line... you could use range()
map(faceIndexes.append, [55,68,69,70,72] + range(75,93+1) + [254])

Or:
import itertools

map(faceIndexes.append, itertools.chain([55,68,69,70,72], xrange(75,93+1), [254]))

Or you could use a loop if you didn't want to generate the garbage None list:
import itertools
for val in itertools.chain([55,68,69,70,72], xrange(75,93+1), [254]):
    faceIndexes.append(val)



--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/ff560c31-f903-42c9-95d8-d05a8d6fffee%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

e955...@gmail.com

unread,
Nov 10, 2015, 4:00:02 AM11/10/15
to Python Programming for Autodesk Maya
aah ok thanks Justin, i don't quite understand why they would be classed as None values because they are integers right? or maybe they don't have a type when mapped.

but yes this is working nicely, thank you

Sam

Justin Israel

unread,
Nov 10, 2015, 4:41:40 AM11/10/15
to python_in...@googlegroups.com
On Tue, Nov 10, 2015 at 9:59 PM <e955...@gmail.com> wrote:
aah ok thanks Justin, i don't quite understand why they would be classed as None values because they are integers right? or maybe they don't have a type when mapped.

map() is a functional programming construct, meant apply the given function to each item in a sequence, and return the results. Example of realistic usage:
nums = range(5)
print nums
# [0,1,2,3,4]

doubled = map(lambda i: i*2, nums)
print doubled
# [0,2,4,6,8]
As you can see, you start with a list of numbers, and apply a function that multiplies the value by 2. map() then gives you back a new list, with the mapped values. 

But look at what happens when you use it for side effects, on something like list.append(), which adds an element to a list but has no return value:
aList = []
results = map(aList.append, [1,2,3,4,5])
print aList
# [1, 2, 3, 4, 5]
print results
# [None, None, None, None, None]
So you see, that while you have managed to append each value to your original list, map() generate a result for you anyways. It is a list of return values from each call to append(). That is just a big list of None values. If you were to do a map() on, say, 10k items, you generate a garbage list of 10k None values, just so you can have the side effect of whatever your function does. Better to just loop and append, in this case.

 

but yes this is working nicely, thank you

Sam

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

s...@weacceptyou.com

unread,
Nov 10, 2015, 7:33:11 AM11/10/15
to Python Programming for Autodesk Maya
Oh ok, i get it now. thanks Justin. Would that garbage list be bad because it takes up memory or compute time or just bad programming practice?

thanks, last time,
Sam;)

Justin Israel

unread,
Nov 10, 2015, 12:39:15 PM11/10/15
to Python Programming for Autodesk Maya


On Wed, 11 Nov 2015 1:33 AM  <s...@weacceptyou.com> wrote:

Oh ok, i get it now. thanks Justin. Would that garbage list be bad because it takes up memory or compute time or just bad programming practice?

a, b, and c  :-)

If map() didn't return anything, then it would be fine. Also in python3, map() is now a generator (like itertools.imap). So your usage would end up breaking. The generator returned from that version of map() would have to be looped over, completely, to have it apply the function to every item and get your side effect. But if you throw away the resulting generator, nothing happens

http://xahlee.info/python/python3_map_with_side_effect.html

thanks, last time,
Sam;)

--


You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/0f7b5430-dfcb-4285-8974-e575e9475311%40googlegroups.com.

s...@weacceptyou.com

unread,
Nov 10, 2015, 1:56:11 PM11/10/15
to Python Programming for Autodesk Maya
i see, got it,

thanks amuch

Sa
Reply all
Reply to author
Forward
0 new messages