I just updated a script I use all the time when exploring Blender while scripting and figured I’d share. It’s just a basic pretty-printer for lists of things.
100% free to use as you wish.
import math, shutil, blf
def pp(items, width=None):
if not width:
if bpy.context.space_data.type == 'CONSOLE':
M = blf.dimensions(1, 'M')[0]
width = (bpy.context.region.width - M) // M - 1
else:
width = shutil.get_terminal_size((80, 34))[0] - 1
#
item_widths = [len(item) for item in items]
#
def calc_widths(ncols):
widths = []
nrows = int(math.ceil(len(item_widths) / ncols))
for n in range(ncols):
widths.append(max(item_widths[nrows*n : nrows*(n+1)] + [0]))
return widths
#
def total_width(widths):
return sum(widths) + 2*(len(widths)-1)
#
if total_width(item_widths) < width:
ncols = len(item_widths)
else:
ncols = 1
while total_width(calc_widths(ncols+1)) < width:
ncols += 1
#
nrows = int(math.ceil(len(items) / ncols))
col_widths = calc_widths(ncols)
#
for row in range(nrows):
for col in range(ncols):
index = col * nrows + row
if index < len(items):
print(items[index].ljust(col_widths[col]+2), end='')
print()
def D(item):
pp(dir(item))
def K(collection):
if len(collection.items()):
pp([key for key,value in collection.items()])
Example Usage
Open Blender and switch to the “Scripting” workspace. Copy & Paste the above code into the Python Console. (For this example, don’t resize it first!) You can then use it meaningfully:
>>> D(bpy)
__all__ __doc__ __name__ __spec__ data path utils
__builtins__ __file__ __package__ app msgbus props
__cached__ __loader__ __path__ context ops types
>>> K(bpy.data.objects)
Camera Cube Light
>>> │
The D() function frobs it for pretty-printing the dir() of an object.
If that object should have a member named keys (because it is a collection of some type), use the K() function.
Makes exploring the bpy interface so much easier on the eyes.
Enjoy!