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