object_update.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * The Original Code is Copyright (C) 20014 by Blender Foundation.
  19. * All rights reserved.
  20. *
  21. * Contributor(s): Sergey Sharybin.
  22. *
  23. * ***** END GPL LICENSE BLOCK *****
  24. */
  25. /** \file blender/blenkernel/intern/object_update.c
  26. * \ingroup bke
  27. */
  28. #include "DNA_anim_types.h"
  29. #include "DNA_constraint_types.h"
  30. #include "DNA_group_types.h"
  31. #include "DNA_key_types.h"
  32. #include "DNA_material_types.h"
  33. #include "DNA_scene_types.h"
  34. #include "BLI_blenlib.h"
  35. #include "BLI_utildefines.h"
  36. #include "BLI_math.h"
  37. #include "BLI_threads.h"
  38. #include "BKE_global.h"
  39. #include "BKE_armature.h"
  40. #include "BKE_action.h"
  41. #include "BKE_constraint.h"
  42. #include "BKE_depsgraph.h"
  43. #include "BKE_DerivedMesh.h"
  44. #include "BKE_animsys.h"
  45. #include "BKE_displist.h"
  46. #include "BKE_effect.h"
  47. #include "BKE_key.h"
  48. #include "BKE_lamp.h"
  49. #include "BKE_lattice.h"
  50. #include "BKE_editmesh.h"
  51. #include "BKE_object.h"
  52. #include "BKE_particle.h"
  53. #include "BKE_pointcache.h"
  54. #include "BKE_scene.h"
  55. #include "BKE_material.h"
  56. #include "BKE_image.h"
  57. #include "DEG_depsgraph.h"
  58. #ifdef WITH_LEGACY_DEPSGRAPH
  59. # define DEBUG_PRINT if (!DEG_depsgraph_use_legacy() && G.debug & G_DEBUG_DEPSGRAPH) printf
  60. #else
  61. # define DEBUG_PRINT if (G.debug & G_DEBUG_DEPSGRAPH) printf
  62. #endif
  63. static ThreadMutex material_lock = BLI_MUTEX_INITIALIZER;
  64. void BKE_object_eval_local_transform(EvaluationContext *UNUSED(eval_ctx),
  65. Scene *UNUSED(scene),
  66. Object *ob)
  67. {
  68. DEBUG_PRINT("%s on %s\n", __func__, ob->id.name);
  69. /* calculate local matrix */
  70. BKE_object_to_mat4(ob, ob->obmat);
  71. }
  72. /* Evaluate parent */
  73. /* NOTE: based on solve_parenting(), but with the cruft stripped out */
  74. void BKE_object_eval_parent(EvaluationContext *UNUSED(eval_ctx),
  75. Scene *scene,
  76. Object *ob)
  77. {
  78. Object *par = ob->parent;
  79. float totmat[4][4];
  80. float tmat[4][4];
  81. float locmat[4][4];
  82. DEBUG_PRINT("%s on %s\n", __func__, ob->id.name);
  83. /* get local matrix (but don't calculate it, as that was done already!) */
  84. // XXX: redundant?
  85. copy_m4_m4(locmat, ob->obmat);
  86. /* get parent effect matrix */
  87. BKE_object_get_parent_matrix(scene, ob, par, totmat);
  88. /* total */
  89. mul_m4_m4m4(tmat, totmat, ob->parentinv);
  90. mul_m4_m4m4(ob->obmat, tmat, locmat);
  91. /* origin, for help line */
  92. if ((ob->partype & PARTYPE) == PARSKEL) {
  93. copy_v3_v3(ob->orig, par->obmat[3]);
  94. }
  95. else {
  96. copy_v3_v3(ob->orig, totmat[3]);
  97. }
  98. }
  99. void BKE_object_eval_constraints(EvaluationContext *UNUSED(eval_ctx),
  100. Scene *scene,
  101. Object *ob)
  102. {
  103. bConstraintOb *cob;
  104. float ctime = BKE_scene_frame_get(scene);
  105. DEBUG_PRINT("%s on %s\n", __func__, ob->id.name);
  106. /* evaluate constraints stack */
  107. /* TODO: split this into:
  108. * - pre (i.e. BKE_constraints_make_evalob, per-constraint (i.e.
  109. * - inner body of BKE_constraints_solve),
  110. * - post (i.e. BKE_constraints_clear_evalob)
  111. *
  112. * Not sure why, this is from Joshua - sergey
  113. *
  114. */
  115. cob = BKE_constraints_make_evalob(scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
  116. BKE_constraints_solve(&ob->constraints, cob, ctime);
  117. BKE_constraints_clear_evalob(cob);
  118. }
  119. void BKE_object_eval_done(EvaluationContext *UNUSED(eval_ctx), Object *ob)
  120. {
  121. DEBUG_PRINT("%s on %s\n", __func__, ob->id.name);
  122. /* Set negative scale flag in object. */
  123. if (is_negative_m4(ob->obmat)) ob->transflag |= OB_NEG_SCALE;
  124. else ob->transflag &= ~OB_NEG_SCALE;
  125. }
  126. void BKE_object_handle_data_update(EvaluationContext *eval_ctx,
  127. Scene *scene,
  128. Object *ob)
  129. {
  130. ID *data_id = (ID *)ob->data;
  131. AnimData *adt = BKE_animdata_from_id(data_id);
  132. Key *key;
  133. float ctime = BKE_scene_frame_get(scene);
  134. if (G.debug & G_DEBUG_DEPSGRAPH)
  135. printf("recalcdata %s\n", ob->id.name + 2);
  136. /* TODO(sergey): Only used by legacy depsgraph. */
  137. if (adt) {
  138. /* evaluate drivers - datalevel */
  139. /* XXX: for mesh types, should we push this to derivedmesh instead? */
  140. BKE_animsys_evaluate_animdata(scene, data_id, adt, ctime, ADT_RECALC_DRIVERS);
  141. }
  142. /* TODO(sergey): Only used by legacy depsgraph. */
  143. key = BKE_key_from_object(ob);
  144. if (key && key->block.first) {
  145. if (!(ob->shapeflag & OB_SHAPE_LOCK))
  146. BKE_animsys_evaluate_animdata(scene, &key->id, key->adt, ctime, ADT_RECALC_DRIVERS);
  147. }
  148. /* includes all keys and modifiers */
  149. switch (ob->type) {
  150. case OB_MESH:
  151. {
  152. BMEditMesh *em = (ob == scene->obedit) ? BKE_editmesh_from_object(ob) : NULL;
  153. uint64_t data_mask = scene->customdata_mask | CD_MASK_BAREMESH;
  154. #ifdef WITH_FREESTYLE
  155. /* make sure Freestyle edge/face marks appear in DM for render (see T40315) */
  156. if (eval_ctx->mode != DAG_EVAL_VIEWPORT) {
  157. data_mask |= CD_MASK_FREESTYLE_EDGE | CD_MASK_FREESTYLE_FACE;
  158. }
  159. #endif
  160. if (em) {
  161. makeDerivedMesh(scene, ob, em, data_mask, false); /* was CD_MASK_BAREMESH */
  162. }
  163. else {
  164. makeDerivedMesh(scene, ob, NULL, data_mask, false);
  165. }
  166. break;
  167. }
  168. case OB_ARMATURE:
  169. if (ID_IS_LINKED_DATABLOCK(ob) && ob->proxy_from) {
  170. if (BKE_pose_copy_result(ob->pose, ob->proxy_from->pose) == false) {
  171. printf("Proxy copy error, lib Object: %s proxy Object: %s\n",
  172. ob->id.name + 2, ob->proxy_from->id.name + 2);
  173. }
  174. }
  175. else {
  176. BKE_pose_where_is(scene, ob);
  177. }
  178. break;
  179. case OB_MBALL:
  180. BKE_displist_make_mball(eval_ctx, scene, ob);
  181. break;
  182. case OB_CURVE:
  183. case OB_SURF:
  184. case OB_FONT:
  185. BKE_displist_make_curveTypes(scene, ob, 0);
  186. break;
  187. case OB_LATTICE:
  188. BKE_lattice_modifiers_calc(scene, ob);
  189. break;
  190. case OB_EMPTY:
  191. if (ob->empty_drawtype == OB_EMPTY_IMAGE && ob->data)
  192. if (BKE_image_is_animated(ob->data))
  193. BKE_image_user_check_frame_calc(ob->iuser, (int)ctime, 0);
  194. break;
  195. }
  196. /* related materials */
  197. /* XXX: without depsgraph tagging, this will always need to be run, which will be slow!
  198. * However, not doing anything (or trying to hack around this lack) is not an option
  199. * anymore, especially due to Cycles [#31834]
  200. */
  201. if (ob->totcol) {
  202. int a;
  203. if (ob->totcol != 0) {
  204. BLI_mutex_lock(&material_lock);
  205. for (a = 1; a <= ob->totcol; a++) {
  206. Material *ma = give_current_material(ob, a);
  207. if (ma) {
  208. /* recursively update drivers for this material */
  209. material_drivers_update(scene, ma, ctime);
  210. }
  211. }
  212. BLI_mutex_unlock(&material_lock);
  213. }
  214. }
  215. else if (ob->type == OB_LAMP)
  216. lamp_drivers_update(scene, ob->data, ctime);
  217. /* particles */
  218. if (ob != scene->obedit && ob->particlesystem.first) {
  219. ParticleSystem *tpsys, *psys;
  220. DerivedMesh *dm;
  221. ob->transflag &= ~OB_DUPLIPARTS;
  222. psys = ob->particlesystem.first;
  223. while (psys) {
  224. /* ensure this update always happens even if psys is disabled */
  225. if (psys->recalc & PSYS_RECALC_TYPE) {
  226. psys_changed_type(ob, psys);
  227. }
  228. if (psys_check_enabled(ob, psys, eval_ctx->mode == DAG_EVAL_RENDER)) {
  229. /* check use of dupli objects here */
  230. if (psys->part && (psys->part->draw_as == PART_DRAW_REND || eval_ctx->mode == DAG_EVAL_RENDER) &&
  231. ((psys->part->ren_as == PART_DRAW_OB && psys->part->dup_ob) ||
  232. (psys->part->ren_as == PART_DRAW_GR && psys->part->dup_group)))
  233. {
  234. ob->transflag |= OB_DUPLIPARTS;
  235. }
  236. particle_system_update(scene, ob, psys, (eval_ctx->mode == DAG_EVAL_RENDER));
  237. psys = psys->next;
  238. }
  239. else if (psys->flag & PSYS_DELETE) {
  240. tpsys = psys->next;
  241. BLI_remlink(&ob->particlesystem, psys);
  242. psys_free(ob, psys);
  243. psys = tpsys;
  244. }
  245. else
  246. psys = psys->next;
  247. }
  248. if (eval_ctx->mode == DAG_EVAL_RENDER && ob->transflag & OB_DUPLIPARTS) {
  249. /* this is to make sure we get render level duplis in groups:
  250. * the derivedmesh must be created before init_render_mesh,
  251. * since object_duplilist does dupliparticles before that */
  252. CustomDataMask data_mask = CD_MASK_BAREMESH | CD_MASK_MFACE | CD_MASK_MTFACE | CD_MASK_MCOL;
  253. dm = mesh_create_derived_render(scene, ob, data_mask);
  254. dm->release(dm);
  255. for (psys = ob->particlesystem.first; psys; psys = psys->next)
  256. psys_get_modifier(ob, psys)->flag &= ~eParticleSystemFlag_psys_updated;
  257. }
  258. }
  259. /* quick cache removed */
  260. }
  261. void BKE_object_eval_uber_transform(EvaluationContext *UNUSED(eval_ctx),
  262. Scene *UNUSED(scene),
  263. Object *ob)
  264. {
  265. /* TODO(sergey): Currently it's a duplicate of logic in BKE_object_handle_update_ex(). */
  266. // XXX: it's almost redundant now...
  267. /* Handle proxy copy for target, */
  268. if (ID_IS_LINKED_DATABLOCK(ob) && ob->proxy_from) {
  269. if (ob->proxy_from->proxy_group) {
  270. /* Transform proxy into group space. */
  271. Object *obg = ob->proxy_from->proxy_group;
  272. float imat[4][4];
  273. invert_m4_m4(imat, obg->obmat);
  274. mul_m4_m4m4(ob->obmat, imat, ob->proxy_from->obmat);
  275. /* Should always be true. */
  276. if (obg->dup_group) {
  277. add_v3_v3(ob->obmat[3], obg->dup_group->dupli_ofs);
  278. }
  279. }
  280. else
  281. copy_m4_m4(ob->obmat, ob->proxy_from->obmat);
  282. }
  283. ob->recalc &= ~(OB_RECALC_OB | OB_RECALC_TIME);
  284. if (ob->data == NULL) {
  285. ob->recalc &= ~OB_RECALC_DATA;
  286. }
  287. }
  288. void BKE_object_eval_uber_data(EvaluationContext *eval_ctx,
  289. Scene *scene,
  290. Object *ob)
  291. {
  292. DEBUG_PRINT("%s on %s\n", __func__, ob->id.name);
  293. BLI_assert(ob->type != OB_ARMATURE);
  294. BKE_object_handle_data_update(eval_ctx, scene, ob);
  295. ob->recalc &= ~(OB_RECALC_DATA | OB_RECALC_TIME);
  296. }
  297. void BKE_object_eval_cloth(EvaluationContext *UNUSED(eval_ctx), Scene *scene, Object *object)
  298. {
  299. DEBUG_PRINT("%s on %s\n", __func__, object->id.name);
  300. BKE_ptcache_object_reset(scene, object, PTCACHE_RESET_DEPSGRAPH);
  301. }