Blender  V2.93
rna_scene_api.c
Go to the documentation of this file.
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  * The Original Code is Copyright (C) 2009 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include <stdio.h>
25 #include <stdlib.h>
26 
27 #include "BLI_kdopbvh.h"
28 #include "BLI_path_util.h"
29 #include "BLI_utildefines.h"
30 
31 #include "RNA_define.h"
32 #include "RNA_enum_types.h"
33 
34 #include "DNA_anim_types.h"
35 #include "DNA_object_types.h"
36 #include "DNA_scene_types.h"
37 
38 #include "rna_internal.h" /* own include */
39 
40 #ifdef WITH_ALEMBIC
41 # include "ABC_alembic.h"
42 #endif
43 
44 #ifdef RNA_RUNTIME
45 
46 # include "BKE_editmesh.h"
47 # include "BKE_global.h"
48 # include "BKE_image.h"
49 # include "BKE_scene.h"
50 # include "BKE_writeavi.h"
51 
52 # include "DEG_depsgraph_query.h"
53 
54 # include "ED_transform.h"
56 # include "ED_uvedit.h"
57 
58 # ifdef WITH_PYTHON
59 # include "BPY_extern.h"
60 # endif
61 
62 static void rna_Scene_frame_set(Scene *scene, Main *bmain, int frame, float subframe)
63 {
64  double cfra = (double)frame + (double)subframe;
65 
66  CLAMP(cfra, MINAFRAME, MAXFRAME);
68 
69 # ifdef WITH_PYTHON
71 # endif
72 
73  for (ViewLayer *view_layer = scene->view_layers.first; view_layer != NULL;
74  view_layer = view_layer->next) {
75  Depsgraph *depsgraph = BKE_scene_ensure_depsgraph(bmain, scene, view_layer);
77  }
78 
79 # ifdef WITH_PYTHON
81 # endif
82 
84  for (bScreen *screen = bmain->screens.first; screen; screen = screen->id.next) {
86  }
87  }
88 
89  /* don't do notifier when we're rendering, avoid some viewport crashes
90  * redrawing while the data is being modified for render */
91  if (!G.is_rendering) {
92  /* cant use NC_SCENE|ND_FRAME because this causes wm_event_do_notifiers to call
93  * BKE_scene_graph_update_for_newframe which will lose any un-keyed changes T24690. */
94  /* WM_main_add_notifier(NC_SCENE|ND_FRAME, scene); */
95 
96  /* instead just redraw the views */
98  }
99 }
100 
101 static void rna_Scene_uvedit_aspect(Scene *UNUSED(scene), Object *ob, float *aspect)
102 {
103  if ((ob->type == OB_MESH) && (ob->mode == OB_MODE_EDIT)) {
104  BMEditMesh *em;
105  em = BKE_editmesh_from_object(ob);
106  if (EDBM_uv_check(em)) {
107  ED_uvedit_get_aspect(ob, aspect, aspect + 1);
108  return;
109  }
110  }
111 
112  aspect[0] = aspect[1] = 1.0f;
113 }
114 
115 static void rna_SceneRender_get_frame_path(
116  RenderData *rd, Main *bmain, int frame, bool preview, const char *view, char *name)
117 {
118  const char *suffix = BKE_scene_multiview_view_suffix_get(rd, view);
119 
120  /* avoid NULL pointer */
121  if (!suffix) {
122  suffix = "";
123  }
124 
126  BKE_movie_filepath_get(name, rd, preview != 0, suffix);
127  }
128  else {
130  rd->pic,
132  (frame == INT_MIN) ? rd->cfra : frame,
133  &rd->im_format,
134  (rd->scemode & R_EXTENSION) != 0,
135  true,
136  suffix);
137  }
138 }
139 
140 static void rna_Scene_ray_cast(Scene *scene,
142  float origin[3],
143  float direction[3],
144  float ray_dist,
145  bool *r_success,
146  float r_location[3],
147  float r_normal[3],
148  int *r_index,
149  Object **r_ob,
150  float r_obmat[16])
151 {
152  normalize_v3(direction);
154 
156  depsgraph,
157  &(const struct SnapObjectParams){
158  .snap_select = SNAP_ALL,
159  },
160  origin,
161  direction,
162  &ray_dist,
163  r_location,
164  r_normal,
165  r_index,
166  r_ob,
167  (float(*)[4])r_obmat);
168 
170 
171  if (r_ob != NULL && *r_ob != NULL) {
172  *r_ob = DEG_get_original_object(*r_ob);
173  }
174 
175  if (ret) {
176  *r_success = true;
177  }
178  else {
179  *r_success = false;
180 
181  unit_m4((float(*)[4])r_obmat);
182  zero_v3(r_location);
183  zero_v3(r_normal);
184  }
185 }
186 
187 static void rna_Scene_sequencer_editing_free(Scene *scene)
188 {
189  SEQ_editing_free(scene, true);
190 }
191 
192 # ifdef WITH_ALEMBIC
193 
194 static void rna_Scene_alembic_export(Scene *scene,
195  bContext *C,
196  const char *filepath,
197  int frame_start,
198  int frame_end,
199  int xform_samples,
200  int geom_samples,
201  float shutter_open,
202  float shutter_close,
203  bool selected_only,
204  bool uvs,
205  bool normals,
206  bool vcolors,
207  bool apply_subdiv,
208  bool flatten_hierarchy,
209  bool visible_objects_only,
210  bool face_sets,
211  bool use_subdiv_schema,
212  bool export_hair,
213  bool export_particles,
214  bool packuv,
215  float scale,
216  bool triangulate,
217  int quad_method,
218  int ngon_method)
219 {
220 /* We have to enable allow_threads, because we may change scene frame number
221  * during export. */
222 # ifdef WITH_PYTHON
224 # endif
225 
226  const struct AlembicExportParams params = {
227  .frame_start = frame_start,
228  .frame_end = frame_end,
229 
230  .frame_samples_xform = xform_samples,
231  .frame_samples_shape = geom_samples,
232 
233  .shutter_open = shutter_open,
234  .shutter_close = shutter_close,
235 
236  .selected_only = selected_only,
237  .uvs = uvs,
238  .normals = normals,
239  .vcolors = vcolors,
240  .apply_subdiv = apply_subdiv,
241  .flatten_hierarchy = flatten_hierarchy,
242  .visible_objects_only = visible_objects_only,
243  .face_sets = face_sets,
244  .use_subdiv_schema = use_subdiv_schema,
245  .export_hair = export_hair,
246  .export_particles = export_particles,
247  .packuv = packuv,
248  .triangulate = triangulate,
249  .quad_method = quad_method,
250  .ngon_method = ngon_method,
251 
252  .global_scale = scale,
253  };
254 
255  ABC_export(scene, C, filepath, &params, true);
256 
257 # ifdef WITH_PYTHON
259 # endif
260 }
261 
262 # endif
263 
264 #else
265 
267 {
268  FunctionRNA *func;
269  PropertyRNA *parm;
270 
271  func = RNA_def_function(srna, "frame_set", "rna_Scene_frame_set");
272  RNA_def_function_ui_description(func, "Set scene frame updating all objects immediately");
273  parm = RNA_def_int(
274  func, "frame", 0, MINAFRAME, MAXFRAME, "", "Frame number to set", MINAFRAME, MAXFRAME);
277  func, "subframe", 0.0, 0.0, 1.0, "", "Subframe time, between 0.0 and 1.0", 0.0, 1.0);
279 
280  func = RNA_def_function(srna, "uvedit_aspect", "rna_Scene_uvedit_aspect");
281  RNA_def_function_ui_description(func, "Get uv aspect for current object");
282  parm = RNA_def_pointer(func, "object", "Object", "", "Object");
284  parm = RNA_def_float_vector(func, "result", 2, NULL, 0.0f, FLT_MAX, "", "aspect", 0.0f, FLT_MAX);
286  RNA_def_function_output(func, parm);
287 
288  /* Ray Cast */
289  func = RNA_def_function(srna, "ray_cast", "rna_Scene_ray_cast");
290  RNA_def_function_ui_description(func, "Cast a ray onto in object space");
291 
292  parm = RNA_def_pointer(func, "depsgraph", "Depsgraph", "", "The current dependency graph");
294  /* ray start and end */
295  parm = RNA_def_float_vector(func, "origin", 3, NULL, -FLT_MAX, FLT_MAX, "", "", -1e4, 1e4);
297  parm = RNA_def_float_vector(func, "direction", 3, NULL, -FLT_MAX, FLT_MAX, "", "", -1e4, 1e4);
299  RNA_def_float(func,
300  "distance",
302  0.0,
304  "",
305  "Maximum distance",
306  0.0,
308  /* return location and normal */
309  parm = RNA_def_boolean(func, "result", 0, "", "");
310  RNA_def_function_output(func, parm);
311  parm = RNA_def_float_vector(func,
312  "location",
313  3,
314  NULL,
315  -FLT_MAX,
316  FLT_MAX,
317  "Location",
318  "The hit location of this ray cast",
319  -1e4,
320  1e4);
322  RNA_def_function_output(func, parm);
323  parm = RNA_def_float_vector(func,
324  "normal",
325  3,
326  NULL,
327  -FLT_MAX,
328  FLT_MAX,
329  "Normal",
330  "The face normal at the ray cast hit location",
331  -1e4,
332  1e4);
334  RNA_def_function_output(func, parm);
335  parm = RNA_def_int(
336  func, "index", 0, 0, 0, "", "The face index, -1 when original data isn't available", 0, 0);
337  RNA_def_function_output(func, parm);
338  parm = RNA_def_pointer(func, "object", "Object", "", "Ray cast object");
339  RNA_def_function_output(func, parm);
340  parm = RNA_def_float_matrix(func, "matrix", 4, 4, NULL, 0.0f, 0.0f, "", "Matrix", 0.0f, 0.0f);
341  RNA_def_function_output(func, parm);
342 
343  /* Sequencer. */
344  func = RNA_def_function(srna, "sequence_editor_create", "SEQ_editing_ensure");
345  RNA_def_function_ui_description(func, "Ensure sequence editor is valid in this scene");
346  parm = RNA_def_pointer(
347  func, "sequence_editor", "SequenceEditor", "", "New sequence editor data or NULL");
348  RNA_def_function_return(func, parm);
349 
350  func = RNA_def_function(srna, "sequence_editor_clear", "rna_Scene_sequencer_editing_free");
351  RNA_def_function_ui_description(func, "Clear sequence editor in this scene");
352 
353 # ifdef WITH_ALEMBIC
354  /* XXX Deprecated, will be removed in 2.8 in favor of calling the export operator. */
355  func = RNA_def_function(srna, "alembic_export", "rna_Scene_alembic_export");
357  func, "Export to Alembic file (deprecated, use the Alembic export operator)");
358 
359  parm = RNA_def_string(
360  func, "filepath", NULL, FILE_MAX, "File Path", "File path to write Alembic file");
362  RNA_def_property_subtype(parm, PROP_FILEPATH); /* allow non utf8 */
363 
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(
367  func, "xform_samples", 1, 1, 128, "Xform samples", "Transform samples per frame", 1, 128);
368  RNA_def_int(
369  func, "geom_samples", 1, 1, 128, "Geom samples", "Geometry samples per frame", 1, 128);
370  RNA_def_float(func, "shutter_open", 0.0f, -1.0f, 1.0f, "Shutter open", "", -1.0f, 1.0f);
371  RNA_def_float(func, "shutter_close", 1.0f, -1.0f, 1.0f, "Shutter close", "", -1.0f, 1.0f);
372  RNA_def_boolean(func, "selected_only", 0, "Selected only", "Export only selected objects");
373  RNA_def_boolean(func, "uvs", 1, "UVs", "Export UVs");
374  RNA_def_boolean(func, "normals", 1, "Normals", "Export normals");
375  RNA_def_boolean(func, "vcolors", 0, "Vertex colors", "Export vertex colors");
377  func, "apply_subdiv", 1, "Subsurfs as meshes", "Export subdivision surfaces as meshes");
378  RNA_def_boolean(func, "flatten", 0, "Flatten hierarchy", "Flatten hierarchy");
379  RNA_def_boolean(func,
380  "visible_objects_only",
381  0,
382  "Visible layers only",
383  "Export only objects in visible layers");
384  RNA_def_boolean(func, "face_sets", 0, "Facesets", "Export face sets");
385  RNA_def_boolean(func,
386  "subdiv_schema",
387  0,
388  "Use Alembic subdivision Schema",
389  "Use Alembic subdivision Schema");
391  func, "export_hair", 1, "Export Hair", "Exports hair particle systems as animated curves");
393  func, "export_particles", 1, "Export Particles", "Exports non-hair particle systems");
395  func, "packuv", 0, "Export with packed UV islands", "Export with packed UV islands");
397  func,
398  "scale",
399  1.0f,
400  0.0001f,
401  1000.0f,
402  "Scale",
403  "Value by which to enlarge or shrink the objects with respect to the world's origin",
404  0.0001f,
405  1000.0f);
407  func, "triangulate", 0, "Triangulate", "Export polygons (quads and n-gons) as triangles");
408  RNA_def_enum(func,
409  "quad_method",
411  0,
412  "Quad Method",
413  "Method for splitting the quads into triangles");
414  RNA_def_enum(func,
415  "ngon_method",
417  0,
418  "N-gon Method",
419  "Method for splitting the n-gons into triangles");
420 
422 # endif
423 }
424 
426 {
427  FunctionRNA *func;
428  PropertyRNA *parm;
429 
430  func = RNA_def_function(srna, "frame_path", "rna_SceneRender_get_frame_path");
433  func, "Return the absolute path to the filename to be written for a given frame");
434  RNA_def_int(func,
435  "frame",
436  INT_MIN,
437  INT_MIN,
438  INT_MAX,
439  "",
440  "Frame number to use, if unset the current frame will be used",
441  MINAFRAME,
442  MAXFRAME);
443  RNA_def_boolean(func, "preview", 0, "Preview", "Use preview range");
445  "view",
446  NULL,
447  FILE_MAX,
448  "View",
449  "The name of the view to use to replace the \"%\" chars");
450  parm = RNA_def_string_file_path(func,
451  "filepath",
452  NULL,
453  FILE_MAX,
454  "File Path",
455  "The resulting filepath from the scenes render settings");
456  RNA_def_parameter_flags(parm, PROP_THICK_WRAP, 0); /* needed for string return value */
457  RNA_def_function_output(func, parm);
458 }
459 
460 #endif
bool ABC_export(struct Scene *scene, struct bContext *C, const char *filepath, const struct AlembicExportParams *params, bool as_background_job)
typedef float(TangentPoint)[2]
BMEditMesh * BKE_editmesh_from_object(struct Object *ob)
Return the BMEditMesh for a given object.
Definition: editmesh.c:85
void BKE_image_path_from_imformat(char *string, const char *base, const char *relbase, int frame, const struct ImageFormatData *im_format, const bool use_ext, const bool use_frames, const char *suffix)
bool BKE_imtype_is_movie(const char imtype)
Definition: image.c:1443
const char * BKE_main_blendfile_path(const struct Main *bmain) ATTR_NONNULL()
void BKE_scene_frame_set(struct Scene *scene, double cfra)
Definition: scene.c:2475
bool BKE_scene_camera_switch_update(struct Scene *scene)
Definition: scene.c:2349
void BKE_scene_graph_update_for_newframe(struct Depsgraph *depsgraph)
Definition: scene.c:2794
struct Depsgraph * BKE_scene_ensure_depsgraph(struct Main *bmain, struct Scene *scene, struct ViewLayer *view_layer)
Definition: scene.c:3526
const char * BKE_scene_multiview_view_suffix_get(const struct RenderData *rd, const char *viewname)
void BKE_screen_view3d_scene_sync(struct bScreen *screen, struct Scene *scene)
Definition: screen.c:1041
void BKE_movie_filepath_get(char *string, const struct RenderData *rd, bool preview, const char *suffix)
#define BVH_RAYCAST_DIST_MAX
Definition: BLI_kdopbvh.h:105
void unit_m4(float m[4][4])
Definition: rct.c:1140
MINLINE float normalize_v3(float r[3])
MINLINE void zero_v3(float r[3])
#define FILE_MAX
#define UNUSED(x)
#define BPy_BEGIN_ALLOW_THREADS
Definition: BPY_extern.h:62
#define BPy_END_ALLOW_THREADS
Definition: BPY_extern.h:66
typedef double(DMatrix)[4][4]
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:51
struct Object * DEG_get_original_object(struct Object *object)
@ OB_MODE_EDIT
Object is a sort of wrapper for general info.
@ OB_MESH
#define R_EXTENSION
#define MINAFRAME
#define MAXFRAME
bool EDBM_uv_check(struct BMEditMesh *em)
SnapObjectContext * ED_transform_snap_object_context_create(struct Scene *scene, int flag)
void ED_transform_snap_object_context_destroy(SnapObjectContext *sctx)
bool ED_transform_snap_object_project_ray_ex(struct SnapObjectContext *sctx, struct Depsgraph *depsgraph, const struct SnapObjectParams *params, const float ray_start[3], const float ray_normal[3], float *ray_depth, float r_loc[3], float r_no[3], int *r_index, struct Object **r_ob, float r_obmat[4][4])
void ED_uvedit_get_aspect(struct Object *obedit, float *r_aspx, float *r_aspy)
static AppView * view
Group RGB to Bright Vector Camera CLAMP
@ PARM_REQUIRED
Definition: RNA_types.h:337
@ FUNC_USE_MAIN
Definition: RNA_types.h:576
@ FUNC_USE_CONTEXT
Definition: RNA_types.h:577
@ PROP_THICK_WRAP
Definition: RNA_types.h:270
@ PROP_NEVER_NULL
Definition: RNA_types.h:225
@ PROP_FILEPATH
Definition: RNA_types.h:116
#define C
Definition: RandGen.cpp:39
#define NC_WINDOW
Definition: WM_types.h:277
Scene scene
const Depsgraph * depsgraph
static float normals[][3]
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
return ret
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, float default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax)
Definition: rna_define.c:3825
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, bool default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3481
PropertyRNA * RNA_def_pointer(StructOrFunctionRNA *cont_, const char *identifier, const char *type, const char *ui_name, const char *ui_description)
Definition: rna_define.c:4159
void RNA_def_function_return(FunctionRNA *func, PropertyRNA *ret)
Definition: rna_define.c:4302
PropertyRNA * RNA_def_float_matrix(StructOrFunctionRNA *cont_, const char *identifier, int rows, int columns, const float *default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax)
Definition: rna_define.c:3943
PropertyRNA * RNA_def_string_file_path(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, int maxlen, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3699
FunctionRNA * RNA_def_function(StructRNA *srna, const char *identifier, const char *call)
Definition: rna_define.c:4262
void RNA_def_function_output(FunctionRNA *UNUSED(func), PropertyRNA *ret)
Definition: rna_define.c:4327
PropertyRNA * RNA_def_float_vector(StructOrFunctionRNA *cont_, const char *identifier, int len, const float *default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax)
Definition: rna_define.c:3851
void RNA_def_function_ui_description(FunctionRNA *func, const char *description)
Definition: rna_define.c:4337
void RNA_def_function_flag(FunctionRNA *func, int flag)
Definition: rna_define.c:4332
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, int maxlen, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3675
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, int default_value, int hardmin, int hardmax, const char *ui_name, const char *ui_description, int softmin, int softmax)
Definition: rna_define.c:3585
void RNA_def_property_subtype(PropertyRNA *prop, PropertySubType subtype)
Definition: rna_define.c:1563
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, int default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3771
void RNA_def_parameter_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
Definition: rna_define.c:1547
const EnumPropertyItem rna_enum_modifier_triangulate_ngon_method_items[]
Definition: rna_modifier.c:338
const EnumPropertyItem rna_enum_modifier_triangulate_quad_method_items[]
Definition: rna_modifier.c:314
void RNA_api_scene(StructRNA *srna)
void RNA_api_scene_render(StructRNA *srna)
void SEQ_editing_free(Scene *scene, const bool do_id_user)
Definition: sequencer.c:255
void * first
Definition: DNA_listBase.h:47
Definition: BKE_main.h:116
ListBase screens
Definition: BKE_main.h:161
struct ImageFormatData im_format
char pic[1024]
ListBase view_layers
#define G(x, y, z)
void WM_main_add_notifier(unsigned int type, void *reference)