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 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_depsgraph.h"
64 #include "BKE_editmesh.h"
65 #include "BKE_global.h"
66 #include "BKE_image.h"
67 #include "BKE_scene.h"
68 #include "BKE_writeavi.h"
70 #include "ED_transform.h"
71 #include "ED_transform_snap_object_context.h"
72 #include "ED_uvedit.h"
75 # include "BPY_extern.h"
78 static void rna_Scene_frame_set(Scene *scene, int frame, float subframe)
80 double cfra = (double)frame + (double)subframe;
82 CLAMP(cfra, MINAFRAME, MAXFRAME);
83 BKE_scene_frame_set(scene, cfra);
86 BPy_BEGIN_ALLOW_THREADS;
89 /* It's possible that here we're including layers which were never visible before. */
90 BKE_scene_update_for_newframe_ex(G.main->eval_ctx, G.main, scene, (1 << 20) - 1, true);
93 BPy_END_ALLOW_THREADS;
96 BKE_scene_camera_switch_update(scene);
98 /* don't do notifier when we're rendering, avoid some viewport crashes
99 * redrawing while the data is being modified for render */
100 if (!G.is_rendering) {
101 /* cant use NC_SCENE|ND_FRAME because this causes wm_event_do_notifiers to call
102 * BKE_scene_update_for_newframe which will loose any un-keyed changes [#24690] */
103 /* WM_main_add_notifier(NC_SCENE|ND_FRAME, scene); */
105 /* instead just redraw the views */
106 WM_main_add_notifier(NC_WINDOW, NULL);
110 static void rna_Scene_uvedit_aspect(Scene *scene, Object *ob, float *aspect)
112 if ((ob->type == OB_MESH) && (ob->mode == OB_MODE_EDIT)) {
114 em = BKE_editmesh_from_object(ob);
115 if (EDBM_mtexpoly_check(em)) {
116 ED_uvedit_get_aspect(scene, ob, em->bm, aspect, aspect + 1);
121 aspect[0] = aspect[1] = 1.0f;
124 static void rna_Scene_update_tagged(Scene *scene)
127 BPy_BEGIN_ALLOW_THREADS;
130 BKE_scene_update_tagged(G.main->eval_ctx, G.main, scene);
133 BPy_END_ALLOW_THREADS;
137 static void rna_SceneRender_get_frame_path(RenderData *rd, int frame, int preview, const char *view, char *name)
139 const char *suffix = BKE_scene_multiview_view_suffix_get(rd, view);
141 /* avoid NULL pointer */
145 if (BKE_imtype_is_movie(rd->im_format.imtype)) {
146 BKE_movie_filepath_get(name, rd, preview != 0, suffix);
149 BKE_image_path_from_imformat(
150 name, rd->pic, G.main->name, (frame == INT_MIN) ? rd->cfra : frame,
151 &rd->im_format, (rd->scemode & R_EXTENSION) != 0, true, suffix);
155 static void rna_Scene_ray_cast(
156 Scene *scene, float origin[3], float direction[3], float ray_dist,
157 int *r_success, float r_location[3], float r_normal[3], int *r_index,
158 Object **r_ob, float r_obmat[16])
160 normalize_v3(direction);
162 SnapObjectContext *sctx = ED_transform_snap_object_context_create(
165 bool ret = ED_transform_snap_object_project_ray_ex(
168 &(const struct SnapObjectParams){
169 .snap_select = SNAP_ALL,
171 origin, direction, &ray_dist,
172 r_location, r_normal, r_index,
173 r_ob, (float(*)[4])r_obmat);
175 ED_transform_snap_object_context_destroy(sctx);
183 unit_m4((float(*)[4])r_obmat);
191 static void rna_Scene_alembic_export(
194 const char *filepath,
206 int flatten_hierarchy,
207 int visible_layers_only,
210 int use_subdiv_schema,
212 int export_particles,
213 int compression_type,
220 /* We have to enable allow_threads, because we may change scene frame number
223 BPy_BEGIN_ALLOW_THREADS;
226 const struct AlembicExportParams params = {
227 .frame_start = frame_start,
228 .frame_end = frame_end,
230 .frame_step_xform = 1.0 / (double)xform_samples,
231 .frame_step_shape = 1.0 / (double)geom_samples,
233 .shutter_open = shutter_open,
234 .shutter_close = shutter_close,
236 .selected_only = selected_only,
240 .apply_subdiv = apply_subdiv,
241 .flatten_hierarchy = flatten_hierarchy,
242 .visible_layers_only = visible_layers_only,
243 .renderable_only = renderable_only,
244 .face_sets = face_sets,
245 .use_subdiv_schema = use_subdiv_schema,
246 .export_hair = export_hair,
247 .export_particles = export_particles,
248 .compression_type = compression_type,
250 .triangulate = triangulate,
251 .quad_method = quad_method,
252 .ngon_method = ngon_method,
254 .global_scale = scale,
257 ABC_export(scene, C, filepath, ¶ms);
260 BPy_END_ALLOW_THREADS;
267 /* don't remove this, as COLLADA exporting cannot be done through operators in render() callback. */
268 #include "../../collada/collada.h"
270 /* Note: This definition must match to the generated function call */
271 static void rna_Scene_collada_export(
273 const char *filepath,
276 int export_mesh_type,
278 int include_children,
279 int include_armatures,
280 int include_shapekeys,
281 int deform_bones_only,
283 int include_uv_textures,
284 int include_material_textures,
285 int use_texture_copies,
287 int use_object_instantiation,
288 int use_blender_profile,
290 int export_transformation_type,
295 collada_export(scene,
309 include_material_textures,
313 use_object_instantiation,
317 export_transformation_type,
327 void RNA_api_scene(StructRNA *srna)
332 func = RNA_def_function(srna, "frame_set", "rna_Scene_frame_set");
333 RNA_def_function_ui_description(func, "Set scene frame updating all objects immediately");
334 parm = RNA_def_int(func, "frame", 0, MINAFRAME, MAXFRAME, "", "Frame number to set", MINAFRAME, MAXFRAME);
335 RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
336 RNA_def_float(func, "subframe", 0.0, 0.0, 1.0, "", "Sub-frame time, between 0.0 and 1.0", 0.0, 1.0);
338 func = RNA_def_function(srna, "update", "rna_Scene_update_tagged");
339 RNA_def_function_ui_description(func,
340 "Update data tagged to be updated from previous access to data or operators");
342 func = RNA_def_function(srna, "uvedit_aspect", "rna_Scene_uvedit_aspect");
343 RNA_def_function_ui_description(func, "Get uv aspect for current object");
344 parm = RNA_def_pointer(func, "object", "Object", "", "Object");
345 RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
346 parm = RNA_def_float_vector(func, "result", 2, NULL, 0.0f, FLT_MAX, "", "aspect", 0.0f, FLT_MAX);
347 RNA_def_parameter_flags(parm, PROP_THICK_WRAP, 0);
348 RNA_def_function_output(func, parm);
351 func = RNA_def_function(srna, "ray_cast", "rna_Scene_ray_cast");
352 RNA_def_function_ui_description(func, "Cast a ray onto in object space");
353 /* ray start and end */
354 parm = RNA_def_float_vector(func, "origin", 3, NULL, -FLT_MAX, FLT_MAX, "", "", -1e4, 1e4);
355 RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
356 parm = RNA_def_float_vector(func, "direction", 3, NULL, -FLT_MAX, FLT_MAX, "", "", -1e4, 1e4);
357 RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
358 RNA_def_float(func, "distance", BVH_RAYCAST_DIST_MAX, 0.0, BVH_RAYCAST_DIST_MAX,
359 "", "Maximum distance", 0.0, BVH_RAYCAST_DIST_MAX);
360 /* return location and normal */
361 parm = RNA_def_boolean(func, "result", 0, "", "");
362 RNA_def_function_output(func, parm);
363 parm = RNA_def_float_vector(func, "location", 3, NULL, -FLT_MAX, FLT_MAX, "Location",
364 "The hit location of this ray cast", -1e4, 1e4);
365 RNA_def_parameter_flags(parm, PROP_THICK_WRAP, 0);
366 RNA_def_function_output(func, parm);
367 parm = RNA_def_float_vector(func, "normal", 3, NULL, -FLT_MAX, FLT_MAX, "Normal",
368 "The face normal at the ray cast hit location", -1e4, 1e4);
369 RNA_def_parameter_flags(parm, PROP_THICK_WRAP, 0);
370 RNA_def_function_output(func, parm);
371 parm = RNA_def_int(func, "index", 0, 0, 0, "", "The face index, -1 when original data isn't available", 0, 0);
372 RNA_def_function_output(func, parm);
373 parm = RNA_def_pointer(func, "object", "Object", "", "Ray cast object");
374 RNA_def_function_output(func, parm);
375 parm = RNA_def_float_matrix(func, "matrix", 4, 4, NULL, 0.0f, 0.0f, "", "Matrix", 0.0f, 0.0f);
376 RNA_def_function_output(func, parm);
379 /* don't remove this, as COLLADA exporting cannot be done through operators in render() callback. */
380 func = RNA_def_function(srna, "collada_export", "rna_Scene_collada_export");
381 parm = RNA_def_string(func, "filepath", NULL, FILE_MAX, "File Path", "File path to write Collada file");
382 RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
383 RNA_def_property_subtype(parm, PROP_FILEPATH); /* allow non utf8 */
385 RNA_def_boolean(func, "apply_modifiers", false,
386 "Apply Modifiers", "Apply modifiers to exported mesh (non destructive))");
388 RNA_def_int(func, "export_mesh_type", 0, INT_MIN, INT_MAX,
389 "Resolution", "Modifier resolution for export", INT_MIN, INT_MAX);
391 RNA_def_boolean(func, "selected", false, "Selection Only", "Export only selected elements");
393 RNA_def_boolean(func, "include_children", false,
394 "Include Children", "Export all children of selected objects (even if not selected)");
396 RNA_def_boolean(func, "include_armatures", false,
397 "Include Armatures", "Export related armatures (even if not selected)");
399 RNA_def_boolean(func, "include_shapekeys", true, "Include Shape Keys", "Export all Shape Keys from Mesh Objects");
401 RNA_def_boolean(func, "deform_bones_only", false,
402 "Deform Bones only", "Only export deforming bones with armatures");
404 RNA_def_boolean(func, "active_uv_only", false, "Only Selected UV Map", "Export only the selected UV Map");
406 RNA_def_boolean(func, "include_uv_textures", false,
407 "Include UV Textures", "Export textures assigned to the object UV Maps");
409 RNA_def_boolean(func, "include_material_textures", false,
410 "Include Material Textures", "Export textures assigned to the object Materials");
412 RNA_def_boolean(func, "use_texture_copies", true,
413 "Copy", "Copy textures to same folder where the .dae file is exported");
415 RNA_def_boolean(func, "triangulate", true, "Triangulate", "Export Polygons (Quads & NGons) as Triangles");
417 RNA_def_boolean(func, "use_object_instantiation", true,
418 "Use Object Instances", "Instantiate multiple Objects from same Data");
420 RNA_def_boolean(func, "use_blender_profile", true, "Use Blender Profile",
421 "Export additional Blender specific information (for material, shaders, bones, etc.)");
423 RNA_def_boolean(func, "sort_by_name", false, "Sort by Object name", "Sort exported data by Object name");
425 RNA_def_int(func, "export_transformation_type", 0, INT_MIN, INT_MAX,
426 "Transform", "Transformation type for translation, scale and rotation", INT_MIN, INT_MAX);
428 RNA_def_boolean(func, "open_sim", false,
429 "Export to SL/OpenSim", "Compatibility mode for SL, OpenSim and other compatible online worlds");
431 RNA_def_boolean(func, "limit_precision", false,
433 "Reduce the precision of the exported data to 6 digits");
435 RNA_def_boolean(func, "keep_bind_info", false,
437 "Store bind pose information in custom bone properties for later use during Collada export");
442 func = RNA_def_function(srna, "alembic_export", "rna_Scene_alembic_export");
443 RNA_def_function_ui_description(func, "Export to Alembic file");
445 parm = RNA_def_string(func, "filepath", NULL, FILE_MAX, "File Path", "File path to write Alembic file");
446 RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
447 RNA_def_property_subtype(parm, PROP_FILEPATH); /* allow non utf8 */
449 RNA_def_int(func, "frame_start", 1, INT_MIN, INT_MAX, "Start", "Start Frame", INT_MIN, INT_MAX);
450 RNA_def_int(func, "frame_end", 1, INT_MIN, INT_MAX, "End", "End Frame", INT_MIN, INT_MAX);
451 RNA_def_int(func, "xform_samples", 1, 1, 128, "Xform samples", "Transform samples per frame", 1, 128);
452 RNA_def_int(func, "geom_samples", 1, 1, 128, "Geom samples", "Geometry samples per frame", 1, 128);
453 RNA_def_float(func, "shutter_open", 0.0f, -1.0f, 1.0f, "Shutter open", "", -1.0f, 1.0f);
454 RNA_def_float(func, "shutter_close", 1.0f, -1.0f, 1.0f, "Shutter close", "", -1.0f, 1.0f);
455 RNA_def_boolean(func, "selected_only" , 0, "Selected only", "Export only selected objects");
456 RNA_def_boolean(func, "uvs" , 1, "UVs", "Export UVs");
457 RNA_def_boolean(func, "normals" , 1, "Normals", "Export cormals");
458 RNA_def_boolean(func, "vcolors" , 0, "Vertex colors", "Export vertex colors");
459 RNA_def_boolean(func, "apply_subdiv" , 1, "Subsurfs as meshes", "Export subdivision surfaces as meshes");
460 RNA_def_boolean(func, "flatten" , 0, "Flatten hierarchy", "Flatten hierarchy");
461 RNA_def_boolean(func, "visible_layers_only" , 0, "Visible layers only", "Export only objects in visible layers");
462 RNA_def_boolean(func, "renderable_only" , 0, "Renderable objects only", "Export only objects marked renderable in the outliner");
463 RNA_def_boolean(func, "face_sets" , 0, "Facesets", "Export face sets");
464 RNA_def_boolean(func, "subdiv_schema", 0, "Use Alembic subdivision Schema", "Use Alembic subdivision Schema");
465 RNA_def_boolean(func, "export_hair", 1, "Export Hair", "Exports hair particle systems as animated curves");
466 RNA_def_boolean(func, "export_particles", 1, "Export Particles", "Exports non-hair particle systems");
467 RNA_def_enum(func, "compression_type", rna_enum_abc_compression_items, 0, "Compression", "");
468 RNA_def_boolean(func, "packuv" , 0, "Export with packed UV islands", "Export with packed UV islands");
469 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);
470 RNA_def_boolean(func, "triangulate", 0, "Triangulate", "Export Polygons (Quads & NGons) as Triangles");
471 RNA_def_enum(func, "quad_method", rna_enum_modifier_triangulate_quad_method_items, 0, "Quad Method", "Method for splitting the quads into triangles");
472 RNA_def_enum(func, "ngon_method", rna_enum_modifier_triangulate_quad_method_items, 0, "Polygon Method", "Method for splitting the polygons into triangles");
474 RNA_def_function_flag(func, FUNC_USE_CONTEXT);
479 void RNA_api_scene_render(StructRNA *srna)
484 func = RNA_def_function(srna, "frame_path", "rna_SceneRender_get_frame_path");
485 RNA_def_function_ui_description(func, "Return the absolute path to the filename to be written for a given frame");
486 RNA_def_int(func, "frame", INT_MIN, INT_MIN, INT_MAX, "",
487 "Frame number to use, if unset the current frame will be used", MINAFRAME, MAXFRAME);
488 RNA_def_boolean(func, "preview", 0, "Preview", "Use preview range");
489 RNA_def_string_file_path(func, "view", NULL, FILE_MAX, "View",
490 "The name of the view to use to replace the \"%\" chars");
491 parm = RNA_def_string_file_path(func, "filepath", NULL, FILE_MAX, "File Path",
492 "The resulting filepath from the scenes render settings");
493 RNA_def_parameter_flags(parm, PROP_THICK_WRAP, 0); /* needed for string return value */
494 RNA_def_function_output(func, parm);