1

In maya, I want to make an OpenMaya MSelectionList (api version 2.0) with more than one item in it... I've only been able to fill it with the add method like so:

import maya.api.OpenMaya as om

selList = om.MSelectionList()
selList.add('node1')
selList.add('node2')
selList.add('node3')

That's ok for populating it with just a few items, but it's tedious if you have more... I'm wondering if there's a way to do something more like this:

import maya.api.OpenMaya as om

selList = om.MSelectionList(['node1', 'node2', 'node3'])

I could write my own function to create an empty MSelectionList, loop through a list, and add them and then return it; I'm just wondering I have completely looked over something obvious? From what I can tell on the docs, you can only create an empty MSelectionList, or create one by passing in another MSelectionList (to basically duplicate it).

If this can't really be done inherently in the class, does anyone have an idea why it was implemented this way?

silent_sight
  • 492
  • 1
  • 8
  • 16

1 Answers1

2

The MSelectionList is ultimately a wrapper around a C++ list of object pointers (the Maya api is unusual in that uses diffeent function sets to work on different aspects of an object, rather than the more familiar use of a classic inheritance tree).

Implementing variadic functions in C++ is not trivial (especially not back in the 90s when the Maya API was designed. I suspect nobody felt it was worth the time for what's essentially syntactic sugar.

sl = om.MSelectionList()
for node in nodelist:
    sl.add(n0de)

or

sl = om.MSelectionList()
[sl.add(n0de) for node in nodelist]

although I would not recommend the shorter version, which produces an list of meaningless Nones as a side effect

theodox
  • 12,028
  • 3
  • 23
  • 36
  • yeah... I suspected the only way to do it was through a loop... bleh - looks like I'll be keeping my function which does that. But, hey, thanks for the extra explanation of why it is this way (hurray for limitations of the 90's)! – silent_sight Aug 10 '17 at 16:36