22c58c9e479e0e90f7316f59cb53b37e89d76a59
[blender.git] / source / blender / blenloader / intern / versioning_defaults.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16
17 /** \file blender/blenloader/intern/versioning_defaults.c
18  *  \ingroup blenloader
19  */
20
21 #include "MEM_guardedalloc.h"
22
23 #include "BLI_utildefines.h"
24 #include "BLI_listbase.h"
25 #include "BLI_math.h"
26 #include "BLI_string.h"
27
28 #include "DNA_gpencil_types.h"
29 #include "DNA_mesh_types.h"
30 #include "DNA_object_types.h"
31 #include "DNA_scene_types.h"
32 #include "DNA_screen_types.h"
33 #include "DNA_space_types.h"
34 #include "DNA_userdef_types.h"
35 #include "DNA_windowmanager_types.h"
36 #include "DNA_workspace_types.h"
37
38 #include "BKE_appdir.h"
39 #include "BKE_brush.h"
40 #include "BKE_colorband.h"
41 #include "BKE_colortools.h"
42 #include "BKE_idprop.h"
43 #include "BKE_keyconfig.h"
44 #include "BKE_layer.h"
45 #include "BKE_library.h"
46 #include "BKE_main.h"
47 #include "BKE_node.h"
48 #include "BKE_paint.h"
49 #include "BKE_screen.h"
50 #include "BKE_workspace.h"
51
52 #include "BLO_readfile.h"
53
54 /**
55  * Override values in in-memory startup.blend, avoids resaving for small changes.
56  */
57 void BLO_update_defaults_userpref_blend(void)
58 {
59         /* default so DPI is detected automatically */
60         U.dpi = 0;
61         U.ui_scale = 1.0f;
62
63 #ifdef WITH_PYTHON_SECURITY
64         /* use alternative setting for security nuts
65          * otherwise we'd need to patch the binary blob - startup.blend.c */
66         U.flag |= USER_SCRIPT_AUTOEXEC_DISABLE;
67 #else
68         U.flag &= ~USER_SCRIPT_AUTOEXEC_DISABLE;
69 #endif
70
71         /* Clear addon preferences. */
72         for (bAddon *addon = U.addons.first; addon; addon = addon->next) {
73                 if (addon->prop) {
74                         IDP_FreeProperty(addon->prop);
75                         MEM_freeN(addon->prop);
76                         addon->prop = NULL;
77                 }
78         }
79
80         /* Transform tweak with single click and drag. */
81         U.flag |= USER_RELEASECONFIRM;
82
83         /* Ignore the theme saved in the blend file,
84          * instead use the theme from 'userdef_default_theme.c' */
85         {
86                 bTheme *theme = U.themes.first;
87                 memcpy(theme, &U_theme_default, sizeof(bTheme));
88         }
89
90         /* Leave temp directory empty, will then get appropriate value per OS. */
91         U.tempdir[0] = '\0';
92
93         /* Only enable tooltips translation by default, without actually enabling translation itself, for now. */
94         U.transopts = USER_TR_TOOLTIPS;
95         U.memcachelimit = 4096;
96
97         /* Auto perspective. */
98         U.uiflag |= USER_AUTOPERSP;
99
100         /* Init weight paint range. */
101         BKE_colorband_init(&U.coba_weight, true);
102
103         /* Default visible section. */
104         U.userpref = USER_SECTION_INTERFACE;
105
106         /* Default to left click select. */
107         BKE_keyconfig_pref_set_select_mouse(&U, 0, true);
108 }
109
110
111 /**
112  * Rename if the ID doesn't exist.
113  */
114 static ID *rename_id_for_versioning(Main *bmain, const short id_type, const char *name_src, const char *name_dst)
115 {
116         /* We can ignore libraries */
117         ListBase *lb = which_libbase(bmain, id_type);
118         ID *id = NULL;
119         for (ID *idtest = lb->first; idtest; idtest = idtest->next) {
120                 if (idtest->lib == NULL) {
121                         if (STREQ(idtest->name + 2, name_src)) {
122                                 id = idtest;
123                         }
124                         if (STREQ(idtest->name + 2, name_dst)) {
125                                 return NULL;
126                         }
127                 }
128         }
129         if (id != NULL) {
130                 BLI_strncpy(id->name + 2, name_dst, sizeof(id->name) - 2);
131                 /* We know it's unique, this just sorts. */
132                 BLI_libblock_ensure_unique_name(bmain, id->name);
133         }
134         return id;
135 }
136
137 /**
138  * Update defaults in startup.blend, without having to save and embed the file.
139  * This function can be emptied each time the startup.blend is updated. */
140 void BLO_update_defaults_startup_blend(Main *bmain, const char *app_template)
141 {
142         /* For all startup.blend files. */
143         for (bScreen *screen = bmain->screen.first; screen; screen = screen->id.next) {
144                 for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
145                         for (ARegion *ar = sa->regionbase.first; ar; ar = ar->next) {
146                                 /* Remove all stored panels, we want to use defaults (order, open/closed) as defined by UI code here! */
147                                 BKE_area_region_panels_free(&ar->panels);
148
149                                 /* some toolbars have been saved as initialized,
150                                  * we don't want them to have odd zoom-level or scrolling set, see: T47047 */
151                                 if (ELEM(ar->regiontype, RGN_TYPE_UI, RGN_TYPE_TOOLS, RGN_TYPE_TOOL_PROPS)) {
152                                         ar->v2d.flag &= ~V2D_IS_INITIALISED;
153                                 }
154                         }
155
156                         for (SpaceLink *sl = sa->spacedata.first; sl; sl = sl->next) {
157                                 switch (sl->spacetype) {
158                                         case SPACE_VIEW3D:
159                                         {
160                                                 View3D *v3d = (View3D *)sl;
161                                                 v3d->overlay.texture_paint_mode_opacity = 1.0f;
162                                                 v3d->overlay.weight_paint_mode_opacity = 1.0f;
163                                                 v3d->overlay.vertex_paint_mode_opacity = 1.0f;
164                                                 /* grease pencil settings */
165                                                 v3d->vertex_opacity = 1.0f;
166                                                 v3d->gp_flag |= V3D_GP_SHOW_EDIT_LINES;
167                                                 /* Skip startups that use the viewport color by default. */
168                                                 if (v3d->shading.background_type != V3D_SHADING_BACKGROUND_VIEWPORT) {
169                                                         copy_v3_fl(v3d->shading.background_color, 0.05f);
170                                                 }
171                                                 break;
172                                         }
173                                         case SPACE_FILE:
174                                         {
175                                                 SpaceFile *sfile = (SpaceFile *)sl;
176                                                 if (sfile->params) {
177                                                         const char *dir_default = BKE_appdir_folder_default();
178                                                         if (dir_default) {
179                                                                 STRNCPY(sfile->params->dir, dir_default);
180                                                                 sfile->params->file[0] = '\0';
181                                                         }
182                                                 }
183                                                 break;
184                                         }
185                                 }
186                         }
187                 }
188         }
189
190         if (app_template == NULL) {
191                 /* Name all screens by their workspaces (avoids 'Default.###' names). */
192                 {
193                         /* Default only has one window. */
194                         wmWindow *win = ((wmWindowManager *)bmain->wm.first)->windows.first;
195                         for (WorkSpace *workspace = bmain->workspaces.first; workspace; workspace = workspace->id.next) {
196                                 WorkSpaceLayout *layout = BKE_workspace_hook_layout_for_workspace_get(win->workspace_hook, workspace);
197                                 bScreen *screen = layout->screen;
198                                 BLI_strncpy(screen->id.name + 2, workspace->id.name + 2, sizeof(screen->id.name) - 2);
199                                 BLI_libblock_ensure_unique_name(bmain, screen->id.name);
200                         }
201                 }
202
203                 {
204                         /* 'UV Editing' should use UV mode. */
205                         bScreen *screen = BLI_findstring(&bmain->screen, "UV Editing", offsetof(ID, name) + 2);
206                         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
207                                 for (SpaceLink *sl = sa->spacedata.first; sl; sl = sl->next) {
208                                         if (sl->spacetype == SPACE_IMAGE) {
209                                                 SpaceImage *sima = (SpaceImage *)sl;
210                                                 if (sima->mode == SI_MODE_VIEW) {
211                                                         sima->mode = SI_MODE_UV;
212                                                 }
213                                         }
214                                 }
215                         }
216                 }
217         }
218
219         /* For 2D animation template. */
220         if (app_template && STREQ(app_template, "2D_Animation")) {
221                 for (WorkSpace *workspace = bmain->workspaces.first; workspace; workspace = workspace->id.next) {
222                         const char *name = workspace->id.name + 2;
223
224                         if (STREQ(name, "Drawing")) {
225                                 workspace->object_mode = OB_MODE_PAINT_GPENCIL;
226                         }
227                 }
228                 /* set object in drawing mode */
229                 for (Object *object = bmain->object.first; object; object = object->id.next) {
230                         if (object->type == OB_GPENCIL) {
231                                 bGPdata *gpd = (bGPdata *)object->data;
232                                 object->mode = OB_MODE_PAINT_GPENCIL;
233                                 gpd->flag |= GP_DATA_STROKE_PAINTMODE;
234                                 break;
235                         }
236                 }
237
238                 /* Be sure curfalloff and primitive are initializated */
239                 for (Scene *scene = bmain->scene.first; scene; scene = scene->id.next) {
240                         ToolSettings *ts = scene->toolsettings;
241                         if (ts->gp_sculpt.cur_falloff == NULL) {
242                                 ts->gp_sculpt.cur_falloff = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
243                                 CurveMapping *gp_falloff_curve = ts->gp_sculpt.cur_falloff;
244                                 curvemapping_initialize(gp_falloff_curve);
245                                 curvemap_reset(
246                                         gp_falloff_curve->cm,
247                                         &gp_falloff_curve->clipr,
248                                         CURVE_PRESET_GAUSS,
249                                         CURVEMAP_SLOPE_POSITIVE);
250                         }
251                         if (ts->gp_sculpt.cur_primitive == NULL) {
252                                 ts->gp_sculpt.cur_primitive = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
253                                 CurveMapping *gp_primitive_curve = ts->gp_sculpt.cur_primitive;
254                                 curvemapping_initialize(gp_primitive_curve);
255                                 curvemap_reset(
256                                         gp_primitive_curve->cm,
257                                         &gp_primitive_curve->clipr,
258                                         CURVE_PRESET_BELL,
259                                         CURVEMAP_SLOPE_POSITIVE);
260                         }
261                 }
262         }
263
264         /* For all builtin templates shipped with Blender. */
265         bool builtin_template = (
266                 !app_template ||
267                 STREQ(app_template, "2D_Animation") ||
268                 STREQ(app_template, "Sculpting") ||
269                 STREQ(app_template, "VFX") ||
270                 STREQ(app_template, "Video_Editing"));
271
272         if (builtin_template) {
273                 /* Clear all tools to use default options instead, ignore the tool saved in the file. */
274                 for (WorkSpace *workspace = bmain->workspaces.first; workspace; workspace = workspace->id.next) {
275                         while (!BLI_listbase_is_empty(&workspace->tools)) {
276                                 BKE_workspace_tool_remove(workspace, workspace->tools.first);
277                         }
278                 }
279
280                 for (bScreen *screen = bmain->screen.first; screen; screen = screen->id.next) {
281                         /* Hide channels in timelines. */
282                         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
283                                 SpaceAction *saction = (sa->spacetype == SPACE_ACTION) ? sa->spacedata.first : NULL;
284
285                                 if (saction && saction->mode == SACTCONT_TIMELINE) {
286                                         for (ARegion *ar = sa->regionbase.first; ar; ar = ar->next) {
287                                                 if (ar->regiontype == RGN_TYPE_CHANNELS) {
288                                                         ar->flag |= RGN_FLAG_HIDDEN;
289                                                 }
290                                         }
291                                 }
292                         }
293                 }
294
295                 for (Scene *scene = bmain->scene.first; scene; scene = scene->id.next) {
296                         BLI_strncpy(scene->r.engine, RE_engine_id_BLENDER_EEVEE, sizeof(scene->r.engine));
297
298                         scene->r.cfra = 1.0f;
299                         scene->r.displaymode = R_OUTPUT_WINDOW;
300
301                         if (app_template && STREQ(app_template, "Video_Editing")) {
302                                 /* Filmic is too slow, use default until it is optimized. */
303                                 STRNCPY(scene->view_settings.view_transform, "Default");
304                         }
305                         else {
306                                 /* AV Sync break physics sim caching, disable until that is fixed. */
307                                 scene->audio.flag &= ~AUDIO_SYNC;
308                                 scene->flag &= ~SCE_FRAME_DROP;
309                         }
310
311                         /* Don't enable compositing nodes. */
312                         if (scene->nodetree) {
313                                 ntreeFreeNestedTree(scene->nodetree);
314                                 MEM_freeN(scene->nodetree);
315                                 scene->nodetree = NULL;
316                                 scene->use_nodes = false;
317                         }
318
319                         /* Rename render layers. */
320                         BKE_view_layer_rename(bmain, scene, scene->view_layers.first, "View Layer");
321                 }
322
323                 /* Rename lamp objects. */
324                 rename_id_for_versioning(bmain, ID_OB, "Lamp", "Light");
325                 rename_id_for_versioning(bmain, ID_LA, "Lamp", "Light");
326
327                 for (Mesh *mesh = bmain->mesh.first; mesh; mesh = mesh->id.next) {
328                         /* Match default for new meshes. */
329                         mesh->smoothresh = DEG2RADF(30);
330                 }
331         }
332
333         for (bScreen *sc = bmain->screen.first; sc; sc = sc->id.next) {
334                 for (ScrArea *sa = sc->areabase.first; sa; sa = sa->next) {
335                         for (SpaceLink *sl = sa->spacedata.first; sl; sl = sl->next) {
336                                 if (sl->spacetype == SPACE_VIEW3D) {
337                                         View3D *v3d = (View3D *)sl;
338                                         v3d->shading.flag |= V3D_SHADING_SPECULAR_HIGHLIGHT;
339                                 }
340                         }
341                 }
342         }
343
344         for (Scene *scene = bmain->scene.first; scene; scene = scene->id.next) {
345                 copy_v3_v3(scene->display.light_direction, (float[3]){M_SQRT1_3, M_SQRT1_3, M_SQRT1_3});
346                 copy_v2_fl2(scene->safe_areas.title, 0.1f, 0.05f);
347                 copy_v2_fl2(scene->safe_areas.action, 0.035f, 0.035f);
348         }
349 }