# Pretty print dir() & collection.keys() &c

**URL:** <https://blenderartists.org/t/pretty-print-dir-collection-keys-c/1652436>\
**Category:** Released Add-ons and Extensions\
**Tags:** scripting, free\
**Created:** [September 9, 2026, 9:36pm UTC](https://blenderartists.org/t/pretty-print-dir-collection-keys-c/1652436 "2026-09-09T21:36:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Duthomhas](https://blenderartists.org/user_avatar/blenderartists.org/duthomhas/32/1582_2.png) [@Duthomhas](https://blenderartists.org/u/Duthomhas)\
**Post date:** [September 9, 2026, 9:36pm UTC](https://blenderartists.org/t/pretty-print-dir-collection-keys-c/1652436/1 "2026-09-09T21:36:24Z")

</div>

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.

```python
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:

```python
>>> 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!

---

<div class="post-metadata">

**Author:** ![Duthomhas](https://blenderartists.org/user_avatar/blenderartists.org/duthomhas/32/1582_2.png) [@Duthomhas](https://blenderartists.org/u/Duthomhas)\
**Post date:** [October 20, 2026, 2:59pm UTC](https://blenderartists.org/t/pretty-print-dir-collection-keys-c/1652436/2 "2026-10-20T14:59:40Z")

</div>

Updated. The `K()` function (to print keys) was broken — I had added it when creating this post because I wanted it, but now that I put it to use it showed that Blender doesn’t report `.keys()` properly. It _does_ report `.items()` properly, though.

There is still a bit of an error here and there with the width calculation for `pp()`, and you´ll get some obnoxious wrapping. This has always been an issue… if it happens, just resize your window a little larger or invoke with fewer columns. Perhaps I’ll mess with it again some time, but for now it generally works well enough…
