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