Created
September 28, 2023 11:42
-
-
Save xdl/3aafa86d8b69f31845139924896233ba to your computer and use it in GitHub Desktop.
Blender addon that renders all the objects in the 'solo' Collection individually (and omits them from the overall render)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bl_info = { | |
"name": "Render Solo ", | |
"author": "Lionbark", | |
"version": (1), | |
"blender": (3, 6, 0), | |
"location": "3D Viewport > Side panel", | |
"description": "Render objects in the solo collection individually", | |
"warning": "", | |
"doc_url": "", | |
"category": "Render", | |
} | |
import bpy | |
import os | |
from pathlib import Path | |
########## Render solo ########## | |
def render_solo(context): | |
blend_file_directory = os.path.dirname(bpy.data.filepath) | |
basename = os.path.basename(bpy.data.filepath) | |
name_no_ext = basename.split(".")[0] | |
output_directory_name = name_no_ext + "_render_solo" | |
output_directory = os.path.join(blend_file_directory, output_directory_name) | |
output_directory_path = Path(output_directory) | |
if not output_directory_path.is_dir(): | |
output_directory_path.mkdir(parents=True, exist_ok=True) | |
collection = bpy.data.collections.get("solo") | |
# Render the image with all objects hidden | |
for obj in collection.objects: | |
obj.hide_render = True | |
render_sans_solo_filename = name_no_ext + "_sans_solo.png" | |
bpy.context.scene.render.filepath = os.path.join(output_directory, render_sans_solo_filename) | |
bpy.ops.render.render(write_still=True) | |
# Hide all other collections | |
for c in bpy.data.collections: | |
if c.name != "solo": | |
c.hide_render = True | |
c.hide_viewport = True | |
else: | |
c.hide_render = False | |
c.hide_viewport = False | |
# Unhide and render each object individually | |
for obj in collection.objects: | |
obj.hide_render = False | |
obj.hide_viewport = False | |
bpy.context.view_layer.objects.active = obj | |
render_solo_filename = obj.name + ".png" | |
bpy.context.scene.render.filepath = os.path.join(output_directory, render_solo_filename) | |
bpy.ops.render.render(write_still=True) | |
obj.hide_render = True | |
obj.hide_viewport = True | |
# Cleanup: unhide everything | |
for c in bpy.data.collections: | |
c.hide_render = False | |
c.hide_viewport = False | |
class RENDER_SOLO_render_solo(bpy.types.Operator): | |
"""Renders objects in the 'solo' collection individually""" | |
bl_idname = "render.render_solo" | |
bl_label = "Render Solo" | |
bl_options = {'REGISTER',} | |
def execute(self, context): | |
render_solo(context) | |
return {'FINISHED'} | |
class RENDER_SOLO_PT_render_solo_panel(bpy.types.Panel): | |
"""Creates a Panel for Rendering Solo in the UI Panels""" | |
bl_label = "Render Solo" | |
bl_idname = "RENDER_SOLO_PT_render_solo_panel" | |
bl_space_type = 'VIEW_3D' | |
bl_region_type = 'UI' | |
bl_category = "Render Solo" | |
def draw(self, context): | |
layout = self.layout | |
scene = context.scene | |
box = layout.box() | |
row = box.row() | |
row.alignment = 'CENTER' | |
# RESTRICT_RENDER_OFF: Camera icon | |
row.label(text="Render solo collection", icon = "RESTRICT_RENDER_OFF") | |
row = box.row() | |
row = box.row() | |
row.scale_y = 1.5 | |
row.operator("render.render_solo") | |
row = box.row() | |
row = layout.row() | |
def register(): | |
bpy.utils.register_class(RENDER_SOLO_render_solo) | |
bpy.utils.register_class(RENDER_SOLO_PT_render_solo_panel) | |
def unregister(): | |
bpy.utils.unregister_class(RENDER_SOLO_render_solo) | |
bpy.utils.unregister_class(RENDER_SOLO_PT_render_solo_panel) | |
if __name__ == "__main__": | |
register() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment