Blender  V3.3
object_dupli.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2001-2002 NaN Holding BV. All rights reserved. */
3 
8 #include <climits>
9 #include <cstddef>
10 #include <cstdlib>
11 
12 #include "MEM_guardedalloc.h"
13 
14 #include "BLI_listbase.h"
15 #include "BLI_string_utf8.h"
16 
17 #include "BLI_array.hh"
18 #include "BLI_float4x4.hh"
19 #include "BLI_math.h"
20 #include "BLI_math_vec_types.hh"
21 #include "BLI_rand.h"
22 #include "BLI_span.hh"
23 #include "BLI_vector.hh"
24 
25 #include "DNA_anim_types.h"
26 #include "DNA_collection_types.h"
27 #include "DNA_mesh_types.h"
28 #include "DNA_meshdata_types.h"
29 #include "DNA_pointcloud_types.h"
30 #include "DNA_scene_types.h"
31 #include "DNA_vfont_types.h"
32 
33 #include "BKE_collection.h"
34 #include "BKE_duplilist.h"
35 #include "BKE_editmesh.h"
36 #include "BKE_editmesh_cache.h"
37 #include "BKE_geometry_set.h"
38 #include "BKE_geometry_set.hh"
39 #include "BKE_global.h"
40 #include "BKE_idprop.h"
41 #include "BKE_lattice.h"
42 #include "BKE_main.h"
43 #include "BKE_mesh.h"
44 #include "BKE_mesh_iterators.h"
45 #include "BKE_mesh_runtime.h"
46 #include "BKE_object.h"
47 #include "BKE_particle.h"
48 #include "BKE_scene.h"
49 #include "BKE_vfont.h"
50 
51 #include "DEG_depsgraph.h"
52 #include "DEG_depsgraph_query.h"
53 
54 #include "BLI_hash.h"
55 #include "BLI_strict_flags.h"
56 
57 using blender::Array;
58 using blender::float3;
59 using blender::float4x4;
60 using blender::Span;
61 using blender::Vector;
62 
63 /* -------------------------------------------------------------------- */
67 struct DupliContext {
73 
79  float space_mat[4][4];
80 
87 
89  int level;
90 
91  const struct DupliGenerator *gen;
92 
94  ListBase *duplilist; /* Legacy doubly-linked list. */
95 };
96 
98  short type; /* Dupli Type, see members of #OB_DUPLI. */
99  void (*make_duplis)(const DupliContext *ctx);
100 };
101 
102 static const DupliGenerator *get_dupli_generator(const DupliContext *ctx);
103 
107 static void init_context(DupliContext *r_ctx,
109  Scene *scene,
110  Object *ob,
111  const float space_mat[4][4],
112  Vector<Object *> &instance_stack)
113 {
114  r_ctx->depsgraph = depsgraph;
115  r_ctx->scene = scene;
116  r_ctx->collection = nullptr;
117 
118  r_ctx->root_object = ob;
119  r_ctx->object = ob;
120  r_ctx->obedit = OBEDIT_FROM_OBACT(ob);
121  r_ctx->instance_stack = &instance_stack;
122  if (space_mat) {
123  copy_m4_m4(r_ctx->space_mat, space_mat);
124  }
125  else {
126  unit_m4(r_ctx->space_mat);
127  }
128  r_ctx->level = 0;
129 
130  r_ctx->gen = get_dupli_generator(r_ctx);
131 
132  r_ctx->duplilist = nullptr;
133 }
134 
138 static bool copy_dupli_context(
139  DupliContext *r_ctx, const DupliContext *ctx, Object *ob, const float mat[4][4], int index)
140 {
141  *r_ctx = *ctx;
142 
143  /* XXX annoying, previously was done by passing an ID* argument,
144  * this at least is more explicit. */
145  if (ctx->gen->type == OB_DUPLICOLLECTION) {
146  r_ctx->collection = ctx->object->instance_collection;
147  }
148 
149  r_ctx->object = ob;
150  r_ctx->instance_stack = ctx->instance_stack;
151  if (mat) {
152  mul_m4_m4m4(r_ctx->space_mat, (float(*)[4])ctx->space_mat, mat);
153  }
154  r_ctx->persistent_id[r_ctx->level] = index;
155  ++r_ctx->level;
156 
157  if (r_ctx->level == MAX_DUPLI_RECUR - 1) {
158  std::cerr << "Warning: Maximum instance recursion level reached.\n";
159  return false;
160  }
161 
162  r_ctx->gen = get_dupli_generator(r_ctx);
163  return true;
164 }
165 
172  Object *ob,
173  const float mat[4][4],
174  int index)
175 {
176  DupliObject *dob;
177  int i;
178 
179  /* Add a #DupliObject instance to the result container. */
180  if (ctx->duplilist) {
181  dob = MEM_cnew<DupliObject>("dupli object");
182  BLI_addtail(ctx->duplilist, dob);
183  }
184  else {
185  return nullptr;
186  }
187 
188  dob->ob = ob;
189  dob->ob_data = (ID *)ob->data;
190  mul_m4_m4m4(dob->mat, (float(*)[4])ctx->space_mat, mat);
191  dob->type = ctx->gen->type;
192 
193  /* Set persistent id, which is an array with a persistent index for each level
194  * (particle number, vertex number, ..). by comparing this we can find the same
195  * dupli-object between frames, which is needed for motion blur.
196  * The last level is ordered first in the array. */
197  dob->persistent_id[0] = index;
198  for (i = 1; i < ctx->level + 1; i++) {
199  dob->persistent_id[i] = ctx->persistent_id[ctx->level - i];
200  }
201  /* Fill rest of values with #INT_MAX which index will never have as value. */
202  for (; i < MAX_DUPLI_RECUR; i++) {
203  dob->persistent_id[i] = INT_MAX;
204  }
205 
206  /* Meta-balls never draw in duplis, they are instead merged into one by the basis
207  * meta-ball outside of the group. this does mean that if that meta-ball is not in the
208  * scene, they will not show up at all, limitation that should be solved once. */
209  if (ob->type == OB_MBALL) {
210  dob->no_draw = true;
211  }
212 
213  /* Random number per instance.
214  * The root object in the scene, persistent ID up to the instance object, and the instance object
215  * name together result in a unique random number. */
216  dob->random_id = BLI_hash_string(dob->ob->id.name + 2);
217 
218  if (dob->persistent_id[0] != INT_MAX) {
219  for (i = 0; i < MAX_DUPLI_RECUR; i++) {
220  dob->random_id = BLI_hash_int_2d(dob->random_id, (unsigned int)dob->persistent_id[i]);
221  }
222  }
223  else {
224  dob->random_id = BLI_hash_int_2d(dob->random_id, 0);
225  }
226 
227  if (ctx->root_object != ob) {
229  }
230 
231  return dob;
232 }
233 
239 static void make_recursive_duplis(const DupliContext *ctx,
240  Object *ob,
241  const float space_mat[4][4],
242  int index)
243 {
244  if (ctx->instance_stack->contains(ob)) {
245  /* Avoid recursive instances. */
246  printf("Warning: '%s' object is trying to instance itself.\n", ob->id.name + 2);
247  return;
248  }
249  /* Simple preventing of too deep nested collections with #MAX_DUPLI_RECUR. */
250  if (ctx->level < MAX_DUPLI_RECUR) {
251  DupliContext rctx;
252  if (!copy_dupli_context(&rctx, ctx, ob, space_mat, index)) {
253  return;
254  }
255  if (rctx.gen) {
256  ctx->instance_stack->append(ob);
257  rctx.gen->make_duplis(&rctx);
258  ctx->instance_stack->remove_last();
259  }
260  }
261 }
262 
265 /* -------------------------------------------------------------------- */
269 using MakeChildDuplisFunc = void (*)(const DupliContext *ctx, void *userdata, Object *child);
270 
271 static bool is_child(const Object *ob, const Object *parent)
272 {
273  const Object *ob_parent = ob->parent;
274  while (ob_parent) {
275  if (ob_parent == parent) {
276  return true;
277  }
278  ob_parent = ob_parent->parent;
279  }
280  return false;
281 }
282 
286 static void make_child_duplis(const DupliContext *ctx,
287  void *userdata,
288  MakeChildDuplisFunc make_child_duplis_cb)
289 {
290  Object *parent = ctx->object;
291 
292  if (ctx->collection) {
295  if ((ob != ctx->obedit) && is_child(ob, parent)) {
296  DupliContext pctx;
297  if (copy_dupli_context(&pctx, ctx, ctx->object, nullptr, _base_id)) {
298  /* Meta-balls have a different dupli handling. */
299  if (ob->type != OB_MBALL) {
300  ob->flag |= OB_DONE; /* Doesn't render. */
301  }
302  make_child_duplis_cb(&pctx, userdata, ob);
303  }
304  }
305  }
307  }
308  else {
309  /* FIXME: using a mere counter to generate a 'persistent' dupli id is very weak. One possible
310  * better solution could be to use `session_uuid` of ID's instead? */
311  int persistent_dupli_id = 0;
312  /* NOTE: this set of flags ensure we only iterate over objects that have a base in either the
313  * current scene, or the set (background) scene. */
314  int deg_objects_visibility_flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY |
316 
317  DEG_OBJECT_ITER_BEGIN (ctx->depsgraph, ob, deg_objects_visibility_flags) {
318  if ((ob != ctx->obedit) && is_child(ob, parent)) {
319  DupliContext pctx;
320  if (copy_dupli_context(&pctx, ctx, ctx->object, nullptr, persistent_dupli_id)) {
321  /* Meta-balls have a different dupli-handling. */
322  if (ob->type != OB_MBALL) {
323  ob->flag |= OB_DONE; /* Doesn't render. */
324  }
325 
326  make_child_duplis_cb(&pctx, userdata, ob);
327  }
328  }
329  persistent_dupli_id++;
330  }
332  }
333 }
334 
337 /* -------------------------------------------------------------------- */
342  BMEditMesh **r_em,
343  const float (**r_vert_coords)[3],
344  const float (**r_vert_normals)[3])
345 {
346  /* Gather mesh info. */
348  const Mesh *me_eval;
349 
350  *r_em = nullptr;
351  *r_vert_coords = nullptr;
352  if (r_vert_normals != nullptr) {
353  *r_vert_normals = nullptr;
354  }
355 
356  /* We do not need any render-specific handling anymore, depsgraph takes care of that. */
357  /* NOTE: Do direct access to the evaluated mesh: this function is used
358  * during meta balls evaluation. But even without those all the objects
359  * which are needed for correct instancing are already evaluated. */
360  if (em != nullptr) {
361  /* Note that this will only show deformation if #eModifierMode_OnCage is enabled.
362  * We could change this but it matches 2.7x behavior. */
363  me_eval = BKE_object_get_editmesh_eval_cage(ob);
364  if ((me_eval == nullptr) || (me_eval->runtime.wrapper_type == ME_WRAPPER_TYPE_BMESH)) {
365  EditMeshData *emd = me_eval ? me_eval->runtime.edit_data : nullptr;
366 
367  /* Only assign edit-mesh in the case we can't use `me_eval`. */
368  *r_em = em;
369  me_eval = nullptr;
370 
371  if ((emd != nullptr) && (emd->vertexCos != nullptr)) {
372  *r_vert_coords = emd->vertexCos;
373  if (r_vert_normals != nullptr) {
375  *r_vert_normals = emd->vertexNos;
376  }
377  }
378  }
379  }
380  else {
381  me_eval = BKE_object_get_evaluated_mesh(ob);
382  }
383  return me_eval;
384 }
385 
388 /* -------------------------------------------------------------------- */
392 static void make_duplis_collection(const DupliContext *ctx)
393 {
394  Object *ob = ctx->object;
395  Collection *collection;
396  float collection_mat[4][4];
397 
398  if (ob->instance_collection == nullptr) {
399  return;
400  }
401  collection = ob->instance_collection;
402 
403  /* Combine collection offset and `obmat`. */
404  unit_m4(collection_mat);
405  sub_v3_v3(collection_mat[3], collection->instance_offset);
406  mul_m4_m4m4(collection_mat, ob->obmat, collection_mat);
407  /* Don't access 'ob->obmat' from now on. */
408 
410  FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_BEGIN (collection, cob, mode) {
411  if (cob != ob) {
412  float mat[4][4];
413 
414  /* Collection dupli-offset, should apply after everything else. */
415  mul_m4_m4m4(mat, collection_mat, cob->obmat);
416 
417  make_dupli(ctx, cob, mat, _base_id);
418 
419  /* Recursion. */
420  make_recursive_duplis(ctx, cob, collection_mat, _base_id);
421  }
422  }
424 }
425 
427  OB_DUPLICOLLECTION, /* type */
428  make_duplis_collection /* make_duplis */
429 };
430 
433 /* -------------------------------------------------------------------- */
444 
446 };
447 
450 
451  int totvert;
452  const MVert *mvert;
453  const float (*vert_normals)[3];
454 
455  const float (*orco)[3];
456 };
457 
460 
462 
463  /* Can be nullptr. */
464  const float (*vert_coords)[3];
465  const float (*vert_normals)[3];
466 
474  bool has_orco;
475 };
476 
482 static void get_duplivert_transform(const float co[3],
483  const float no[3],
484  const bool use_rotation,
485  const short axis,
486  const short upflag,
487  float r_mat[4][4])
488 {
489  float quat[4];
490  const float size[3] = {1.0f, 1.0f, 1.0f};
491 
492  if (use_rotation) {
493  /* Construct rotation matrix from normals. */
494  float no_flip[3];
495  negate_v3_v3(no_flip, no);
496  vec_to_quat(quat, no_flip, axis, upflag);
497  }
498  else {
499  unit_qt(quat);
500  }
501 
502  loc_quat_size_to_mat4(r_mat, co, quat, size);
503 }
504 
506  Object *inst_ob,
507  const float child_imat[4][4],
508  int index,
509  const float co[3],
510  const float no[3],
511  const bool use_rotation)
512 {
513  /* `obmat` is transform to vertex. */
514  float obmat[4][4];
515  get_duplivert_transform(co, no, use_rotation, inst_ob->trackflag, inst_ob->upflag, obmat);
516 
517  float space_mat[4][4];
518 
519  /* Make offset relative to inst_ob using relative child transform. */
520  mul_mat3_m4_v3(child_imat, obmat[3]);
521  /* Apply `obmat` _after_ the local vertex transform. */
522  mul_m4_m4m4(obmat, inst_ob->obmat, obmat);
523 
524  /* Space matrix is constructed by removing `obmat` transform,
525  * this yields the world-space transform for recursive duplis. */
526  mul_m4_m4m4(space_mat, obmat, inst_ob->imat);
527 
528  DupliObject *dob = make_dupli(ctx, inst_ob, obmat, index);
529 
530  /* Recursion. */
531  make_recursive_duplis(ctx, inst_ob, space_mat, index);
532 
533  return dob;
534 }
535 
537  void *userdata,
538  Object *inst_ob)
539 {
540  VertexDupliData_Mesh *vdd = (VertexDupliData_Mesh *)userdata;
541  const bool use_rotation = vdd->params.use_rotation;
542 
543  const MVert *mvert = vdd->mvert;
544  const int totvert = vdd->totvert;
545 
546  invert_m4_m4(inst_ob->imat, inst_ob->obmat);
547  /* Relative transform from parent to child space. */
548  float child_imat[4][4];
549  mul_m4_m4m4(child_imat, inst_ob->imat, ctx->object->obmat);
550 
551  for (int i = 0; i < totvert; i++) {
552  DupliObject *dob = vertex_dupli(
553  vdd->params.ctx, inst_ob, child_imat, i, mvert[i].co, vdd->vert_normals[i], use_rotation);
554  if (vdd->orco) {
555  copy_v3_v3(dob->orco, vdd->orco[i]);
556  }
557  }
558 }
559 
561  void *userdata,
562  Object *inst_ob)
563 {
565  BMEditMesh *em = vdd->em;
566  const bool use_rotation = vdd->params.use_rotation;
567 
568  invert_m4_m4(inst_ob->imat, inst_ob->obmat);
569  /* Relative transform from parent to child space. */
570  float child_imat[4][4];
571  mul_m4_m4m4(child_imat, inst_ob->imat, ctx->object->obmat);
572 
573  BMVert *v;
574  BMIter iter;
575  int i;
576 
577  const float(*vert_coords)[3] = vdd->vert_coords;
578  const float(*vert_normals)[3] = vdd->vert_normals;
579 
580  BM_ITER_MESH_INDEX (v, &iter, em->bm, BM_VERTS_OF_MESH, i) {
581  const float *co, *no;
582  if (vert_coords != nullptr) {
583  co = vert_coords[i];
584  no = vert_normals ? vert_normals[i] : nullptr;
585  }
586  else {
587  co = v->co;
588  no = v->no;
589  }
590 
591  DupliObject *dob = vertex_dupli(vdd->params.ctx, inst_ob, child_imat, i, co, no, use_rotation);
592  if (vdd->has_orco) {
593  copy_v3_v3(dob->orco, v->co);
594  }
595  }
596 }
597 
598 static void make_duplis_verts(const DupliContext *ctx)
599 {
600  Object *parent = ctx->object;
601  const bool use_rotation = parent->transflag & OB_DUPLIROT;
602 
603  /* Gather mesh info. */
604  BMEditMesh *em = nullptr;
605  const float(*vert_coords)[3] = nullptr;
606  const float(*vert_normals)[3] = nullptr;
607  const Mesh *me_eval = mesh_data_from_duplicator_object(
608  parent, &em, &vert_coords, use_rotation ? &vert_normals : nullptr);
609  if (em == nullptr && me_eval == nullptr) {
610  return;
611  }
612 
613  VertexDupliData_Params vdd_params{ctx, use_rotation};
614 
615  if (em != nullptr) {
617  vdd.params = vdd_params;
618  vdd.em = em;
619  vdd.vert_coords = vert_coords;
620  vdd.vert_normals = vert_normals;
621  vdd.has_orco = (vert_coords != nullptr);
622 
624  }
625  else {
626  VertexDupliData_Mesh vdd{};
627  vdd.params = vdd_params;
628  vdd.totvert = me_eval->totvert;
629  vdd.mvert = me_eval->mvert;
630  vdd.vert_normals = BKE_mesh_vertex_normals_ensure(me_eval);
631  vdd.orco = (const float(*)[3])CustomData_get_layer(&me_eval->vdata, CD_ORCO);
632 
634  }
635 }
636 
638  OB_DUPLIVERTS, /* type */
639  make_duplis_verts /* make_duplis */
640 };
641 
644 /* -------------------------------------------------------------------- */
649  Main *bmain, const char *family, size_t family_len, unsigned int ch, GHash *family_gh)
650 {
651  void *ch_key = POINTER_FROM_UINT(ch);
652 
653  Object **ob_pt;
654  if ((ob_pt = (Object **)BLI_ghash_lookup_p(family_gh, ch_key))) {
655  return *ob_pt;
656  }
657 
658  char ch_utf8[BLI_UTF8_MAX + 1];
659  size_t ch_utf8_len;
660 
661  ch_utf8_len = BLI_str_utf8_from_unicode(ch, ch_utf8, sizeof(ch_utf8) - 1);
662  ch_utf8[ch_utf8_len] = '\0';
663  ch_utf8_len += 1; /* Compare with null terminator. */
664 
665  LISTBASE_FOREACH (Object *, ob, &bmain->objects) {
666  if (STREQLEN(ob->id.name + 2 + family_len, ch_utf8, ch_utf8_len)) {
667  if (STREQLEN(ob->id.name + 2, family, family_len)) {
668  /* Inserted value can be nullptr, just to save searches in future. */
669  BLI_ghash_insert(family_gh, ch_key, ob);
670  return ob;
671  }
672  }
673  }
674 
675  return nullptr;
676 }
677 
678 static void make_duplis_font(const DupliContext *ctx)
679 {
680  Object *par = ctx->object;
681  GHash *family_gh;
682  Object *ob;
683  Curve *cu;
684  struct CharTrans *ct, *chartransdata = nullptr;
685  float vec[3], obmat[4][4], pmat[4][4], fsize, xof, yof;
686  int text_len, a;
687  size_t family_len;
688  const char32_t *text = nullptr;
689  bool text_free = false;
690 
691  /* Font dupli-verts not supported inside collections. */
692  if (ctx->collection) {
693  return;
694  }
695 
696  copy_m4_m4(pmat, par->obmat);
697 
698  /* In `par` the family name is stored, use this to find the other objects. */
699 
701  par, (Curve *)par->data, FO_DUPLI, nullptr, &text, &text_len, &text_free, &chartransdata);
702 
703  if (text == nullptr || chartransdata == nullptr) {
704  return;
705  }
706 
707  cu = (Curve *)par->data;
708  fsize = cu->fsize;
709  xof = cu->xof;
710  yof = cu->yof;
711 
712  ct = chartransdata;
713 
714  /* Cache result. */
715  family_len = strlen(cu->family);
716  family_gh = BLI_ghash_int_new_ex(__func__, 256);
717 
718  /* Safety check even if it might fail badly when called for original object. */
719  const bool is_eval_curve = DEG_is_evaluated_id(&cu->id);
720 
721  /* Advance matching BLI_str_utf8_as_utf32. */
722  for (a = 0; a < text_len; a++, ct++) {
723 
724  /* XXX That G.main is *really* ugly, but not sure what to do here.
725  * Definitively don't think it would be safe to put back `Main *bmain` pointer
726  * in #DupliContext as done in 2.7x? */
727  ob = find_family_object(G.main, cu->family, family_len, (unsigned int)text[a], family_gh);
728 
729  if (is_eval_curve) {
730  /* Workaround for the above hack. */
731  ob = DEG_get_evaluated_object(ctx->depsgraph, ob);
732  }
733 
734  if (ob) {
735  vec[0] = fsize * (ct->xof - xof);
736  vec[1] = fsize * (ct->yof - yof);
737  vec[2] = 0.0;
738 
739  mul_m4_v3(pmat, vec);
740 
741  copy_m4_m4(obmat, par->obmat);
742 
743  if (UNLIKELY(ct->rot != 0.0f)) {
744  float rmat[4][4];
745 
746  zero_v3(obmat[3]);
747  axis_angle_to_mat4_single(rmat, 'Z', -ct->rot);
748  mul_m4_m4m4(obmat, obmat, rmat);
749  }
750 
751  copy_v3_v3(obmat[3], vec);
752 
753  make_dupli(ctx, ob, obmat, a);
754  }
755  }
756 
757  if (text_free) {
758  MEM_freeN((void *)text);
759  }
760 
761  BLI_ghash_free(family_gh, nullptr, nullptr);
762 
763  MEM_freeN(chartransdata);
764 }
765 
767  OB_DUPLIVERTS, /* type */
768  make_duplis_font /* make_duplis */
769 };
770 
773 /* -------------------------------------------------------------------- */
778  const GeometrySet &geometry_set,
779  const float parent_transform[4][4],
780  bool geometry_set_is_instance)
781 {
782  int component_index = 0;
783  if (ctx->object->type != OB_MESH || geometry_set_is_instance) {
784  if (const Mesh *mesh = geometry_set.get_mesh_for_read()) {
785  DupliObject *dupli = make_dupli(ctx, ctx->object, parent_transform, component_index++);
786  dupli->ob_data = (ID *)mesh;
787  }
788  }
789  if (ctx->object->type != OB_VOLUME || geometry_set_is_instance) {
790  if (const Volume *volume = geometry_set.get_volume_for_read()) {
791  DupliObject *dupli = make_dupli(ctx, ctx->object, parent_transform, component_index++);
792  dupli->ob_data = (ID *)volume;
793  }
794  }
795  if (!ELEM(ctx->object->type, OB_CURVES_LEGACY, OB_FONT, OB_CURVES) || geometry_set_is_instance) {
796  if (const CurveComponent *component = geometry_set.get_component_for_read<CurveComponent>()) {
797  if (const Curve *curve = component->get_curve_for_render()) {
798  DupliObject *dupli = make_dupli(ctx, ctx->object, parent_transform, component_index++);
799  dupli->ob_data = (ID *)curve;
800  }
801  }
802  }
803  if (ctx->object->type != OB_POINTCLOUD || geometry_set_is_instance) {
804  if (const PointCloud *pointcloud = geometry_set.get_pointcloud_for_read()) {
805  DupliObject *dupli = make_dupli(ctx, ctx->object, parent_transform, component_index++);
806  dupli->ob_data = (ID *)pointcloud;
807  }
808  }
809  const bool creates_duplis_for_components = component_index >= 1;
810 
812  if (component == nullptr) {
813  return;
814  }
815 
816  const DupliContext *instances_ctx = ctx;
817  /* Create a sub-context if some duplis were created above. This is to avoid dupli id collisions
818  * between the instances component below and the other components above. */
819  DupliContext new_instances_ctx;
820  if (creates_duplis_for_components) {
821  if (!copy_dupli_context(&new_instances_ctx, ctx, ctx->object, nullptr, component_index)) {
822  return;
823  }
824  instances_ctx = &new_instances_ctx;
825  }
826 
827  Span<float4x4> instance_offset_matrices = component->instance_transforms();
828  Span<int> instance_reference_handles = component->instance_reference_handles();
829  Span<int> almost_unique_ids = component->almost_unique_ids();
830  Span<InstanceReference> references = component->references();
831 
832  for (int64_t i : instance_offset_matrices.index_range()) {
833  const InstanceReference &reference = references[instance_reference_handles[i]];
834  const int id = almost_unique_ids[i];
835 
836  switch (reference.type()) {
838  Object &object = reference.object();
839  float matrix[4][4];
840  mul_m4_m4m4(matrix, parent_transform, instance_offset_matrices[i].values);
841  make_dupli(instances_ctx, &object, matrix, id);
842 
843  float space_matrix[4][4];
844  mul_m4_m4m4(space_matrix, instance_offset_matrices[i].values, object.imat);
845  mul_m4_m4_pre(space_matrix, parent_transform);
846  make_recursive_duplis(instances_ctx, &object, space_matrix, id);
847  break;
848  }
850  Collection &collection = reference.collection();
851  float collection_matrix[4][4];
852  unit_m4(collection_matrix);
853  sub_v3_v3(collection_matrix[3], collection.instance_offset);
854  mul_m4_m4_pre(collection_matrix, instance_offset_matrices[i].values);
855  mul_m4_m4_pre(collection_matrix, parent_transform);
856 
857  DupliContext sub_ctx;
858  if (!copy_dupli_context(&sub_ctx, instances_ctx, instances_ctx->object, nullptr, id)) {
859  break;
860  }
861 
862  eEvaluationMode mode = DEG_get_mode(instances_ctx->depsgraph);
863  int object_id = 0;
864  FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_BEGIN (&collection, object, mode) {
865  if (object == instances_ctx->object) {
866  continue;
867  }
868 
869  float instance_matrix[4][4];
870  mul_m4_m4m4(instance_matrix, collection_matrix, object->obmat);
871 
872  make_dupli(&sub_ctx, object, instance_matrix, object_id++);
873  make_recursive_duplis(&sub_ctx, object, collection_matrix, object_id++);
874  }
876  break;
877  }
879  float new_transform[4][4];
880  mul_m4_m4m4(new_transform, parent_transform, instance_offset_matrices[i].values);
881 
882  DupliContext sub_ctx;
883  if (copy_dupli_context(&sub_ctx, instances_ctx, instances_ctx->object, nullptr, id)) {
884  make_duplis_geometry_set_impl(&sub_ctx, reference.geometry_set(), new_transform, true);
885  }
886  break;
887  }
889  break;
890  }
891  }
892  }
893 }
894 
895 static void make_duplis_geometry_set(const DupliContext *ctx)
896 {
897  const GeometrySet *geometry_set = ctx->object->runtime.geometry_set_eval;
898  make_duplis_geometry_set_impl(ctx, *geometry_set, ctx->object->obmat, false);
899 }
900 
902  0,
904 };
905 
908 /* -------------------------------------------------------------------- */
919 
920  bool use_scale;
921 };
922 
925 
926  int totface;
927  const MPoly *mpoly;
928  const MLoop *mloop;
929  const MVert *mvert;
930  const float (*orco)[3];
931  const MLoopUV *mloopuv;
932 };
933 
936 
938 
941  /* Can be nullptr. */
942  const float (*vert_coords)[3];
943 };
944 
946  const bool use_scale,
947  const float scale_fac,
948  float r_mat[4][4])
949 {
950  using namespace blender::math;
951 
952  /* Location. */
953  float3 location(0);
954  for (const float3 &coord : coords) {
955  location += coord;
956  }
957  location *= 1.0f / (float)coords.size();
958 
959  /* Rotation. */
960  float quat[4];
961 
962  float3 f_no = normalize(cross_poly(coords));
963  tri_to_quat_ex(quat, coords[0], coords[1], coords[2], f_no);
964 
965  /* Scale. */
966  float scale;
967  if (use_scale) {
968  const float area = area_poly_v3((const float(*)[3])coords.data(), (uint)coords.size());
969  scale = sqrtf(area) * scale_fac;
970  }
971  else {
972  scale = 1.0f;
973  }
974 
975  loc_quat_size_to_mat4(r_mat, location, quat, float3(scale));
976 }
977 
979  Object *inst_ob,
980  const float child_imat[4][4],
981  const int index,
982  const bool use_scale,
983  const float scale_fac,
984  Span<float3> coords)
985 {
986  float obmat[4][4];
987  float space_mat[4][4];
988 
989  /* `obmat` is transform to face. */
990  get_dupliface_transform_from_coords(coords, use_scale, scale_fac, obmat);
991 
992  /* Make offset relative to inst_ob using relative child transform. */
993  mul_mat3_m4_v3(child_imat, obmat[3]);
994 
995  /* XXX ugly hack to ensure same behavior as in master.
996  * This should not be needed, #Object.parentinv is not consistent outside of parenting. */
997  {
998  float imat[3][3];
999  copy_m3_m4(imat, inst_ob->parentinv);
1000  mul_m4_m3m4(obmat, imat, obmat);
1001  }
1002 
1003  /* Apply `obmat` _after_ the local face transform. */
1004  mul_m4_m4m4(obmat, inst_ob->obmat, obmat);
1005 
1006  /* Space matrix is constructed by removing `obmat` transform,
1007  * this yields the world-space transform for recursive duplis. */
1008  mul_m4_m4m4(space_mat, obmat, inst_ob->imat);
1009 
1010  DupliObject *dob = make_dupli(ctx, inst_ob, obmat, index);
1011 
1012  /* Recursion. */
1013  make_recursive_duplis(ctx, inst_ob, space_mat, index);
1014 
1015  return dob;
1016 }
1017 
1019  Object *inst_ob,
1020  const float child_imat[4][4],
1021  const int index,
1022  const bool use_scale,
1023  const float scale_fac,
1024 
1025  /* Mesh variables. */
1026  const MPoly *mpoly,
1027  const MLoop *mloopstart,
1028  const MVert *mvert)
1029 {
1030  const int coords_len = mpoly->totloop;
1031  Array<float3, 64> coords(coords_len);
1032 
1033  const MLoop *ml = mloopstart;
1034  for (int i = 0; i < coords_len; i++, ml++) {
1035  coords[i] = float3(mvert[ml->v].co);
1036  }
1037 
1038  return face_dupli(ctx, inst_ob, child_imat, index, use_scale, scale_fac, coords);
1039 }
1040 
1042  Object *inst_ob,
1043  const float child_imat[4][4],
1044  const int index,
1045  const bool use_scale,
1046  const float scale_fac,
1047 
1048  /* Mesh variables. */
1049  BMFace *f,
1050  const float (*vert_coords)[3])
1051 {
1052  const int coords_len = f->len;
1053  Array<float3, 64> coords(coords_len);
1054 
1055  BMLoop *l_first, *l_iter;
1056  int i = 0;
1057  l_iter = l_first = BM_FACE_FIRST_LOOP(f);
1058  if (vert_coords != nullptr) {
1059  do {
1060  copy_v3_v3(coords[i++], vert_coords[BM_elem_index_get(l_iter->v)]);
1061  } while ((l_iter = l_iter->next) != l_first);
1062  }
1063  else {
1064  do {
1065  copy_v3_v3(coords[i++], l_iter->v->co);
1066  } while ((l_iter = l_iter->next) != l_first);
1067  }
1068 
1069  return face_dupli(ctx, inst_ob, child_imat, index, use_scale, scale_fac, coords);
1070 }
1071 
1073  void *userdata,
1074  Object *inst_ob)
1075 {
1076  FaceDupliData_Mesh *fdd = (FaceDupliData_Mesh *)userdata;
1077  const MPoly *mpoly = fdd->mpoly, *mp;
1078  const MLoop *mloop = fdd->mloop;
1079  const MVert *mvert = fdd->mvert;
1080  const float(*orco)[3] = fdd->orco;
1081  const MLoopUV *mloopuv = fdd->mloopuv;
1082  const int totface = fdd->totface;
1083  const bool use_scale = fdd->params.use_scale;
1084  int a;
1085 
1086  float child_imat[4][4];
1087 
1088  invert_m4_m4(inst_ob->imat, inst_ob->obmat);
1089  /* Relative transform from parent to child space. */
1090  mul_m4_m4m4(child_imat, inst_ob->imat, ctx->object->obmat);
1091  const float scale_fac = ctx->object->instance_faces_scale;
1092 
1093  for (a = 0, mp = mpoly; a < totface; a++, mp++) {
1094  const MLoop *loopstart = mloop + mp->loopstart;
1096  fdd->params.ctx, inst_ob, child_imat, a, use_scale, scale_fac, mp, loopstart, mvert);
1097 
1098  const float w = 1.0f / (float)mp->totloop;
1099  if (orco) {
1100  for (int j = 0; j < mp->totloop; j++) {
1101  madd_v3_v3fl(dob->orco, orco[loopstart[j].v], w);
1102  }
1103  }
1104  if (mloopuv) {
1105  for (int j = 0; j < mp->totloop; j++) {
1106  madd_v2_v2fl(dob->uv, mloopuv[mp->loopstart + j].uv, w);
1107  }
1108  }
1109  }
1110 }
1111 
1113  void *userdata,
1114  Object *inst_ob)
1115 {
1117  BMEditMesh *em = fdd->em;
1118  float child_imat[4][4];
1119  int a;
1120  BMFace *f;
1121  BMIter iter;
1122  const bool use_scale = fdd->params.use_scale;
1123 
1124  const float(*vert_coords)[3] = fdd->vert_coords;
1125 
1126  BLI_assert((vert_coords == nullptr) || (em->bm->elem_index_dirty & BM_VERT) == 0);
1127 
1128  invert_m4_m4(inst_ob->imat, inst_ob->obmat);
1129  /* Relative transform from parent to child space. */
1130  mul_m4_m4m4(child_imat, inst_ob->imat, ctx->object->obmat);
1131  const float scale_fac = ctx->object->instance_faces_scale;
1132 
1133  BM_ITER_MESH_INDEX (f, &iter, em->bm, BM_FACES_OF_MESH, a) {
1135  fdd->params.ctx, inst_ob, child_imat, a, use_scale, scale_fac, f, vert_coords);
1136 
1137  if (fdd->has_orco) {
1138  const float w = 1.0f / (float)f->len;
1139  BMLoop *l_first, *l_iter;
1140  l_iter = l_first = BM_FACE_FIRST_LOOP(f);
1141  do {
1142  madd_v3_v3fl(dob->orco, l_iter->v->co, w);
1143  } while ((l_iter = l_iter->next) != l_first);
1144  }
1145  if (fdd->has_uvs) {
1147  }
1148  }
1149 }
1150 
1151 static void make_duplis_faces(const DupliContext *ctx)
1152 {
1153  Object *parent = ctx->object;
1154 
1155  /* Gather mesh info. */
1156  BMEditMesh *em = nullptr;
1157  const float(*vert_coords)[3] = nullptr;
1158  const Mesh *me_eval = mesh_data_from_duplicator_object(parent, &em, &vert_coords, nullptr);
1159  if (em == nullptr && me_eval == nullptr) {
1160  return;
1161  }
1162 
1163  FaceDupliData_Params fdd_params = {ctx, (parent->transflag & OB_DUPLIFACES_SCALE) != 0};
1164 
1165  if (em != nullptr) {
1166  const int uv_idx = CustomData_get_render_layer(&em->bm->ldata, CD_MLOOPUV);
1167  FaceDupliData_EditMesh fdd{};
1168  fdd.params = fdd_params;
1169  fdd.em = em;
1170  fdd.vert_coords = vert_coords;
1171  fdd.has_orco = (vert_coords != nullptr);
1172  fdd.has_uvs = (uv_idx != -1);
1173  fdd.cd_loop_uv_offset = (uv_idx != -1) ?
1174  CustomData_get_n_offset(&em->bm->ldata, CD_MLOOPUV, uv_idx) :
1175  -1;
1177  }
1178  else {
1179  const int uv_idx = CustomData_get_render_layer(&me_eval->ldata, CD_MLOOPUV);
1180  FaceDupliData_Mesh fdd{};
1181  fdd.params = fdd_params;
1182  fdd.totface = me_eval->totpoly;
1183  fdd.mpoly = me_eval->mpoly;
1184  fdd.mloop = me_eval->mloop;
1185  fdd.mvert = me_eval->mvert;
1186  fdd.mloopuv = (uv_idx != -1) ? (const MLoopUV *)CustomData_get_layer_n(
1187  &me_eval->ldata, CD_MLOOPUV, uv_idx) :
1188  nullptr;
1189  fdd.orco = (const float(*)[3])CustomData_get_layer(&me_eval->vdata, CD_ORCO);
1190 
1192  }
1193 }
1194 
1196  OB_DUPLIFACES, /* type */
1197  make_duplis_faces /* make_duplis */
1198 };
1199 
1202 /* -------------------------------------------------------------------- */
1207 {
1208  Scene *scene = ctx->scene;
1209  Object *par = ctx->object;
1210  eEvaluationMode mode = DEG_get_mode(ctx->depsgraph);
1211  bool for_render = mode == DAG_EVAL_RENDER;
1212 
1213  Object *ob = nullptr, **oblist = nullptr;
1214  DupliObject *dob;
1215  ParticleSettings *part;
1216  ParticleData *pa;
1217  ChildParticle *cpa = nullptr;
1219  ParticleCacheKey *cache;
1220  float ctime, scale = 1.0f;
1221  float tmat[4][4], mat[4][4], pamat[4][4], size = 0.0;
1222  int a, b, hair = 0;
1223  int totpart, totchild;
1224 
1225  int no_draw_flag = PARS_UNEXIST;
1226 
1227  if (psys == nullptr) {
1228  return;
1229  }
1230 
1231  part = psys->part;
1232 
1233  if (part == nullptr) {
1234  return;
1235  }
1236 
1237  if (!psys_check_enabled(par, psys, for_render)) {
1238  return;
1239  }
1240 
1241  if (!for_render) {
1242  no_draw_flag |= PARS_NO_DISP;
1243  }
1244 
1245  /* NOTE: in old animation system, used parent object's time-offset. */
1246  ctime = DEG_get_ctime(ctx->depsgraph);
1247 
1248  totpart = psys->totpart;
1249  totchild = psys->totchild;
1250 
1251  if ((for_render || part->draw_as == PART_DRAW_REND) &&
1252  ELEM(part->ren_as, PART_DRAW_OB, PART_DRAW_GR)) {
1253  ParticleSimulationData sim = {nullptr};
1254  sim.depsgraph = ctx->depsgraph;
1255  sim.scene = scene;
1256  sim.ob = par;
1257  sim.psys = psys;
1258  sim.psmd = psys_get_modifier(par, psys);
1259  /* Make sure emitter `imat` is in global coordinates instead of render view coordinates. */
1260  invert_m4_m4(par->imat, par->obmat);
1261 
1262  /* First check for loops (particle system object used as dupli-object). */
1263  if (part->ren_as == PART_DRAW_OB) {
1264  if (ELEM(part->instance_object, nullptr, par)) {
1265  return;
1266  }
1267  }
1268  else { /* #PART_DRAW_GR. */
1269  if (part->instance_collection == nullptr) {
1270  return;
1271  }
1272 
1273  const ListBase dup_collection_objects = BKE_collection_object_cache_get(
1274  part->instance_collection);
1275  if (BLI_listbase_is_empty(&dup_collection_objects)) {
1276  return;
1277  }
1278 
1279  if (BLI_findptr(&dup_collection_objects, par, offsetof(Base, object))) {
1280  return;
1281  }
1282  }
1283 
1284  /* If we have a hair particle system, use the path cache. */
1285  if (part->type == PART_HAIR) {
1286  if (psys->flag & PSYS_HAIR_DONE) {
1287  hair = (totchild == 0 || psys->childcache) && psys->pathcache;
1288  }
1289  if (!hair) {
1290  return;
1291  }
1292 
1293  /* We use cache, update `totchild` according to cached data. */
1294  totchild = psys->totchildcache;
1295  totpart = psys->totcached;
1296  }
1297 
1298  RNG *rng = BLI_rng_new_srandom(31415926u + (unsigned int)psys->seed);
1299 
1300  psys_sim_data_init(&sim);
1301 
1302  /* Gather list of objects or single object. */
1303  int totcollection = 0;
1304 
1305  const bool use_whole_collection = part->draw & PART_DRAW_WHOLE_GR;
1306  const bool use_collection_count = part->draw & PART_DRAW_COUNT_GR && !use_whole_collection;
1307  if (part->ren_as == PART_DRAW_GR) {
1308  if (use_collection_count) {
1312  part->instance_collection, object, mode) {
1313  if (dw->ob == object) {
1314  totcollection += dw->count;
1315  break;
1316  }
1317  }
1319  }
1320  }
1321  else {
1323  part->instance_collection, object, mode) {
1324  (void)object;
1325  totcollection++;
1326  }
1328  }
1329 
1330  oblist = (Object **)MEM_callocN((size_t)totcollection * sizeof(Object *),
1331  "dupcollection object list");
1332 
1333  if (use_collection_count) {
1334  a = 0;
1337  part->instance_collection, object, mode) {
1338  if (dw->ob == object) {
1339  for (b = 0; b < dw->count; b++, a++) {
1340  oblist[a] = dw->ob;
1341  }
1342  break;
1343  }
1344  }
1346  }
1347  }
1348  else {
1349  a = 0;
1351  part->instance_collection, object, mode) {
1352  oblist[a] = object;
1353  a++;
1354  }
1356  }
1357  }
1358  else {
1359  ob = part->instance_object;
1360  }
1361 
1362  if (totchild == 0 || part->draw & PART_DRAW_PARENT) {
1363  a = 0;
1364  }
1365  else {
1366  a = totpart;
1367  }
1368 
1369  for (pa = psys->particles; a < totpart + totchild; a++, pa++) {
1370  if (a < totpart) {
1371  /* Handle parent particle. */
1372  if (pa->flag & no_draw_flag) {
1373  continue;
1374  }
1375 
1376 #if 0 /* UNUSED */
1377  pa_num = pa->num;
1378 #endif
1379  size = pa->size;
1380  }
1381  else {
1382  /* Handle child particle. */
1383  cpa = &psys->child[a - totpart];
1384 
1385 #if 0 /* UNUSED */
1386  pa_num = a;
1387 #endif
1388  size = psys_get_child_size(psys, cpa, ctime, nullptr);
1389  }
1390 
1391  /* Some hair paths might be non-existent so they can't be used for duplication. */
1392  if (hair && psys->pathcache &&
1393  ((a < totpart && psys->pathcache[a]->segments < 0) ||
1394  (a >= totpart && psys->childcache[a - totpart]->segments < 0))) {
1395  continue;
1396  }
1397 
1398  if (part->ren_as == PART_DRAW_GR) {
1399  /* Prevent divide by zero below T28336. */
1400  if (totcollection == 0) {
1401  continue;
1402  }
1403 
1404  /* For collections, pick the object based on settings. */
1405  if (part->draw & PART_DRAW_RAND_GR && !use_whole_collection) {
1406  b = BLI_rng_get_int(rng) % totcollection;
1407  }
1408  else {
1409  b = a % totcollection;
1410  }
1411 
1412  ob = oblist[b];
1413  }
1414 
1415  if (hair) {
1416  /* Hair we handle separate and compute transform based on hair keys. */
1417  if (a < totpart) {
1418  cache = psys->pathcache[a];
1419  psys_get_dupli_path_transform(&sim, pa, nullptr, cache, pamat, &scale);
1420  }
1421  else {
1422  cache = psys->childcache[a - totpart];
1423  psys_get_dupli_path_transform(&sim, nullptr, cpa, cache, pamat, &scale);
1424  }
1425 
1426  copy_v3_v3(pamat[3], cache->co);
1427  pamat[3][3] = 1.0f;
1428  }
1429  else {
1430  /* First key. */
1431  state.time = ctime;
1432  if (psys_get_particle_state(&sim, a, &state, false) == 0) {
1433  continue;
1434  }
1435 
1436  float tquat[4];
1437  normalize_qt_qt(tquat, state.rot);
1438  quat_to_mat4(pamat, tquat);
1439  copy_v3_v3(pamat[3], state.co);
1440  pamat[3][3] = 1.0f;
1441  }
1442 
1443  if (part->ren_as == PART_DRAW_GR && psys->part->draw & PART_DRAW_WHOLE_GR) {
1444  b = 0;
1446  part->instance_collection, object, mode) {
1447  copy_m4_m4(tmat, oblist[b]->obmat);
1448 
1449  /* Apply collection instance offset. */
1451 
1452  /* Apply particle scale. */
1453  mul_mat3_m4_fl(tmat, size * scale);
1454  mul_v3_fl(tmat[3], size * scale);
1455 
1456  /* Individual particle transform. */
1457  mul_m4_m4m4(mat, pamat, tmat);
1458 
1459  dob = make_dupli(ctx, object, mat, a);
1460  dob->particle_system = psys;
1461 
1462  psys_get_dupli_texture(psys, part, sim.psmd, pa, cpa, dob->uv, dob->orco);
1463 
1464  b++;
1465  }
1467  }
1468  else {
1469  float obmat[4][4];
1470  copy_m4_m4(obmat, ob->obmat);
1471 
1472  float vec[3];
1473  copy_v3_v3(vec, obmat[3]);
1474  zero_v3(obmat[3]);
1475 
1476  /* Particle rotation uses x-axis as the aligned axis,
1477  * so pre-rotate the object accordingly. */
1478  if ((part->draw & PART_DRAW_ROTATE_OB) == 0) {
1479  float xvec[3], q[4], size_mat[4][4], original_size[3];
1480 
1481  mat4_to_size(original_size, obmat);
1482  size_to_mat4(size_mat, original_size);
1483 
1484  xvec[0] = -1.0f;
1485  xvec[1] = xvec[2] = 0;
1486  vec_to_quat(q, xvec, ob->trackflag, ob->upflag);
1487  quat_to_mat4(obmat, q);
1488  obmat[3][3] = 1.0f;
1489 
1490  /* Add scaling if requested. */
1491  if ((part->draw & PART_DRAW_NO_SCALE_OB) == 0) {
1492  mul_m4_m4m4(obmat, obmat, size_mat);
1493  }
1494  }
1495  else if (part->draw & PART_DRAW_NO_SCALE_OB) {
1496  /* Remove scaling. */
1497  float size_mat[4][4], original_size[3];
1498 
1499  mat4_to_size(original_size, obmat);
1500  size_to_mat4(size_mat, original_size);
1501  invert_m4(size_mat);
1502 
1503  mul_m4_m4m4(obmat, obmat, size_mat);
1504  }
1505 
1506  mul_m4_m4m4(tmat, pamat, obmat);
1507  mul_mat3_m4_fl(tmat, size * scale);
1508 
1509  copy_m4_m4(mat, tmat);
1510 
1511  if (part->draw & PART_DRAW_GLOBAL_OB) {
1512  add_v3_v3v3(mat[3], mat[3], vec);
1513  }
1514 
1515  dob = make_dupli(ctx, ob, mat, a);
1516  dob->particle_system = psys;
1517  psys_get_dupli_texture(psys, part, sim.psmd, pa, cpa, dob->uv, dob->orco);
1518  }
1519  }
1520 
1521  BLI_rng_free(rng);
1522  psys_sim_data_free(&sim);
1523  }
1524 
1525  /* Clean up. */
1526  if (oblist) {
1527  MEM_freeN(oblist);
1528  }
1529 }
1530 
1531 static void make_duplis_particles(const DupliContext *ctx)
1532 {
1533  /* Particle system take up one level in id, the particles another. */
1534  int psysid;
1535  LISTBASE_FOREACH_INDEX (ParticleSystem *, psys, &ctx->object->particlesystem, psysid) {
1536  /* Particles create one more level for persistent `psys` index. */
1537  DupliContext pctx;
1538  if (copy_dupli_context(&pctx, ctx, ctx->object, nullptr, psysid)) {
1539  make_duplis_particle_system(&pctx, psys);
1540  }
1541  }
1542 }
1543 
1545  OB_DUPLIPARTS, /* type */
1546  make_duplis_particles /* make_duplis */
1547 };
1548 
1551 /* -------------------------------------------------------------------- */
1556 {
1557  int transflag = ctx->object->transflag;
1558  int visibility_flag = ctx->object->visibility_flag;
1559 
1560  if ((transflag & OB_DUPLI) == 0 && ctx->object->runtime.geometry_set_eval == nullptr) {
1561  return nullptr;
1562  }
1563 
1564  /* Should the dupli's be generated for this object? - Respect restrict flags. */
1565  if (DEG_get_mode(ctx->depsgraph) == DAG_EVAL_RENDER ? (visibility_flag & OB_HIDE_RENDER) :
1566  (visibility_flag & OB_HIDE_VIEWPORT)) {
1567  return nullptr;
1568  }
1569 
1570  /* Give "Object as Font" instances higher priority than geometry set instances, to retain
1571  * the behavior from before curve object meshes were processed as instances internally. */
1572  if (transflag & OB_DUPLIVERTS) {
1573  if (ctx->object->type == OB_FONT) {
1574  return &gen_dupli_verts_font;
1575  }
1576  }
1577 
1578  if (ctx->object->runtime.geometry_set_eval != nullptr) {
1580  return &gen_dupli_geometry_set;
1581  }
1582  }
1583 
1584  if (transflag & OB_DUPLIPARTS) {
1585  return &gen_dupli_particles;
1586  }
1587  if (transflag & OB_DUPLIVERTS) {
1588  if (ctx->object->type == OB_MESH) {
1589  return &gen_dupli_verts;
1590  }
1591  }
1592  else if (transflag & OB_DUPLIFACES) {
1593  if (ctx->object->type == OB_MESH) {
1594  return &gen_dupli_faces;
1595  }
1596  }
1597  else if (transflag & OB_DUPLICOLLECTION) {
1598  return &gen_dupli_collection;
1599  }
1600 
1601  return nullptr;
1602 }
1603 
1606 /* -------------------------------------------------------------------- */
1611 {
1612  ListBase *duplilist = MEM_cnew<ListBase>("duplilist");
1613  DupliContext ctx;
1614  Vector<Object *> instance_stack;
1615  instance_stack.append(ob);
1616  init_context(&ctx, depsgraph, sce, ob, nullptr, instance_stack);
1617  if (ctx.gen) {
1618  ctx.duplilist = duplilist;
1619  ctx.gen->make_duplis(&ctx);
1620  }
1621 
1622  return duplilist;
1623 }
1624 
1626 {
1627  BLI_freelistN(lb);
1628  MEM_freeN(lb);
1629 }
1630 
typedef float(TangentPoint)[2]
struct ListBase BKE_collection_object_cache_get(struct Collection *collection)
Definition: collection.c:787
#define FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_BEGIN(_collection, _object, _mode)
#define FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_END
void * CustomData_get_layer_n(const struct CustomData *data, int type, int n)
void * CustomData_get_layer(const struct CustomData *data, int type)
int CustomData_get_n_offset(const struct CustomData *data, int type, int n)
int CustomData_get_render_layer(const struct CustomData *data, int type)
BMEditMesh * BKE_editmesh_from_object(struct Object *ob)
Return the BMEditMesh for a given object.
Definition: editmesh.c:58
void BKE_editmesh_cache_ensure_vert_normals(struct BMEditMesh *em, struct EditMeshData *emd)
bool BKE_object_has_geometry_set_instances(const struct Object *ob)
const float(* BKE_mesh_vertex_normals_ensure(const struct Mesh *mesh))[3]
General operations, lookup, etc. for blender objects.
struct Mesh * BKE_object_get_evaluated_mesh(const struct Object *object)
struct Mesh * BKE_object_get_editmesh_eval_cage(const struct Object *object)
void psys_get_dupli_path_transform(struct ParticleSimulationData *sim, struct ParticleData *pa, struct ChildParticle *cpa, struct ParticleCacheKey *cache, float mat[4][4], float *scale)
Definition: particle.c:5139
void psys_get_dupli_texture(struct ParticleSystem *psys, struct ParticleSettings *part, struct ParticleSystemModifierData *psmd, struct ParticleData *pa, struct ChildParticle *cpa, float uv[2], float orco[3])
Definition: particle.c:5052
bool psys_check_enabled(struct Object *ob, struct ParticleSystem *psys, bool use_render_params)
Definition: particle.c:801
void psys_sim_data_free(struct ParticleSimulationData *sim)
Definition: particle.c:726
struct ParticleSystemModifierData * psys_get_modifier(struct Object *ob, struct ParticleSystem *psys)
Definition: particle.c:2230
void psys_find_group_weights(struct ParticleSettings *part)
Definition: particle.c:836
float psys_get_child_size(struct ParticleSystem *psys, struct ChildParticle *cpa, float cfra, float *pa_time)
void psys_sim_data_init(struct ParticleSimulationData *sim)
Definition: particle.c:685
bool psys_get_particle_state(struct ParticleSimulationData *sim, int p, struct ParticleKey *state, bool always)
Definition: particle.c:4884
bool BKE_vfont_to_curve_ex(struct Object *ob, struct Curve *cu, int mode, struct ListBase *r_nubase, const char32_t **r_text, int *r_text_len, bool *r_text_free, struct CharTrans **r_chartransdata)
Definition: vfont.c:1709
#define BLI_assert(a)
Definition: BLI_assert.h:46
GHash * BLI_ghash_int_new_ex(const char *info, unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition: BLI_ghash.c:710
void ** BLI_ghash_lookup_p(GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:748
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition: BLI_ghash.c:863
BLI_INLINE unsigned int BLI_hash_int(unsigned int k)
Definition: BLI_hash.h:89
BLI_INLINE unsigned int BLI_hash_string(const char *str)
Definition: BLI_hash.h:69
BLI_INLINE unsigned int BLI_hash_int_2d(unsigned int kx, unsigned int ky)
Definition: BLI_hash.h:53
BLI_INLINE bool BLI_listbase_is_empty(const struct ListBase *lb)
Definition: BLI_listbase.h:269
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:336
#define LISTBASE_FOREACH_INDEX(type, var, list, index_var)
Definition: BLI_listbase.h:344
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:466
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:80
void * BLI_findptr(const struct ListBase *listbase, const void *ptr, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
float area_poly_v3(const float verts[][3], unsigned int nr)
Definition: math_geom.c:125
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
Definition: math_matrix.c:259
void mul_mat3_m4_fl(float R[4][4], float f)
Definition: math_matrix.c:978
bool invert_m4(float R[4][4])
Definition: math_matrix.c:1206
void copy_m3_m4(float m1[3][3], const float m2[4][4])
Definition: math_matrix.c:87
void mul_m4_m4_pre(float R[4][4], const float A[4][4])
Definition: math_matrix.c:372
void unit_m4(float m[4][4])
Definition: rct.c:1090
void mul_mat3_m4_v3(const float M[4][4], float r[3])
Definition: math_matrix.c:790
bool invert_m4_m4(float R[4][4], const float A[4][4])
Definition: math_matrix.c:1287
void size_to_mat4(float R[4][4], const float size[3])
Definition: math_matrix.c:2111
void mul_m4_v3(const float M[4][4], float r[3])
Definition: math_matrix.c:729
void copy_m4_m4(float m1[4][4], const float m2[4][4])
Definition: math_matrix.c:77
void loc_quat_size_to_mat4(float R[4][4], const float loc[3], const float quat[4], const float size[3])
Definition: math_matrix.c:2596
void mat4_to_size(float size[3], const float M[4][4])
Definition: math_matrix.c:2138
void mul_m4_m3m4(float R[4][4], const float A[3][3], const float B[4][4])
Definition: math_matrix.c:500
void vec_to_quat(float q[4], const float vec[3], short axis, short upflag)
void unit_qt(float q[4])
Definition: math_rotation.c:27
float normalize_qt_qt(float r[4], const float q[4])
void tri_to_quat_ex(float quat[4], const float v1[3], const float v2[3], const float v3[3], const float no_orig[3])
void axis_angle_to_mat4_single(float R[4][4], char axis, float angle)
void quat_to_mat4(float mat[4][4], const float q[4])
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
MINLINE void madd_v2_v2fl(float r[2], const float a[2], float f)
MINLINE void sub_v3_v3(float r[3], const float a[3])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void negate_v3_v3(float r[3], const float a[3])
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void zero_v3(float r[3])
Random number functions.
void int BLI_rng_get_int(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition: rand.cc:78
void BLI_rng_free(struct RNG *rng) ATTR_NONNULL(1)
Definition: rand.cc:58
struct RNG * BLI_rng_new_srandom(unsigned int seed)
Definition: rand.cc:46
Strict compiler flags for areas of code we want to ensure don't do conversions without us knowing abo...
#define BLI_UTF8_MAX
size_t BLI_str_utf8_from_unicode(unsigned int c, char *outbuf, size_t outbuf_len) ATTR_NONNULL(2)
Definition: string_utf8.c:575
unsigned int uint
Definition: BLI_sys_types.h:67
#define STREQLEN(a, b, n)
#define UNLIKELY(x)
#define ELEM(...)
#define POINTER_FROM_UINT(i)
static uint8 component(Color32 c, uint i)
Definition: ColorBlock.cpp:108
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:35
eEvaluationMode
Definition: DEG_depsgraph.h:44
@ DAG_EVAL_RENDER
Definition: DEG_depsgraph.h:46
float DEG_get_ctime(const Depsgraph *graph)
#define DEG_OBJECT_ITER_END
eEvaluationMode DEG_get_mode(const Depsgraph *graph)
#define DEG_OBJECT_ITER_BEGIN(graph_, instance_, flag_)
@ DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY
@ DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET
bool DEG_is_evaluated_id(const struct ID *id)
struct Object * DEG_get_evaluated_object(const struct Depsgraph *depsgraph, struct Object *object)
Object groups, one object can be in many groups at once.
@ CD_MLOOPUV
@ ME_WRAPPER_TYPE_BMESH
@ OB_HIDE_RENDER
@ OB_HIDE_VIEWPORT
#define OB_DONE
#define MAX_DUPLI_RECUR
@ OB_MBALL
@ OB_FONT
@ OB_MESH
@ OB_POINTCLOUD
@ OB_VOLUME
@ OB_CURVES_LEGACY
@ OB_CURVES
@ OB_DUPLIFACES
@ OB_DUPLI
@ OB_DUPLIPARTS
@ OB_DUPLIVERTS
@ OB_DUPLICOLLECTION
@ OB_DUPLIROT
@ OB_DUPLIFACES_SCALE
#define PART_DRAW_OB
#define PARS_UNEXIST
#define PART_DRAW_REND
#define PARS_NO_DISP
@ PART_DRAW_WHOLE_GR
@ PART_DRAW_GLOBAL_OB
@ PART_DRAW_PARENT
@ PART_DRAW_NO_SCALE_OB
@ PART_DRAW_COUNT_GR
@ PART_DRAW_RAND_GR
@ PART_DRAW_ROTATE_OB
@ PART_HAIR
#define PART_DRAW_GR
#define PSYS_HAIR_DONE
#define OBEDIT_FROM_OBACT(ob)
#define FO_DUPLI
float float4x4[4][4]
float float3[3]
Read Guarded memory(de)allocation.
#define BM_FACE_FIRST_LOOP(p)
Definition: bmesh_class.h:622
@ BM_VERT
Definition: bmesh_class.h:383
#define BM_elem_index_get(ele)
Definition: bmesh_inline.h:110
#define BM_ITER_MESH_INDEX(ele, iter, bm, itype, indexvar)
@ BM_VERTS_OF_MESH
@ BM_FACES_OF_MESH
ATTR_WARN_UNUSED_RESULT const BMVert * v
void BM_face_uv_calc_center_median(const BMFace *f, const int cd_loop_uv_offset, float r_cent[2])
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition: btQuadWord.h:119
Object & object() const
Collection & collection() const
const GeometrySet & geometry_set() const
constexpr const T * data() const
Definition: BLI_span.hh:203
constexpr int64_t size() const
Definition: BLI_span.hh:240
constexpr IndexRange index_range() const
Definition: BLI_span.hh:401
bool contains(const T &value) const
Definition: BLI_vector.hh:837
void append(const T &value)
Definition: BLI_vector.hh:433
void remove_last()
Definition: BLI_vector.hh:715
Scene scene
Curve curve
const Depsgraph * depsgraph
SyclQueue void void size_t num_bytes void
const int state
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
#define G(x, y, z)
#define sqrtf(x)
Definition: metal/compat.h:243
static unsigned a[3]
Definition: RandGen.cpp:78
static void area(int d1, int d2, int e1, int e2, float weights[2])
vec_base< T, Size > normalize(const vec_base< T, Size > &v)
vec_base< T, 3 > cross_poly(Span< vec_base< T, 3 >> poly)
vec_base< float, 3 > float3
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
static void make_duplis_verts(const DupliContext *ctx)
static void make_recursive_duplis(const DupliContext *ctx, Object *ob, const float space_mat[4][4], int index)
static void make_child_duplis(const DupliContext *ctx, void *userdata, MakeChildDuplisFunc make_child_duplis_cb)
static DupliObject * face_dupli(const DupliContext *ctx, Object *inst_ob, const float child_imat[4][4], const int index, const bool use_scale, const float scale_fac, Span< float3 > coords)
static void make_duplis_collection(const DupliContext *ctx)
static void make_child_duplis_verts_from_mesh(const DupliContext *ctx, void *userdata, Object *inst_ob)
ListBase * object_duplilist(Depsgraph *depsgraph, Scene *sce, Object *ob)
static void make_duplis_font(const DupliContext *ctx)
static DupliObject * vertex_dupli(const DupliContext *ctx, Object *inst_ob, const float child_imat[4][4], int index, const float co[3], const float no[3], const bool use_rotation)
static bool copy_dupli_context(DupliContext *r_ctx, const DupliContext *ctx, Object *ob, const float mat[4][4], int index)
static void make_child_duplis_faces_from_editmesh(const DupliContext *ctx, void *userdata, Object *inst_ob)
static const DupliGenerator gen_dupli_verts
static void make_duplis_particles(const DupliContext *ctx)
static DupliObject * face_dupli_from_mesh(const DupliContext *ctx, Object *inst_ob, const float child_imat[4][4], const int index, const bool use_scale, const float scale_fac, const MPoly *mpoly, const MLoop *mloopstart, const MVert *mvert)
static const DupliGenerator gen_dupli_collection
static DupliObject * face_dupli_from_editmesh(const DupliContext *ctx, Object *inst_ob, const float child_imat[4][4], const int index, const bool use_scale, const float scale_fac, BMFace *f, const float(*vert_coords)[3])
static void make_duplis_particle_system(const DupliContext *ctx, ParticleSystem *psys)
static Object * find_family_object(Main *bmain, const char *family, size_t family_len, unsigned int ch, GHash *family_gh)
static void init_context(DupliContext *r_ctx, Depsgraph *depsgraph, Scene *scene, Object *ob, const float space_mat[4][4], Vector< Object * > &instance_stack)
static void get_duplivert_transform(const float co[3], const float no[3], const bool use_rotation, const short axis, const short upflag, float r_mat[4][4])
static void make_child_duplis_faces_from_mesh(const DupliContext *ctx, void *userdata, Object *inst_ob)
void(*)(const DupliContext *ctx, void *userdata, Object *child) MakeChildDuplisFunc
void free_object_duplilist(ListBase *lb)
static const DupliGenerator * get_dupli_generator(const DupliContext *ctx)
static bool is_child(const Object *ob, const Object *parent)
static DupliObject * make_dupli(const DupliContext *ctx, Object *ob, const float mat[4][4], int index)
static const Mesh * mesh_data_from_duplicator_object(Object *ob, BMEditMesh **r_em, const float(**r_vert_coords)[3], const float(**r_vert_normals)[3])
static const DupliGenerator gen_dupli_particles
static const DupliGenerator gen_dupli_faces
static void make_duplis_geometry_set_impl(const DupliContext *ctx, const GeometrySet &geometry_set, const float parent_transform[4][4], bool geometry_set_is_instance)
static void make_child_duplis_verts_from_editmesh(const DupliContext *ctx, void *userdata, Object *inst_ob)
static const DupliGenerator gen_dupli_geometry_set
static const DupliGenerator gen_dupli_verts_font
static void get_dupliface_transform_from_coords(Span< float3 > coords, const bool use_scale, const float scale_fac, float r_mat[4][4])
static void make_duplis_faces(const DupliContext *ctx)
static void make_duplis_geometry_set(const DupliContext *ctx)
static void text_free(SpaceLink *sl)
Definition: space_text.c:85
__int64 int64_t
Definition: stdint.h:89
struct BMesh * bm
Definition: BKE_editmesh.h:40
int len
Definition: bmesh_class.h:267
struct BMVert * v
Definition: bmesh_class.h:153
struct BMLoop * next
Definition: bmesh_class.h:233
float co[3]
Definition: bmesh_class.h:87
float no[3]
Definition: bmesh_class.h:88
char elem_index_dirty
Definition: bmesh_class.h:305
CustomData ldata
Definition: bmesh_class.h:337
float yof
Definition: BKE_vfont.h:20
float xof
Definition: BKE_vfont.h:20
float rot
Definition: BKE_vfont.h:21
float instance_offset[3]
float xof
char family[64]
float fsize
float yof
Collection * collection
Definition: object_dupli.cc:70
Object * obedit
Definition: object_dupli.cc:72
float space_mat[4][4]
Definition: object_dupli.cc:79
Object * root_object
Definition: object_dupli.cc:76
Object * object
Definition: object_dupli.cc:78
int persistent_id[MAX_DUPLI_RECUR]
Definition: object_dupli.cc:88
Scene * scene
Definition: object_dupli.cc:74
ListBase * duplilist
Definition: object_dupli.cc:94
Depsgraph * depsgraph
Definition: object_dupli.cc:68
const struct DupliGenerator * gen
Definition: object_dupli.cc:91
Vector< Object * > * instance_stack
Definition: object_dupli.cc:86
void(* make_duplis)(const DupliContext *ctx)
Definition: object_dupli.cc:99
float uv[2]
Definition: BKE_duplilist.h:38
float mat[4][4]
Definition: BKE_duplilist.h:37
struct ParticleSystem * particle_system
Definition: BKE_duplilist.h:48
int persistent_id[8]
Definition: BKE_duplilist.h:45
float orco[3]
Definition: BKE_duplilist.h:38
struct ID * ob_data
Definition: BKE_duplilist.h:36
struct Object * ob
Definition: BKE_duplilist.h:34
unsigned int random_id
Definition: BKE_duplilist.h:51
float const (* vertexNos)[3]
const float(* vertexCos)[3]
FaceDupliData_Params params
const float(* vert_coords)[3]
FaceDupliData_Params params
const float(* orco)[3]
const MLoopUV * mloopuv
const MVert * mvert
const MPoly * mpoly
const MLoop * mloop
const DupliContext * ctx
const PointCloud * get_pointcloud_for_read() const
const Volume * get_volume_for_read() const
const GeometryComponent * get_component_for_read(GeometryComponentType component_type) const
const Mesh * get_mesh_for_read() const
Definition: DNA_ID.h:368
char name[66]
Definition: DNA_ID.h:378
unsigned int v
float co[3]
Definition: BKE_main.h:121
ListBase objects
Definition: BKE_main.h:170
struct EditMeshData * edit_data
CustomData vdata
struct MVert * mvert
int totvert
struct MLoop * mloop
Mesh_Runtime runtime
int totpoly
struct MPoly * mpoly
CustomData ldata
struct GeometrySet * geometry_set_eval
ListBase particlesystem
short transflag
struct Collection * instance_collection
float imat[4][4]
float parentinv[4][4]
Object_Runtime runtime
short visibility_flag
float obmat[4][4]
float instance_faces_scale
struct Object * parent
short trackflag
short upflag
void * data
struct Collection * instance_collection
struct ListBase instance_weights
struct Object * instance_object
struct Depsgraph * depsgraph
Definition: BKE_particle.h:69
struct ParticleSystemModifierData * psmd
Definition: BKE_particle.h:73
struct Scene * scene
Definition: BKE_particle.h:70
struct ParticleSystem * psys
Definition: BKE_particle.h:72
struct Object * ob
Definition: BKE_particle.h:71
ChildParticle * child
ParticleData * particles
ParticleSettings * part
struct ParticleCacheKey ** childcache
struct ParticleCacheKey ** pathcache
Definition: rand.cc:33
const float(* vert_normals)[3]
VertexDupliData_Params params
const float(* vert_coords)[3]
const MVert * mvert
VertexDupliData_Params params
const float(* orco)[3]
const float(* vert_normals)[3]
const DupliContext * ctx