-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperators_story.py
More file actions
375 lines (311 loc) · 11.9 KB
/
Copy pathoperators_story.py
File metadata and controls
375 lines (311 loc) · 11.9 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import bpy
from . import page_manager
from .operators_textflow import DEFAULT_WIDTH, DEFAULT_HEIGHT
STORY_SOURCE_KEY = "greasepress_story_source"
STORY_ORDER_KEY = "greasepress_story_order"
STORY_FRAME_KEY = "greasepress_story_frame"
def _settings(context):
return context.scene.editorial_gp
def _source_text(context):
name = _settings(context).story_source_name.strip()
return bpy.data.texts.get(name) if name else None
def _source_contents(text_block):
if text_block is None:
return ""
if hasattr(text_block, "as_string"):
return text_block.as_string()
return "\n".join(line.body for line in text_block.lines)
def _story_frames(source_name):
frames = [
obj for obj in bpy.data.objects
if obj.type == 'FONT'
and obj.get(STORY_FRAME_KEY)
and obj.get(STORY_SOURCE_KEY) == source_name
]
frames.sort(key=lambda obj: (int(obj.get(STORY_ORDER_KEY, 0)), obj.name))
return frames
def _renumber(frames):
for index, obj in enumerate(frames, start=1):
obj[STORY_ORDER_KEY] = index
def _new_story_frame(context, source_name):
page = page_manager.active_page(context.scene)
if page is None:
return None
page_manager.activate_page_collection(page, context.view_layer)
curve = bpy.data.curves.new("Story Frame", type='FONT')
curve.body = ""
curve.align_x = 'LEFT'
curve.align_y = 'TOP_BASELINE'
curve.size = 0.006
curve.space_line = 1.0
if hasattr(curve, "overflow"):
curve.overflow = 'NONE'
box = curve.text_boxes[0]
box.width = DEFAULT_WIDTH
box.height = DEFAULT_HEIGHT
box.x = -DEFAULT_WIDTH * 0.5
box.y = DEFAULT_HEIGHT * 0.5
obj = bpy.data.objects.new("Story Frame", curve)
obj.location = (0.0, 0.0, 0.012)
obj["greasepress_flow_text"] = True
obj["greasepress_textbox_index"] = 0
obj[STORY_FRAME_KEY] = True
obj[STORY_SOURCE_KEY] = source_name
frames = _story_frames(source_name)
obj[STORY_ORDER_KEY] = len(frames) + 1
page.objects.link(obj)
bpy.ops.object.select_all(action='DESELECT')
obj.select_set(True)
context.view_layer.objects.active = obj
return obj
def _copy_text_style(source, target):
attrs = (
"align_x", "align_y", "size", "shear", "space_character",
"space_word", "space_line", "offset", "extrude",
"bevel_depth", "bevel_resolution", "resolution_u",
"fill_mode", "follow_curve",
)
for attr in attrs:
if hasattr(source, attr) and hasattr(target, attr):
try:
setattr(target, attr, getattr(source, attr))
except (AttributeError, TypeError, ValueError):
pass
for attr in ("font", "font_bold", "font_italic", "font_bold_italic"):
if hasattr(source, attr) and hasattr(target, attr):
try:
setattr(target, attr, getattr(source, attr))
except (AttributeError, TypeError, ValueError):
pass
if hasattr(target, "overflow"):
target.overflow = 'NONE'
def _make_measure_object(context, template):
curve = bpy.data.curves.new("GP_STORY_MEASURE", type='FONT')
_copy_text_style(template, curve)
box = curve.text_boxes[0]
box.x = 0.0
box.y = 0.0
obj = bpy.data.objects.new("GP_STORY_MEASURE", curve)
context.scene.collection.objects.link(obj)
obj.hide_render = True
obj.hide_set(True)
return obj
def _remove_measure_object(obj):
curve = obj.data if obj else None
if obj and obj.name in bpy.data.objects:
bpy.data.objects.remove(obj, do_unlink=True)
if curve and curve.name in bpy.data.curves and curve.users == 0:
bpy.data.curves.remove(curve)
def _fits(context, measure_obj, candidate, width, height):
curve = measure_obj.data
box = curve.text_boxes[0]
box.width = max(width, 0.001)
box.height = max(height, 0.001)
curve.body = candidate or " "
context.view_layer.update()
evaluated = measure_obj.evaluated_get(context.evaluated_depsgraph_get())
dimensions = evaluated.dimensions
epsilon = 0.00075
return dimensions.x <= box.width + epsilon and dimensions.y <= box.height + epsilon
def _word_boundary(text, count):
if count >= len(text):
return len(text)
if count <= 0:
return 0
lower = max(0, count - 80)
for index in range(count, lower, -1):
if text[index - 1].isspace():
return index
return count
def _fit_box(context, measure_obj, text, width, height):
if not text:
return 0
if _fits(context, measure_obj, text, width, height):
return len(text)
low, high = 0, len(text)
while low < high:
middle = (low + high + 1) // 2
if _fits(context, measure_obj, text[:middle], width, height):
low = middle
else:
high = middle - 1
count = _word_boundary(text, low)
if count == 0 and low > 0:
count = low
return count
def _capacity_for_object(context, obj, remaining):
measure = _make_measure_object(context, obj.data)
consumed = 0
try:
for box in obj.data.text_boxes:
if consumed >= len(remaining):
break
count = _fit_box(
context,
measure,
remaining[consumed:],
float(box.width),
float(box.height),
)
consumed += count
if count <= 0:
break
finally:
_remove_measure_object(measure)
return consumed
def refresh_story(context, source):
settings = _settings(context)
contents = _source_contents(source)
frames = _story_frames(source.name)
_renumber(frames)
cursor = 0
for frame in frames:
remaining = contents[cursor:]
count = _capacity_for_object(context, frame, remaining)
frame.data.body = remaining[:count]
frame["greasepress_story_start"] = cursor
frame["greasepress_story_length"] = count
cursor += count
overflow = max(0, len(contents) - cursor)
settings.story_last_overflow_chars = overflow
settings.story_last_distributed_chars = cursor
settings.story_last_frame_count = len(frames)
return len(frames), cursor, overflow
class EDITORIALGP_OT_story_source_new(bpy.types.Operator):
bl_idname = "editorial_gp.story_source_new"
bl_label = "New Source"
bl_description = "Create a Text Data-Block in the Text Editor"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
base = "GreasePress Story"
text = bpy.data.texts.new(base)
text.write("Paste or write the complete story text here.\n")
_settings(context).story_source_name = text.name
self.report({'INFO'}, f"Source created: {text.name}")
return {'FINISHED'}
class EDITORIALGP_OT_story_source_open(bpy.types.Operator):
bl_idname = "editorial_gp.story_source_open"
bl_label = "Open in Text Editor"
bl_description = "Open the selected source in the current area"
def execute(self, context):
source = _source_text(context)
if source is None:
self.report({'ERROR'}, "Select or create a text source")
return {'CANCELLED'}
context.area.type = 'TEXT_EDITOR'
context.area.spaces.active.text = source
return {'FINISHED'}
class EDITORIALGP_OT_story_frame_add(bpy.types.Operator):
bl_idname = "editorial_gp.story_frame_add"
bl_label = "Add Frame to Story"
bl_description = "Create a source-linked frame on the current page"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
source = _source_text(context)
if source is None:
self.report({'ERROR'}, "Select or create a text source")
return {'CANCELLED'}
if _new_story_frame(context, source.name) is None:
self.report({'ERROR'}, "Create a GreasePress document first")
return {'CANCELLED'}
return {'FINISHED'}
class EDITORIALGP_OT_story_frame_link(bpy.types.Operator):
bl_idname = "editorial_gp.story_frame_link"
bl_label = "Link Selected"
bl_description = "Link the selected Text object to the current story"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return context.object is not None and context.object.type == 'FONT'
def execute(self, context):
source = _source_text(context)
if source is None:
self.report({'ERROR'}, "Select or create a text source")
return {'CANCELLED'}
obj = context.object
obj[STORY_FRAME_KEY] = True
obj[STORY_SOURCE_KEY] = source.name
frames = _story_frames(source.name)
obj[STORY_ORDER_KEY] = max([int(f.get(STORY_ORDER_KEY, 0)) for f in frames] + [0]) + 1
obj["greasepress_flow_text"] = True
_renumber(_story_frames(source.name))
return {'FINISHED'}
class EDITORIALGP_OT_story_frame_unlink(bpy.types.Operator):
bl_idname = "editorial_gp.story_frame_unlink"
bl_label = "Unlink Frame"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return context.object is not None and bool(context.object.get(STORY_FRAME_KEY))
def execute(self, context):
obj = context.object
source_name = obj.get(STORY_SOURCE_KEY, "")
for key in (STORY_FRAME_KEY, STORY_SOURCE_KEY, STORY_ORDER_KEY, "greasepress_story_start", "greasepress_story_length"):
if key in obj:
del obj[key]
if source_name:
_renumber(_story_frames(source_name))
return {'FINISHED'}
class _MoveFrameBase:
direction = -1
@classmethod
def poll(cls, context):
return context.object is not None and bool(context.object.get(STORY_FRAME_KEY))
def execute(self, context):
obj = context.object
source_name = obj.get(STORY_SOURCE_KEY, "")
frames = _story_frames(source_name)
if obj not in frames:
return {'CANCELLED'}
index = frames.index(obj)
target = index + self.direction
if target < 0 or target >= len(frames):
return {'CANCELLED'}
frames[index], frames[target] = frames[target], frames[index]
_renumber(frames)
return {'FINISHED'}
class EDITORIALGP_OT_story_frame_up(_MoveFrameBase, bpy.types.Operator):
bl_idname = "editorial_gp.story_frame_up"
bl_label = "Earlier in Story"
bl_options = {'REGISTER', 'UNDO'}
direction = -1
class EDITORIALGP_OT_story_frame_down(_MoveFrameBase, bpy.types.Operator):
bl_idname = "editorial_gp.story_frame_down"
bl_label = "Later in Story"
bl_options = {'REGISTER', 'UNDO'}
direction = 1
class EDITORIALGP_OT_story_refresh(bpy.types.Operator):
bl_idname = "editorial_gp.story_refresh"
bl_label = "Update Multi-page Flow"
bl_description = "Distribute the source across linked frames in story order"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
source = _source_text(context)
if source is None:
self.report({'ERROR'}, "Select or create a text source")
return {'CANCELLED'}
frames, distributed, overflow = refresh_story(context, source)
if not frames:
self.report({'WARNING'}, "The story contains no frames")
return {'CANCELLED'}
if overflow:
self.report({'WARNING'}, f"Distributed {distributed} characters; remaining overflow: {overflow}")
else:
self.report({'INFO'}, f"Text distributed across {frames} frames")
return {'FINISHED'}
classes = (
EDITORIALGP_OT_story_source_new,
EDITORIALGP_OT_story_source_open,
EDITORIALGP_OT_story_frame_add,
EDITORIALGP_OT_story_frame_link,
EDITORIALGP_OT_story_frame_unlink,
EDITORIALGP_OT_story_frame_up,
EDITORIALGP_OT_story_frame_down,
EDITORIALGP_OT_story_refresh,
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
for cls in reversed(classes):
bpy.utils.unregister_class(cls)