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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** \file
* \ingroup blenloader
*/
#include "MEM_guardedalloc.h"
#include "BLI_utildefines.h"
#include "BLI_listbase.h"
#include "BLI_math.h"
#include "BLI_string.h"
#include "BLI_system.h"
#include "DNA_camera_types.h"
#include "DNA_gpencil_types.h"
#include "DNA_mesh_types.h"
#include "DNA_object_types.h"
#include "DNA_scene_types.h"
#include "DNA_screen_types.h"
#include "DNA_space_types.h"
#include "DNA_userdef_types.h"
#include "DNA_windowmanager_types.h"
#include "DNA_workspace_types.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_light_types.h"
#include "BKE_appdir.h"
#include "BKE_brush.h"
#include "BKE_colortools.h"
#include "BKE_layer.h"
#include "BKE_library.h"
#include "BKE_main.h"
#include "BKE_node.h"
#include "BKE_paint.h"
#include "BKE_screen.h"
#include "BKE_workspace.h"
#include "BLO_readfile.h"
/**
* Rename if the ID doesn't exist.
*/
static ID *rename_id_for_versioning(Main *bmain,
const short id_type,
const char *name_src,
const char *name_dst)
{
/* We can ignore libraries */
ListBase *lb = which_libbase(bmain, id_type);
ID *id = NULL;
for (ID *idtest = lb->first; idtest; idtest = idtest->next) {
if (idtest->lib == NULL) {
if (STREQ(idtest->name + 2, name_src)) {
id = idtest;
}
if (STREQ(idtest->name + 2, name_dst)) {
return NULL;
}
}
}
if (id != NULL) {
BLI_strncpy(id->name + 2, name_dst, sizeof(id->name) - 2);
/* We know it's unique, this just sorts. */
BLI_libblock_ensure_unique_name(bmain, id->name);
}
return id;
}
static bool blo_is_builtin_template(const char *app_template)
{
/* For all builtin templates shipped with Blender. */
return (!app_template ||
STR_ELEM(app_template, "2D_Animation", "Sculpting", "VFX", "Video_Editing"));
}
static void blo_update_defaults_screen(bScreen *screen,
const char *app_template,
const char *workspace_name)
{
/* For all app templates. */
for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
for (ARegion *ar = sa->regionbase.first; ar; ar = ar->next) {
/* Some toolbars have been saved as initialized,
* we don't want them to have odd zoom-level or scrolling set, see: T47047 */
if (ELEM(ar->regiontype, RGN_TYPE_UI, RGN_TYPE_TOOLS, RGN_TYPE_TOOL_PROPS)) {
ar->v2d.flag &= ~V2D_IS_INITIALISED;
}
}
/* Set default folder. */
for (SpaceLink *sl = sa->spacedata.first; sl; sl = sl->next) {
if (sl->spacetype == SPACE_FILE) {
SpaceFile *sfile = (SpaceFile *)sl;
if (sfile->params) {
const char *dir_default = BKE_appdir_folder_default();
if (dir_default) {
STRNCPY(sfile->params->dir, dir_default);
sfile->params->file[0] = '\0';
}
}
}
}
}
/* For builtin templates only. */
if (!blo_is_builtin_template(app_template)) {
return;
}
for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
for (ARegion *ar = sa->regionbase.first; ar; ar = ar->next) {
/* Remove all stored panels, we want to use defaults
* (order, open/closed) as defined by UI code here! */
BKE_area_region_panels_free(&ar->panels);
BLI_freelistN(&ar->panels_category_active);
/* Reset size so it uses consistent defaults from the region types. */
ar->sizex = 0;
ar->sizey = 0;
}
if (sa->spacetype == SPACE_IMAGE) {
if (STREQ(workspace_name, "UV Editing")) {
SpaceImage *sima = sa->spacedata.first;
if (sima->mode == SI_MODE_VIEW) {
sima->mode = SI_MODE_UV;
}
}
}
else if (sa->spacetype == SPACE_ACTION) {
/* Show marker lines, hide channels and collapse summary in timelines. */
SpaceAction *saction = sa->spacedata.first;
saction->flag |= SACTION_SHOW_MARKER_LINES;
if (saction->mode == SACTCONT_TIMELINE) {
saction->ads.flag |= ADS_FLAG_SUMMARY_COLLAPSED;
for (ARegion *ar = sa->regionbase.first; ar; ar = ar->next) {
if (ar->regiontype == RGN_TYPE_CHANNELS) {
ar->flag |= RGN_FLAG_HIDDEN;
}
}
}
}
else if (sa->spacetype == SPACE_GRAPH) {
SpaceGraph *sipo = sa->spacedata.first;
sipo->flag |= SIPO_MARKER_LINES;
}
else if (sa->spacetype == SPACE_NLA) {
SpaceNla *snla = sa->spacedata.first;
snla->flag |= SNLA_SHOW_MARKER_LINES;
}
else if (sa->spacetype == SPACE_TEXT) {
/* Show syntax and line numbers in Script workspace text editor. */
SpaceText *stext = sa->spacedata.first;
stext->showsyntax = true;
stext->showlinenrs = true;
}
else if (sa->spacetype == SPACE_VIEW3D) {
View3D *v3d = sa->spacedata.first;
/* Screen space cavity by default for faster performance. */
v3d->shading.cavity_type = V3D_SHADING_CAVITY_CURVATURE;
v3d->shading.flag |= V3D_SHADING_SPECULAR_HIGHLIGHT;
v3d->overlay.texture_paint_mode_opacity = 1.0f;
v3d->overlay.weight_paint_mode_opacity = 1.0f;
v3d->overlay.vertex_paint_mode_opacity = 1.0f;
/* Use dimmed selected edges. */
v3d->overlay.edit_flag &= ~V3D_OVERLAY_EDIT_EDGES;
/* grease pencil settings */
v3d->vertex_opacity = 1.0f;
v3d->gp_flag |= V3D_GP_SHOW_EDIT_LINES;
/* Remove dither pattern in wireframe mode. */
v3d->shading.xray_alpha_wire = 0.0f;
/* Skip startups that use the viewport color by default. */
if (v3d->shading.background_type != V3D_SHADING_BACKGROUND_VIEWPORT) {
copy_v3_fl(v3d->shading.background_color, 0.05f);
}
}
else if (sa->spacetype == SPACE_CLIP) {
SpaceClip *sclip = sa->spacedata.first;
sclip->around = V3D_AROUND_CENTER_MEDIAN;
}
}
/* Show top-bar by default. */
for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
for (SpaceLink *sl = sa->spacedata.first; sl; sl = sl->next) {
ListBase *regionbase = (sl == sa->spacedata.first) ? &sa->regionbase : &sl->regionbase;
for (ARegion *ar = regionbase->first; ar; ar = ar->next) {
if (ar->regiontype == RGN_TYPE_TOOL_HEADER) {
ar->flag &= ~(RGN_FLAG_HIDDEN | RGN_FLAG_HIDDEN_BY_USER);
}
}
}
}
}
void BLO_update_defaults_workspace(WorkSpace *workspace, const char *app_template)
{
ListBase *layouts = BKE_workspace_layouts_get(workspace);
for (WorkSpaceLayout *layout = layouts->first; layout; layout = layout->next) {
if (layout->screen) {
blo_update_defaults_screen(layout->screen, app_template, workspace->id.name + 2);
}
}
if (blo_is_builtin_template(app_template)) {
/* Clear all tools to use default options instead, ignore the tool saved in the file. */
while (!BLI_listbase_is_empty(&workspace->tools)) {
BKE_workspace_tool_remove(workspace, workspace->tools.first);
}
/* For 2D animation template. */
if (STREQ(workspace->id.name + 2, "Drawing")) {
workspace->object_mode = OB_MODE_PAINT_GPENCIL;
}
}
}
static void blo_update_defaults_scene(Main *bmain, Scene *scene)
{
BLI_strncpy(scene->r.engine, RE_engine_id_BLENDER_EEVEE, sizeof(scene->r.engine));
scene->r.cfra = 1.0f;
scene->r.displaymode = R_OUTPUT_WINDOW;
/* Don't enable compositing nodes. */
if (scene->nodetree) {
ntreeFreeNestedTree(scene->nodetree);
MEM_freeN(scene->nodetree);
scene->nodetree = NULL;
scene->use_nodes = false;
}
/* Rename render layers. */
BKE_view_layer_rename(bmain, scene, scene->view_layers.first, "View Layer");
/* New EEVEE defaults. */
scene->eevee.bloom_intensity = 0.05f;
scene->eevee.bloom_clamp = 0.0f;
scene->eevee.motion_blur_shutter = 0.5f;
copy_v3_v3(scene->display.light_direction, (float[3]){M_SQRT1_3, M_SQRT1_3, M_SQRT1_3});
copy_v2_fl2(scene->safe_areas.title, 0.1f, 0.05f);
copy_v2_fl2(scene->safe_areas.action, 0.035f, 0.035f);
/* Change default cubemap quality. */
scene->eevee.gi_filter_quality = 3.0f;
/* Enable Soft Shadows by default. */
scene->eevee.flag |= SCE_EEVEE_SHADOW_SOFT;
/* Be sure curfalloff and primitive are initializated */
ToolSettings *ts = scene->toolsettings;
if (ts->gp_sculpt.cur_falloff == NULL) {
ts->gp_sculpt.cur_falloff = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
CurveMapping *gp_falloff_curve = ts->gp_sculpt.cur_falloff;
BKE_curvemapping_initialize(gp_falloff_curve);
BKE_curvemap_reset(gp_falloff_curve->cm,
&gp_falloff_curve->clipr,
CURVE_PRESET_GAUSS,
CURVEMAP_SLOPE_POSITIVE);
}
if (ts->gp_sculpt.cur_primitive == NULL) {
ts->gp_sculpt.cur_primitive = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
CurveMapping *gp_primitive_curve = ts->gp_sculpt.cur_primitive;
BKE_curvemapping_initialize(gp_primitive_curve);
BKE_curvemap_reset(gp_primitive_curve->cm,
&gp_primitive_curve->clipr,
CURVE_PRESET_BELL,
CURVEMAP_SLOPE_POSITIVE);
}
if (ts->sculpt) {
ts->sculpt->paint.symmetry_flags |= PAINT_SYMMETRY_FEATHER;
}
/* Correct default startup UV's. */
Mesh *me = BLI_findstring(&bmain->meshes, "Cube", offsetof(ID, name) + 2);
if (me && (me->totloop == 24) && (me->mloopuv != NULL)) {
const float uv_values[24][2] = {
{0.625, 0.50}, {0.875, 0.50}, {0.875, 0.75}, {0.625, 0.75}, {0.375, 0.75}, {0.625, 0.75},
{0.625, 1.00}, {0.375, 1.00}, {0.375, 0.00}, {0.625, 0.00}, {0.625, 0.25}, {0.375, 0.25},
{0.125, 0.50}, {0.375, 0.50}, {0.375, 0.75}, {0.125, 0.75}, {0.375, 0.50}, {0.625, 0.50},
{0.625, 0.75}, {0.375, 0.75}, {0.375, 0.25}, {0.625, 0.25}, {0.625, 0.50}, {0.375, 0.50},
};
for (int i = 0; i < ARRAY_SIZE(uv_values); i++) {
copy_v2_v2(me->mloopuv[i].uv, uv_values[i]);
}
}
}
/**
* Update defaults in startup.blend, without having to save and embed the file.
* This function can be emptied each time the startup.blend is updated. */
void BLO_update_defaults_startup_blend(Main *bmain, const char *app_template)
{
/* For all app templates. */
for (WorkSpace *workspace = bmain->workspaces.first; workspace; workspace = workspace->id.next) {
BLO_update_defaults_workspace(workspace, app_template);
}
/* For builtin templates only. */
if (!blo_is_builtin_template(app_template)) {
return;
}
/* Workspaces. */
wmWindow *win = ((wmWindowManager *)bmain->wm.first)->windows.first;
for (WorkSpace *workspace = bmain->workspaces.first; workspace; workspace = workspace->id.next) {
WorkSpaceLayout *layout = BKE_workspace_hook_layout_for_workspace_get(win->workspace_hook,
workspace);
/* Name all screens by their workspaces (avoids 'Default.###' names). */
/* Default only has one window. */
if (layout->screen) {
bScreen *screen = layout->screen;
BLI_strncpy(screen->id.name + 2, workspace->id.name + 2, sizeof(screen->id.name) - 2);
BLI_libblock_ensure_unique_name(bmain, screen->id.name);
}
/* For some reason we have unused screens, needed until re-saving.
* Clear unused layouts because they're visible in the outliner & Python API. */
LISTBASE_FOREACH_MUTABLE (WorkSpaceLayout *, layout_iter, &workspace->layouts) {
if (layout != layout_iter) {
BKE_workspace_layout_remove(bmain, workspace, layout_iter);
}
}
}
/* Scenes */
for (Scene *scene = bmain->scenes.first; scene; scene = scene->id.next) {
blo_update_defaults_scene(bmain, scene);
if (app_template && STREQ(app_template, "Video_Editing")) {
/* Filmic is too slow, use standard until it is optimized. */
STRNCPY(scene->view_settings.view_transform, "Standard");
STRNCPY(scene->view_settings.look, "None");
}
else {
/* AV Sync break physics sim caching, disable until that is fixed. */
scene->audio.flag &= ~AUDIO_SYNC;
scene->flag &= ~SCE_FRAME_DROP;
}
/* Change default selection mode for Grease Pencil. */
if (app_template && STREQ(app_template, "2D_Animation")) {
ToolSettings *ts = scene->toolsettings;
ts->gpencil_selectmode_edit = GP_SELECTMODE_STROKE;
}
}
/* Objects */
rename_id_for_versioning(bmain, ID_OB, "Lamp", "Light");
rename_id_for_versioning(bmain, ID_LA, "Lamp", "Light");
if (app_template && STREQ(app_template, "2D_Animation")) {
for (Object *object = bmain->objects.first; object; object = object->id.next) {
if (object->type == OB_GPENCIL) {
/* Set grease pencil object in drawing mode */
bGPdata *gpd = (bGPdata *)object->data;
object->mode = OB_MODE_PAINT_GPENCIL;
gpd->flag |= GP_DATA_STROKE_PAINTMODE;
break;
}
}
}
for (Mesh *mesh = bmain->meshes.first; mesh; mesh = mesh->id.next) {
/* Match default for new meshes. */
mesh->smoothresh = DEG2RADF(30);
}
for (Camera *camera = bmain->cameras.first; camera; camera = camera->id.next) {
/* Initialize to a useful value. */
camera->dof.focus_distance = 10.0f;
camera->dof.aperture_fstop = 2.8f;
}
for (Light *light = bmain->lights.first; light; light = light->id.next) {
/* Fix lights defaults. */
light->clipsta = 0.05f;
light->att_dist = 40.0f;
}
/* Materials */
for (Material *ma = bmain->materials.first; ma; ma = ma->id.next) {
/* Update default material to be a bit more rough. */
ma->roughness = 0.4f;
if (ma->nodetree) {
for (bNode *node = ma->nodetree->nodes.first; node; node = node->next) {
if (node->type == SH_NODE_BSDF_PRINCIPLED) {
bNodeSocket *roughness_socket = nodeFindSocket(node, SOCK_IN, "Roughness");
bNodeSocketValueFloat *roughness_data = roughness_socket->default_value;
roughness_data->value = 0.4f;
}
}
}
}
/* Brushes */
{
/* Enable for UV sculpt (other brush types will be created as needed),
* without this the grab brush will be active but not selectable from the list. */
Brush *brush = BLI_findstring(&bmain->brushes, "Grab", offsetof(ID, name) + 2);
if (brush) {
brush->ob_mode |= OB_MODE_EDIT;
}
}
for (Brush *brush = bmain->brushes.first; brush; brush = brush->id.next) {
brush->blur_kernel_radius = 2;
}
{
/* Change the spacing of the Smear brush to 3.0% */
Brush *brush = BLI_findstring(&bmain->brushes, "Smear", offsetof(ID, name) + 2);
if (brush) {
brush->spacing = 3.0;
}
brush = BLI_findstring(&bmain->brushes, "Draw Sharp", offsetof(ID, name) + 2);
if (!brush) {
brush = BKE_brush_add(bmain, "Draw Sharp", OB_MODE_SCULPT);
id_us_min(&brush->id);
brush->sculpt_tool = SCULPT_TOOL_DRAW_SHARP;
}
brush = BLI_findstring(&bmain->brushes, "Elastic Deform", offsetof(ID, name) + 2);
if (!brush) {
brush = BKE_brush_add(bmain, "Elastic Defrom", OB_MODE_SCULPT);
id_us_min(&brush->id);
brush->sculpt_tool = SCULPT_TOOL_ELASTIC_DEFORM;
}
brush = BLI_findstring(&bmain->brushes, "Pose", offsetof(ID, name) + 2);
if (!brush) {
brush = BKE_brush_add(bmain, "Pose", OB_MODE_SCULPT);
id_us_min(&brush->id);
brush->sculpt_tool = SCULPT_TOOL_POSE;
}
brush = BLI_findstring(&bmain->brushes, "Simplify", offsetof(ID, name) + 2);
if (!brush) {
brush = BKE_brush_add(bmain, "Simplify", OB_MODE_SCULPT);
id_us_min(&brush->id);
brush->sculpt_tool = SCULPT_TOOL_SIMPLIFY;
}
/* Use the same tool icon color in the brush cursor */
for (brush = bmain->brushes.first; brush; brush = brush->id.next) {
if (brush->sculpt_tool) {
BKE_brush_sculpt_reset(brush);
}
}
}
if (app_template && STREQ(app_template, "2D_Animation")) {
/* Update Grease Pencil brushes. */
Brush *brush;
/* Pencil brush. */
brush = BLI_findstring(&bmain->brushes, "Draw Pencil", offsetof(ID, name) + 2);
if (brush) {
/* Change brush name. */
rename_id_for_versioning(bmain, ID_BR, "Draw Pencil", "Pencil");
}
/* Pen brush. */
brush = BLI_findstring(&bmain->brushes, "Draw Pen", offsetof(ID, name) + 2);
if (brush) {
/* Change brush name. */
rename_id_for_versioning(bmain, ID_BR, "Draw Pen", "Pen");
}
/* Pen Soft brush. */
brush = BLI_findstring(&bmain->brushes, "Draw Soft", offsetof(ID, name) + 2);
if (brush) {
brush->gpencil_settings->icon_id = GP_BRUSH_ICON_PEN;
/* Change brush name. */
rename_id_for_versioning(bmain, ID_BR, "Draw Soft", "Pencil Soft");
}
/* Ink Pen brush. */
brush = BLI_findstring(&bmain->brushes, "Draw Ink", offsetof(ID, name) + 2);
if (brush) {
/* Change brush name. */
rename_id_for_versioning(bmain, ID_BR, "Draw Ink", "Ink Pen");
}
/* Ink Pen Rough brush. */
brush = BLI_findstring(&bmain->brushes, "Draw Noise", offsetof(ID, name) + 2);
if (brush) {
/* Change brush name. */
rename_id_for_versioning(bmain, ID_BR, "Draw Noise", "Ink Pen Rough");
}
/* Marker Bold brush. */
brush = BLI_findstring(&bmain->brushes, "Draw Marker", offsetof(ID, name) + 2);
if (brush) {
/* Change brush name. */
rename_id_for_versioning(bmain, ID_BR, "Draw Marker", "Marker Bold");
}
/* Marker Chisel brush. */
brush = BLI_findstring(&bmain->brushes, "Draw Block", offsetof(ID, name) + 2);
if (brush) {
/* Change brush name. */
rename_id_for_versioning(bmain, ID_BR, "Draw Block", "Marker Chisel");
}
/* Remove useless Fill Area.001 brush. */
brush = BLI_findstring(&bmain->brushes, "Fill Area.001", offsetof(ID, name) + 2);
if (brush) {
BKE_id_delete(bmain, brush);
}
/* Reset all grease pencil brushes. */
Scene *scene = bmain->scenes.first;
BKE_brush_gpencil_presets(bmain, scene->toolsettings);
}
}
|