Python – Some useful list-of-tuples operations

Language:
Python 3.7

Sometimes we need to sort a list of tuples (or other list-type containers) according to their internal values.
For instance, you might have a list of tuples representing X,Y coordinates and need to sort that list according to the Y coordinate of the locations.
Using the sorted function with a lambda function as the supplied key argument can do that.
In this case, each tuple element is fed to the lambda function as the argument x and the function simply returns x[1], which is the second (Y) value within the tuple as the key for the sorting comparison:

sorted(my_tuple_list, key=lambda x: x[1])

Again, in a list of tuples (or other list-type containers), Sometimes we need to find the index of the first occurrence of a tuple that has a specific value as one of its elements.
This example will return the index of the first occurrence in the list of a tuple with the value 8 as its second element:

list(zip(*my_tuple_list))[1].index(8)

The zip function “decouples” the tuples into two lists one containing the tuples first element values and the second containing the tuples second element values.
The zip object is fed into a list function to be converted into a subscript-able list containing the 2 new lists, so now we can use the [1] index to access the list containing only the original tuples’s second elements, and use the index function to get the index of first occurrence of value 8.
> Note that the * operator isn’t a C like “pointer” operator..
Its a Python unpack operator needed to unpack the list of tuples to separate tuple arguments for the zip function.

Python – Listing the files in a directory

Language:
Python 2.7

The following example uses functions from the os library to list the files in a given directory:

from os import listdir
from os.path import isfile, join
dir = "D:\\"
files = [f for f in listdir(dir) if isfile(join(dir, f))]
for f in files:
	print join(dir, f)

In the next example the script has been modified to list only files with the name extension ‘jpg’:

from os import listdir
from os.path import isfile, join
dir = "D:\\"
files = [f for f in listdir(dir) if isfile(join(dir, f)) and f[-3:]=='jpg']
for f in files:
	print join(dir, f)

 

* note that when copying and pasting a script from this example, the indentation may not be pasted correctly.

Python for 3ds max – list scene objects

Software:
3ds max 2019

Iterating through a list of all of an object’s hierarchy or all of the objects in the scene is a very common requirement for scripted tools and utilities in 3D animation software.
Coming from a MaxScript background, I was used to just have to write the word ‘objects‘ to reference a list of all of the objects in the scene.

Unless I’m missing something obvious,
There isn’t a shortcut like this available in the 3ds max Python API.

In order to list all the objects in the scene you need to use a recursive function that will return a list of all of a node’s children, and children’s children, etc.

In the following example, the function ‘list_children()’ will return a list all of the objects in the supplied node’s hierarchy, and when given the scene’s root node via MaxPlus.Core.GetRootNode() it will return a list of all the objects in the scene.
Wrapping ‘list_children()’ within the ‘scene_objects()’ function, provides a convenient way get a list of all the objects in the scene just by calling ‘scene_objects()’.

def scene_objects():
    def list_children(node):
       list = []
       for c in node.Children:
           list.append(c)
           list = list + list_children(c)
       return list
    return list_children(MaxPlus.Core.GetRootNode())


for o in scene_objects():
    print o.Name

* note that when copying and pasting a script from this example, the indentation may not be pasted correctly.

Related:
https://odederell3d.blog/2020/02/11/python-for-3ds-max-select-objects-of-type/