/
__init__.py
95 lines (78 loc) · 3.79 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
bl_info = {
"name": "Filepath Formatter",
"author": "Ryan Poole",
"version": (1, 1, 0),
"blender": (2, 80, 0),
"location": "Output Properties > Filepath Formatter",
"description": "Sets the format for ouput renders.",
"category": "Render"
}
import bpy
import datetime
from bpy.app.handlers import persistent
@persistent
def update_filepath(self):
settings = bpy.context.scene.filepath_formatter_settings
if not settings.use_filepath_formatter:
return
bpy.context.scene.render.filepath = replace_tokens(settings.directory, '') + '\\' + replace_tokens(settings.filename, '')
if bpy.context.scene.use_nodes:
for node in bpy.context.scene.node_tree.nodes:
if node.type == "OUTPUT_FILE":
node.base_path = replace_tokens(settings.directory, node.label)
for slot in node.file_slots:
slot.path = replace_tokens(settings.filename, node.label)
def replace_tokens(base_path, label):
base_path = base_path \
.replace('%resx', str(bpy.context.scene.render.resolution_x)) \
.replace('%resy', str(bpy.context.scene.render.resolution_y)) \
.replace('%scene', bpy.context.scene.name) \
.replace('%camera', bpy.context.scene.camera.name) \
.replace('%filename', bpy.path.basename(bpy.context.blend_data.filepath).replace('.blend', '')) \
.replace('%label', label)
now = datetime.datetime.now()
print(base_path)
base_path = now.strftime(base_path)
return base_path
class FilepathFormatterSettings(bpy.types.PropertyGroup):
use_filepath_formatter: bpy.props.BoolProperty(name="Filepath formatter.",
description="Enable/disable generation of filepaths. When enabled, "
"this will use the path and filename for renders and "
"all File Output nodes.",
default=False)
directory: bpy.props.StringProperty(name="Directory",
description="Directory format.",
default="//",
maxlen=4096)
filename: bpy.props.StringProperty(name="File Name",
description="File name format.",
default="#####",
maxlen=4096)
class FilepathFormatter_PT_panel(bpy.types.Panel):
bl_label = "Filepath Formatter"
bl_space_type = "PROPERTIES"
bl_region_type = "WINDOW"
bl_idname = "OUTPUT_PT_filepath_formatter_panel"
bl_context = "output"
bl_options = {"DEFAULT_CLOSED"}
def draw_header(self, context):
self.layout.prop(context.scene.filepath_formatter_settings, "use_filepath_formatter", text="")
def draw(self, context):
layout = self.layout
layout.prop(context.scene.filepath_formatter_settings, "directory")
layout.prop(context.scene.filepath_formatter_settings, "filename")
classes = [FilepathFormatterSettings, FilepathFormatter_PT_panel]
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Scene.filepath_formatter_settings = bpy.props.PointerProperty(type=FilepathFormatterSettings)
if update_filepath not in bpy.app.handlers.render_pre:
bpy.app.handlers.render_pre.append(update_filepath)
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)
del bpy.types.Scene.filepath_formatter_settings
if update_filepath in bpy.app.handlers.render_pre:
bpy.app.handlers.render_pre.remove(update_filepath)
if __name__ == "__main__":
register()