Skip to content

Instantly share code, notes, and snippets.

@natecraddock
Last active July 2, 2020 15:04
Show Gist options
  • Save natecraddock/64a0813690d1eb5a0a51c11e44c5a49f to your computer and use it in GitHub Desktop.
Save natecraddock/64a0813690d1eb5a0a51c11e44c5a49f to your computer and use it in GitHub Desktop.
"""
Example Usage
=============
A folder titled png_icons will be created in the current directory
Run as:
blender icons_geom.blend --background --python blender_icons_geom_png.py
"""
# This script writes out geometry-icons as png files
import bpy
def object_child_map(objects):
objects_children = {}
for ob in objects:
ob_parent = ob.parent
# Get the root.
if ob_parent is not None:
while ob_parent and ob_parent.parent:
ob_parent = ob_parent.parent
if ob_parent is not None:
objects_children.setdefault(ob_parent, []).append(ob)
for ob_all in objects_children.values():
ob_all.sort(key=lambda ob: ob.name)
return objects_children
def create_argparse():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"--output-dir",
dest="output_dir",
default=".",
type=str,
metavar="DIR",
required=False,
help="Directory to write icons to.",
)
parser.add_argument(
"--group",
dest="group",
default="",
type=str,
metavar="GROUP",
required=False,
help="Group name to export from (otherwise export all objects).",
)
return parser
def main():
import os
import sys
parser = create_argparse()
if "--" in sys.argv:
argv = sys.argv[sys.argv.index("--") + 1:]
else:
argv = []
args = parser.parse_args(argv)
objects = []
if args.group:
group = bpy.data.collections.get(args.group)
if group is None:
print(f"Group {args.group!r} not found!")
return
objects_source = group.objects
del group
else:
g = bpy.data.collections.get("Export")
objects_source = g.objects
for ob in objects_source:
# Skip non-mesh objects
if ob.type != 'MESH':
continue
name = ob.name
# Skip copies of objects
if name.rpartition(".")[2].isdigit():
continue
if not ob.data.vertex_colors:
print("Skipping:", name, "(no vertex colors)")
continue
objects.append((name, ob))
objects.sort(key=lambda a: a[0])
objects_children = object_child_map(bpy.data.objects)
camera = bpy.data.objects['Camera']
for name, ob in objects:
if ob.parent:
continue
# Move camera to object location
camera.location.x = ob.location.x
camera.location.y = ob.location.y
filename = os.path.join("png_icons", name + ".png")
bpy.context.scene.render.filepath = filename
bpy.ops.render.render(write_still=True)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment