2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2009 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Joshua Leung, Arystanbek Dyussenov
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/makesrna/intern/rna_scene_api.c
35 #include "BLI_utildefines.h"
36 #include "BLI_kdopbvh.h"
37 #include "BLI_path_util.h"
39 #include "RNA_define.h"
40 #include "RNA_enum_types.h"
42 #include "DNA_anim_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_scene_types.h"
46 #include "rna_internal.h" /* own include */
49 # include "../../alembic/ABC_alembic.h"
52 const EnumPropertyItem rna_enum_abc_compression_items[] = {
54 { ABC_ARCHIVE_OGAWA, "OGAWA", 0, "Ogawa", "" },
55 { ABC_ARCHIVE_HDF5, "HDF5", 0, "HDF5", "" },
57 { 0, NULL, 0, NULL, NULL }
62 #include "BKE_animsys.h"
63 #include "BKE_editmesh.h"
64 #include "BKE_global.h"
65 #include "BKE_image.h"
66 #include "BKE_scene.h"
67 #include "BKE_writeavi.h"
69 #include "DEG_depsgraph_query.h"
71 #include "ED_transform.h"
72 #include "ED_transform_snap_object_context.h"
73 #include "ED_uvedit.h"
76 # include "BPY_extern.h"
79 static void rna_Scene_frame_set(Scene *scene, Main *bmain, int frame, float subframe)
81 double cfra = (double)frame + (double)subframe;
83 CLAMP(cfra, MINAFRAME, MAXFRAME);
84 BKE_scene_frame_set(scene, cfra);
87 BPy_BEGIN_ALLOW_THREADS;
90 for (ViewLayer *view_layer = scene->view_layers.first;
92 view_layer = view_layer->next)
94 Depsgraph *depsgraph = BKE_scene_get_depsgraph(scene, view_layer, true);
95 BKE_scene_graph_update_for_newframe(depsgraph, bmain);
99 BPy_END_ALLOW_THREADS;
102 BKE_scene_camera_switch_update(scene);
104 /* don't do notifier when we're rendering, avoid some viewport crashes
105 * redrawing while the data is being modified for render */
106 if (!G.is_rendering) {
107 /* cant use NC_SCENE|ND_FRAME because this causes wm_event_do_notifiers to call
108 * BKE_scene_graph_update_for_newframe which will loose any un-keyed changes [#24690] */
109 /* WM_main_add_notifier(NC_SCENE|ND_FRAME, scene); */
111 /* instead just redraw the views */
112 WM_main_add_notifier(NC_WINDOW, NULL);
116 static void rna_Scene_uvedit_aspect(Scene *scene, Object *ob, float *aspect)
118 if ((ob->type == OB_MESH) && (ob->mode == OB_MODE_EDIT)) {
120 em = BKE_editmesh_from_object(ob);
121 if (EDBM_uv_check(em)) {
122 ED_uvedit_get_aspect(scene, ob, em->bm, aspect, aspect + 1);
127 aspect[0] = aspect[1] = 1.0f;
130 static void rna_Scene_update_tagged(Scene *scene, Main *bmain)
133 BPy_BEGIN_ALLOW_THREADS;
136 for (ViewLayer *view_layer = scene->view_layers.first;
138 view_layer = view_layer->next)
140 Depsgraph *depsgraph = BKE_scene_get_depsgraph(scene, view_layer, true);
141 BKE_scene_graph_update_tagged(depsgraph, bmain);
145 BPy_END_ALLOW_THREADS;
149 static void rna_SceneRender_get_frame_path(
150 RenderData *rd, Main *bmain, int frame, bool preview, const char *view, char *name)
152 const char *suffix = BKE_scene_multiview_view_suffix_get(rd, view);
154 /* avoid NULL pointer */
158 if (BKE_imtype_is_movie(rd->im_format.imtype)) {
159 BKE_movie_filepath_get(name, rd, preview != 0, suffix);
162 BKE_image_path_from_imformat(
163 name, rd->pic, BKE_main_blendfile_path(bmain), (frame == INT_MIN) ? rd->cfra : frame,
164 &rd->im_format, (rd->scemode & R_EXTENSION) != 0, true, suffix);
168 static void rna_Scene_ray_cast(
169 Scene *scene, Main *bmain, ViewLayer *view_layer,
170 float origin[3], float direction[3], float ray_dist,
171 bool *r_success, float r_location[3], float r_normal[3], int *r_index,
172 Object **r_ob, float r_obmat[16])
174 normalize_v3(direction);
176 Depsgraph *depsgraph = BKE_scene_get_depsgraph(scene, view_layer, true);
177 SnapObjectContext *sctx = ED_transform_snap_object_context_create(bmain, scene, depsgraph, 0);
179 bool ret = ED_transform_snap_object_project_ray_ex(
181 &(const struct SnapObjectParams){
182 .snap_select = SNAP_ALL,
184 origin, direction, &ray_dist,
185 r_location, r_normal, r_index,
186 r_ob, (float(*)[4])r_obmat);
188 ED_transform_snap_object_context_destroy(sctx);
190 if (r_ob != NULL && *r_ob != NULL) {
191 *r_ob = DEG_get_original_object(*r_ob);
200 unit_m4((float(*)[4])r_obmat);
206 static void rna_Scene_sequencer_editing_free(Scene *scene)
208 BKE_sequencer_editing_free(scene, true);
213 static void rna_Scene_alembic_export(
216 const char *filepath,
228 bool flatten_hierarchy,
229 bool visible_layers_only,
230 bool renderable_only,
232 bool use_subdiv_schema,
234 bool export_particles,
235 int compression_type,
242 /* We have to enable allow_threads, because we may change scene frame number
245 BPy_BEGIN_ALLOW_THREADS;
248 const struct AlembicExportParams params = {
249 .frame_start = frame_start,
250 .frame_end = frame_end,
252 .frame_samples_xform = xform_samples,
253 .frame_samples_shape = geom_samples,
255 .shutter_open = shutter_open,
256 .shutter_close = shutter_close,
258 .selected_only = selected_only,
262 .apply_subdiv = apply_subdiv,
263 .flatten_hierarchy = flatten_hierarchy,
264 .visible_layers_only = visible_layers_only,
265 .renderable_only = renderable_only,
266 .face_sets = face_sets,
267 .use_subdiv_schema = use_subdiv_schema,
268 .export_hair = export_hair,
269 .export_particles = export_particles,
270 .compression_type = compression_type,
272 .triangulate = triangulate,
273 .quad_method = quad_method,
274 .ngon_method = ngon_method,
276 .global_scale = scale,
279 ABC_export(scene, C, filepath, ¶ms, true);
282 BPy_END_ALLOW_THREADS;
290 void RNA_api_scene(StructRNA *srna)
295 func = RNA_def_function(srna, "frame_set", "rna_Scene_frame_set");
296 RNA_def_function_ui_description(func, "Set scene frame updating all objects immediately");
297 parm = RNA_def_int(func, "frame", 0, MINAFRAME, MAXFRAME, "", "Frame number to set", MINAFRAME, MAXFRAME);
298 RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
299 RNA_def_float(func, "subframe", 0.0, 0.0, 1.0, "", "Sub-frame time, between 0.0 and 1.0", 0.0, 1.0);
300 RNA_def_function_flag(func, FUNC_USE_MAIN);
302 func = RNA_def_function(srna, "update", "rna_Scene_update_tagged");
303 RNA_def_function_ui_description(func,
304 "Update data tagged to be updated from previous access to data or operators");
305 RNA_def_function_flag(func, FUNC_USE_MAIN);
307 func = RNA_def_function(srna, "uvedit_aspect", "rna_Scene_uvedit_aspect");
308 RNA_def_function_ui_description(func, "Get uv aspect for current object");
309 parm = RNA_def_pointer(func, "object", "Object", "", "Object");
310 RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
311 parm = RNA_def_float_vector(func, "result", 2, NULL, 0.0f, FLT_MAX, "", "aspect", 0.0f, FLT_MAX);
312 RNA_def_parameter_flags(parm, PROP_THICK_WRAP, 0);
313 RNA_def_function_output(func, parm);
316 func = RNA_def_function(srna, "ray_cast", "rna_Scene_ray_cast");
317 RNA_def_function_flag(func, FUNC_USE_MAIN);
318 RNA_def_function_ui_description(func, "Cast a ray onto in object space");
319 parm = RNA_def_pointer(func, "view_layer", "ViewLayer", "", "Scene Layer");
320 RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
321 /* ray start and end */
322 parm = RNA_def_float_vector(func, "origin", 3, NULL, -FLT_MAX, FLT_MAX, "", "", -1e4, 1e4);
323 RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
324 parm = RNA_def_float_vector(func, "direction", 3, NULL, -FLT_MAX, FLT_MAX, "", "", -1e4, 1e4);
325 RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
326 RNA_def_float(func, "distance", BVH_RAYCAST_DIST_MAX, 0.0, BVH_RAYCAST_DIST_MAX,
327 "", "Maximum distance", 0.0, BVH_RAYCAST_DIST_MAX);
328 /* return location and normal */
329 parm = RNA_def_boolean(func, "result", 0, "", "");
330 RNA_def_function_output(func, parm);
331 parm = RNA_def_float_vector(func, "location", 3, NULL, -FLT_MAX, FLT_MAX, "Location",
332 "The hit location of this ray cast", -1e4, 1e4);
333 RNA_def_parameter_flags(parm, PROP_THICK_WRAP, 0);
334 RNA_def_function_output(func, parm);
335 parm = RNA_def_float_vector(func, "normal", 3, NULL, -FLT_MAX, FLT_MAX, "Normal",
336 "The face normal at the ray cast hit location", -1e4, 1e4);
337 RNA_def_parameter_flags(parm, PROP_THICK_WRAP, 0);
338 RNA_def_function_output(func, parm);
339 parm = RNA_def_int(func, "index", 0, 0, 0, "", "The face index, -1 when original data isn't available", 0, 0);
340 RNA_def_function_output(func, parm);
341 parm = RNA_def_pointer(func, "object", "Object", "", "Ray cast object");
342 RNA_def_function_output(func, parm);
343 parm = RNA_def_float_matrix(func, "matrix", 4, 4, NULL, 0.0f, 0.0f, "", "Matrix", 0.0f, 0.0f);
344 RNA_def_function_output(func, parm);
347 func = RNA_def_function(srna, "sequence_editor_create", "BKE_sequencer_editing_ensure");
348 RNA_def_function_ui_description(func, "Ensure sequence editor is valid in this scene");
349 parm = RNA_def_pointer(func, "sequence_editor", "SequenceEditor", "", "New sequence editor data or NULL");
350 RNA_def_function_return(func, parm);
352 func = RNA_def_function(srna, "sequence_editor_clear", "rna_Scene_sequencer_editing_free");
353 RNA_def_function_ui_description(func, "Clear sequence editor in this scene");
356 /* XXX Deprecated, will be removed in 2.8 in favour of calling the export operator. */
357 func = RNA_def_function(srna, "alembic_export", "rna_Scene_alembic_export");
358 RNA_def_function_ui_description(func, "Export to Alembic file (deprecated, use the Alembic export operator)");
360 parm = RNA_def_string(func, "filepath", NULL, FILE_MAX, "File Path", "File path to write Alembic file");
361 RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
362 RNA_def_property_subtype(parm, PROP_FILEPATH); /* allow non utf8 */
364 RNA_def_int(func, "frame_start", 1, INT_MIN, INT_MAX, "Start", "Start Frame", INT_MIN, INT_MAX);
365 RNA_def_int(func, "frame_end", 1, INT_MIN, INT_MAX, "End", "End Frame", INT_MIN, INT_MAX);
366 RNA_def_int(func, "xform_samples", 1, 1, 128, "Xform samples", "Transform samples per frame", 1, 128);
367 RNA_def_int(func, "geom_samples", 1, 1, 128, "Geom samples", "Geometry samples per frame", 1, 128);
368 RNA_def_float(func, "shutter_open", 0.0f, -1.0f, 1.0f, "Shutter open", "", -1.0f, 1.0f);
369 RNA_def_float(func, "shutter_close", 1.0f, -1.0f, 1.0f, "Shutter close", "", -1.0f, 1.0f);
370 RNA_def_boolean(func, "selected_only" , 0, "Selected only", "Export only selected objects");
371 RNA_def_boolean(func, "uvs" , 1, "UVs", "Export UVs");
372 RNA_def_boolean(func, "normals" , 1, "Normals", "Export normals");
373 RNA_def_boolean(func, "vcolors" , 0, "Vertex colors", "Export vertex colors");
374 RNA_def_boolean(func, "apply_subdiv" , 1, "Subsurfs as meshes", "Export subdivision surfaces as meshes");
375 RNA_def_boolean(func, "flatten" , 0, "Flatten hierarchy", "Flatten hierarchy");
376 RNA_def_boolean(func, "visible_layers_only" , 0, "Visible layers only", "Export only objects in visible layers");
377 RNA_def_boolean(func, "renderable_only" , 0, "Renderable objects only", "Export only objects marked renderable in the outliner");
378 RNA_def_boolean(func, "face_sets" , 0, "Facesets", "Export face sets");
379 RNA_def_boolean(func, "subdiv_schema", 0, "Use Alembic subdivision Schema", "Use Alembic subdivision Schema");
380 RNA_def_boolean(func, "export_hair", 1, "Export Hair", "Exports hair particle systems as animated curves");
381 RNA_def_boolean(func, "export_particles", 1, "Export Particles", "Exports non-hair particle systems");
382 RNA_def_enum(func, "compression_type", rna_enum_abc_compression_items, 0, "Compression", "");
383 RNA_def_boolean(func, "packuv", 0, "Export with packed UV islands", "Export with packed UV islands");
384 RNA_def_float(func, "scale", 1.0f, 0.0001f, 1000.0f, "Scale", "Value by which to enlarge or shrink the objects with respect to the world's origin", 0.0001f, 1000.0f);
385 RNA_def_boolean(func, "triangulate", 0, "Triangulate", "Export Polygons (Quads & NGons) as Triangles");
386 RNA_def_enum(func, "quad_method", rna_enum_modifier_triangulate_quad_method_items, 0, "Quad Method", "Method for splitting the quads into triangles");
387 RNA_def_enum(func, "ngon_method", rna_enum_modifier_triangulate_quad_method_items, 0, "Polygon Method", "Method for splitting the polygons into triangles");
389 RNA_def_function_flag(func, FUNC_USE_CONTEXT);
394 void RNA_api_scene_render(StructRNA *srna)
399 func = RNA_def_function(srna, "frame_path", "rna_SceneRender_get_frame_path");
400 RNA_def_function_flag(func, FUNC_USE_MAIN);
401 RNA_def_function_ui_description(func, "Return the absolute path to the filename to be written for a given frame");
402 RNA_def_int(func, "frame", INT_MIN, INT_MIN, INT_MAX, "",
403 "Frame number to use, if unset the current frame will be used", MINAFRAME, MAXFRAME);
404 RNA_def_boolean(func, "preview", 0, "Preview", "Use preview range");
405 RNA_def_string_file_path(func, "view", NULL, FILE_MAX, "View",
406 "The name of the view to use to replace the \"%\" chars");
407 parm = RNA_def_string_file_path(func, "filepath", NULL, FILE_MAX, "File Path",
408 "The resulting filepath from the scenes render settings");
409 RNA_def_parameter_flags(parm, PROP_THICK_WRAP, 0); /* needed for string return value */
410 RNA_def_function_output(func, parm);