-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
120 lines (96 loc) · 3.59 KB
/
__init__.py
File metadata and controls
120 lines (96 loc) · 3.59 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
bl_info = {
"name": "Resolution LITE (Free)",
"description": "Literally just swaps your X and Y resolution. That's it.",
"author": "Mightyiest",
"version": (1, 0, 1),
"blender": (3, 0, 0),
"category": "Render",
"location": "Output Properties > Format",
"license": "GPL-3.0-or-later",
}
import bpy
from bpy.app.handlers import persistent
# Guard flag to prevent infinite loops when swapping
_updating_orientation = False
def update_orientation(self, context):
"""Swap resolution_x and resolution_y only when the new orientation
doesn't match the current aspect ratio. Skips square resolutions."""
global _updating_orientation
if _updating_orientation:
return
_updating_orientation = True
try:
render = context.scene.render
# Bug 1 fix: only swap when the orientation actually needs to change
is_landscape = render.resolution_x >= render.resolution_y
want_landscape = self.resolution_lite_orientation == 'LANDSCAPE'
# Square resolutions — nothing to swap
if render.resolution_x == render.resolution_y:
return
if want_landscape != is_landscape:
render.resolution_x, render.resolution_y = (
render.resolution_y,
render.resolution_x,
)
finally:
_updating_orientation = False
# Bug 2 fix: sync the enum to the real resolution when a .blend is loaded
@persistent
def _sync_orientation_on_load(_):
"""Set the orientation toggle to match the saved resolution on file load."""
global _updating_orientation
_updating_orientation = True
try:
for scene in bpy.data.scenes:
render = scene.render
if render.resolution_x >= render.resolution_y:
scene.resolution_lite_orientation = 'LANDSCAPE'
else:
scene.resolution_lite_orientation = 'PORTRAIT'
finally:
_updating_orientation = False
def draw_lite_panel(self, context):
"""Draw the minimal orientation toggle in the Format panel."""
layout = self.layout
scene = context.scene
render = scene.render
box = layout.box()
# Header row: label on the left, resolution readout on the right
header = box.row()
header.label(text="Resolution LITE", icon='ARROW_LEFTRIGHT')
header.label(text=f"{render.resolution_x} \u00d7 {render.resolution_y}")
row = box.row(align=True)
row.prop(scene, "resolution_lite_orientation", expand=True)
def register():
# Bug 5 fix: reset the guard flag on (re-)registration
global _updating_orientation
_updating_orientation = False
bpy.types.Scene.resolution_lite_orientation = bpy.props.EnumProperty(
name="Orientation",
items=[
('LANDSCAPE', 'Landscape', 'Horizontal'),
('PORTRAIT', 'Portrait', 'Vertical'),
],
default='LANDSCAPE',
update=update_orientation,
)
# Append the toggle to the existing Format panel
bpy.types.RENDER_PT_format.append(draw_lite_panel)
# Register the load handler so the toggle syncs on file open
bpy.app.handlers.load_post.append(_sync_orientation_on_load)
# Bug 4 fix: guard against unregister being called without a prior register
def unregister():
try:
bpy.app.handlers.load_post.remove(_sync_orientation_on_load)
except ValueError:
pass
try:
bpy.types.RENDER_PT_format.remove(draw_lite_panel)
except ValueError:
pass
try:
del bpy.types.Scene.resolution_lite_orientation
except AttributeError:
pass
if __name__ == "__main__":
register()