action.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  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) 2001-2002 by NaN Holding BV.
  19. * All rights reserved.
  20. *
  21. * Contributor(s): Full recode, Ton Roosendaal, Crete 2005
  22. * Full recode, Joshua Leung, 2009
  23. *
  24. * ***** END GPL LICENSE BLOCK *****
  25. */
  26. /** \file blender/blenkernel/intern/action.c
  27. * \ingroup bke
  28. */
  29. #include <string.h>
  30. #include <math.h>
  31. #include <stdlib.h>
  32. #include <stddef.h>
  33. #include "MEM_guardedalloc.h"
  34. #include "DNA_anim_types.h"
  35. #include "DNA_armature_types.h"
  36. #include "DNA_constraint_types.h"
  37. #include "DNA_scene_types.h"
  38. #include "DNA_object_types.h"
  39. #include "BLI_blenlib.h"
  40. #include "BLI_math.h"
  41. #include "BLI_string_utils.h"
  42. #include "BLI_utildefines.h"
  43. #include "BLI_ghash.h"
  44. #include "BLT_translation.h"
  45. #include "BKE_action.h"
  46. #include "BKE_anim.h"
  47. #include "BKE_animsys.h"
  48. #include "BKE_constraint.h"
  49. #include "BKE_deform.h"
  50. #include "BKE_depsgraph.h"
  51. #include "BKE_fcurve.h"
  52. #include "BKE_global.h"
  53. #include "BKE_idprop.h"
  54. #include "BKE_library.h"
  55. #include "BKE_library_query.h"
  56. #include "BKE_library_remap.h"
  57. #include "BKE_main.h"
  58. #include "BKE_object.h"
  59. #include "BIK_api.h"
  60. #include "RNA_access.h"
  61. /* *********************** NOTE ON POSE AND ACTION **********************
  62. *
  63. * - Pose is the local (object level) component of armature. The current
  64. * object pose is saved in files, and (will be) is presorted for dependency
  65. * - Actions have fewer (or other) channels, and write data to a Pose
  66. * - Currently ob->pose data is controlled in BKE_pose_where_is only. The (recalc)
  67. * event system takes care of calling that
  68. * - The NLA system (here too) uses Poses as interpolation format for Actions
  69. * - Therefore we assume poses to be static, and duplicates of poses have channels in
  70. * same order, for quick interpolation reasons
  71. *
  72. * ****************************** (ton) ************************************ */
  73. /* ***************** Library data level operations on action ************** */
  74. bAction *add_empty_action(Main *bmain, const char name[])
  75. {
  76. bAction *act;
  77. act = BKE_libblock_alloc(bmain, ID_AC, name);
  78. return act;
  79. }
  80. /* .................................. */
  81. // does copy_fcurve...
  82. void BKE_action_make_local(Main *bmain, bAction *act, const bool lib_local)
  83. {
  84. BKE_id_make_local_generic(bmain, &act->id, true, lib_local);
  85. }
  86. /* .................................. */
  87. /** Free (or release) any data used by this action (does not free the action itself). */
  88. void BKE_action_free(bAction *act)
  89. {
  90. /* No animdata here. */
  91. /* Free F-Curves */
  92. free_fcurves(&act->curves);
  93. /* Free groups */
  94. BLI_freelistN(&act->groups);
  95. /* Free pose-references (aka local markers) */
  96. BLI_freelistN(&act->markers);
  97. }
  98. /* .................................. */
  99. bAction *BKE_action_copy(Main *bmain, const bAction *src)
  100. {
  101. bAction *dst = NULL;
  102. bActionGroup *dgrp, *sgrp;
  103. FCurve *dfcu, *sfcu;
  104. if (src == NULL)
  105. return NULL;
  106. dst = BKE_libblock_copy(bmain, &src->id);
  107. /* duplicate the lists of groups and markers */
  108. BLI_duplicatelist(&dst->groups, &src->groups);
  109. BLI_duplicatelist(&dst->markers, &src->markers);
  110. /* copy F-Curves, fixing up the links as we go */
  111. BLI_listbase_clear(&dst->curves);
  112. for (sfcu = src->curves.first; sfcu; sfcu = sfcu->next) {
  113. /* duplicate F-Curve */
  114. dfcu = copy_fcurve(sfcu);
  115. BLI_addtail(&dst->curves, dfcu);
  116. /* fix group links (kindof bad list-in-list search, but this is the most reliable way) */
  117. for (dgrp = dst->groups.first, sgrp = src->groups.first; dgrp && sgrp; dgrp = dgrp->next, sgrp = sgrp->next) {
  118. if (sfcu->grp == sgrp) {
  119. dfcu->grp = dgrp;
  120. if (dgrp->channels.first == sfcu)
  121. dgrp->channels.first = dfcu;
  122. if (dgrp->channels.last == sfcu)
  123. dgrp->channels.last = dfcu;
  124. break;
  125. }
  126. }
  127. }
  128. BKE_id_copy_ensure_local(bmain, &src->id, &dst->id);
  129. return dst;
  130. }
  131. /* *************** Action Groups *************** */
  132. /* Get the active action-group for an Action */
  133. bActionGroup *get_active_actiongroup(bAction *act)
  134. {
  135. bActionGroup *agrp = NULL;
  136. if (act && act->groups.first) {
  137. for (agrp = act->groups.first; agrp; agrp = agrp->next) {
  138. if (agrp->flag & AGRP_ACTIVE)
  139. break;
  140. }
  141. }
  142. return agrp;
  143. }
  144. /* Make the given Action-Group the active one */
  145. void set_active_action_group(bAction *act, bActionGroup *agrp, short select)
  146. {
  147. bActionGroup *grp;
  148. /* sanity checks */
  149. if (act == NULL)
  150. return;
  151. /* Deactive all others */
  152. for (grp = act->groups.first; grp; grp = grp->next) {
  153. if ((grp == agrp) && (select))
  154. grp->flag |= AGRP_ACTIVE;
  155. else
  156. grp->flag &= ~AGRP_ACTIVE;
  157. }
  158. }
  159. /* Sync colors used for action/bone group with theme settings */
  160. void action_group_colors_sync(bActionGroup *grp, const bActionGroup *ref_grp)
  161. {
  162. /* only do color copying if using a custom color (i.e. not default color) */
  163. if (grp->customCol) {
  164. if (grp->customCol > 0) {
  165. /* copy theme colors on-to group's custom color in case user tries to edit color */
  166. bTheme *btheme = U.themes.first;
  167. ThemeWireColor *col_set = &btheme->tarm[(grp->customCol - 1)];
  168. memcpy(&grp->cs, col_set, sizeof(ThemeWireColor));
  169. }
  170. else {
  171. /* if a reference group is provided, use the custom color from there... */
  172. if (ref_grp) {
  173. /* assumption: reference group has a color set */
  174. memcpy(&grp->cs, &ref_grp->cs, sizeof(ThemeWireColor));
  175. }
  176. /* otherwise, init custom color with a generic/placeholder color set if
  177. * no previous theme color was used that we can just keep using
  178. */
  179. else if (grp->cs.solid[0] == 0) {
  180. /* define for setting colors in theme below */
  181. rgba_char_args_set(grp->cs.solid, 0xff, 0x00, 0x00, 255);
  182. rgba_char_args_set(grp->cs.select, 0x81, 0xe6, 0x14, 255);
  183. rgba_char_args_set(grp->cs.active, 0x18, 0xb6, 0xe0, 255);
  184. }
  185. }
  186. }
  187. }
  188. /* Add a new action group with the given name to the action */
  189. bActionGroup *action_groups_add_new(bAction *act, const char name[])
  190. {
  191. bActionGroup *agrp;
  192. /* sanity check: must have action and name */
  193. if (ELEM(NULL, act, name))
  194. return NULL;
  195. /* allocate a new one */
  196. agrp = MEM_callocN(sizeof(bActionGroup), "bActionGroup");
  197. /* make it selected, with default name */
  198. agrp->flag = AGRP_SELECTED;
  199. BLI_strncpy(agrp->name, name[0] ? name : DATA_("Group"), sizeof(agrp->name));
  200. /* add to action, and validate */
  201. BLI_addtail(&act->groups, agrp);
  202. BLI_uniquename(&act->groups, agrp, DATA_("Group"), '.', offsetof(bActionGroup, name), sizeof(agrp->name));
  203. /* return the new group */
  204. return agrp;
  205. }
  206. /* Add given channel into (active) group
  207. * - assumes that channel is not linked to anything anymore
  208. * - always adds at the end of the group
  209. */
  210. void action_groups_add_channel(bAction *act, bActionGroup *agrp, FCurve *fcurve)
  211. {
  212. /* sanity checks */
  213. if (ELEM(NULL, act, agrp, fcurve))
  214. return;
  215. /* if no channels anywhere, just add to two lists at the same time */
  216. if (BLI_listbase_is_empty(&act->curves)) {
  217. fcurve->next = fcurve->prev = NULL;
  218. agrp->channels.first = agrp->channels.last = fcurve;
  219. act->curves.first = act->curves.last = fcurve;
  220. }
  221. /* if the group already has channels, the F-Curve can simply be added to the list
  222. * (i.e. as the last channel in the group)
  223. */
  224. else if (agrp->channels.first) {
  225. /* if the group's last F-Curve is the action's last F-Curve too,
  226. * then set the F-Curve as the last for the action first so that
  227. * the lists will be in sync after linking
  228. */
  229. if (agrp->channels.last == act->curves.last)
  230. act->curves.last = fcurve;
  231. /* link in the given F-Curve after the last F-Curve in the group,
  232. * which means that it should be able to fit in with the rest of the
  233. * list seamlessly
  234. */
  235. BLI_insertlinkafter(&agrp->channels, agrp->channels.last, fcurve);
  236. }
  237. /* otherwise, need to find the nearest F-Curve in group before/after current to link with */
  238. else {
  239. bActionGroup *grp;
  240. /* firstly, link this F-Curve to the group */
  241. agrp->channels.first = agrp->channels.last = fcurve;
  242. /* step through the groups preceding this one, finding the F-Curve there to attach this one after */
  243. for (grp = agrp->prev; grp; grp = grp->prev) {
  244. /* if this group has F-Curves, we want weave the given one in right after the last channel there,
  245. * but via the Action's list not this group's list
  246. * - this is so that the F-Curve is in the right place in the Action,
  247. * but won't be included in the previous group
  248. */
  249. if (grp->channels.last) {
  250. /* once we've added, break here since we don't need to search any further... */
  251. BLI_insertlinkafter(&act->curves, grp->channels.last, fcurve);
  252. break;
  253. }
  254. }
  255. /* if grp is NULL, that means we fell through, and this F-Curve should be added as the new first
  256. * since group is (effectively) the first group. Thus, the existing first F-Curve becomes the
  257. * second in the chain, etc. etc.
  258. */
  259. if (grp == NULL)
  260. BLI_insertlinkbefore(&act->curves, act->curves.first, fcurve);
  261. }
  262. /* set the F-Curve's new group */
  263. fcurve->grp = agrp;
  264. }
  265. /* Remove the given channel from all groups */
  266. void action_groups_remove_channel(bAction *act, FCurve *fcu)
  267. {
  268. /* sanity checks */
  269. if (ELEM(NULL, act, fcu))
  270. return;
  271. /* check if any group used this directly */
  272. if (fcu->grp) {
  273. bActionGroup *agrp = fcu->grp;
  274. if (agrp->channels.first == agrp->channels.last) {
  275. if (agrp->channels.first == fcu) {
  276. BLI_listbase_clear(&agrp->channels);
  277. }
  278. }
  279. else if (agrp->channels.first == fcu) {
  280. if ((fcu->next) && (fcu->next->grp == agrp))
  281. agrp->channels.first = fcu->next;
  282. else
  283. agrp->channels.first = NULL;
  284. }
  285. else if (agrp->channels.last == fcu) {
  286. if ((fcu->prev) && (fcu->prev->grp == agrp))
  287. agrp->channels.last = fcu->prev;
  288. else
  289. agrp->channels.last = NULL;
  290. }
  291. fcu->grp = NULL;
  292. }
  293. /* now just remove from list */
  294. BLI_remlink(&act->curves, fcu);
  295. }
  296. /* Find a group with the given name */
  297. bActionGroup *BKE_action_group_find_name(bAction *act, const char name[])
  298. {
  299. /* sanity checks */
  300. if (ELEM(NULL, act, act->groups.first, name) || (name[0] == 0))
  301. return NULL;
  302. /* do string comparisons */
  303. return BLI_findstring(&act->groups, name, offsetof(bActionGroup, name));
  304. }
  305. /* Clear all 'temp' flags on all groups */
  306. void action_groups_clear_tempflags(bAction *act)
  307. {
  308. bActionGroup *agrp;
  309. /* sanity checks */
  310. if (ELEM(NULL, act, act->groups.first))
  311. return;
  312. /* flag clearing loop */
  313. for (agrp = act->groups.first; agrp; agrp = agrp->next)
  314. agrp->flag &= ~AGRP_TEMP;
  315. }
  316. /* *************** Pose channels *************** */
  317. /**
  318. * Return a pointer to the pose channel of the given name
  319. * from this pose.
  320. */
  321. bPoseChannel *BKE_pose_channel_find_name(const bPose *pose, const char *name)
  322. {
  323. if (ELEM(NULL, pose, name) || (name[0] == '\0'))
  324. return NULL;
  325. if (pose->chanhash)
  326. return BLI_ghash_lookup(pose->chanhash, (const void *)name);
  327. return BLI_findstring(&((const bPose *)pose)->chanbase, name, offsetof(bPoseChannel, name));
  328. }
  329. /**
  330. * Looks to see if the channel with the given name
  331. * already exists in this pose - if not a new one is
  332. * allocated and initialized.
  333. *
  334. * \note Use with care, not on Armature poses but for temporal ones.
  335. * \note (currently used for action constraints and in rebuild_pose).
  336. */
  337. bPoseChannel *BKE_pose_channel_verify(bPose *pose, const char *name)
  338. {
  339. bPoseChannel *chan;
  340. if (pose == NULL)
  341. return NULL;
  342. /* See if this channel exists */
  343. chan = BLI_findstring(&pose->chanbase, name, offsetof(bPoseChannel, name));
  344. if (chan) {
  345. return chan;
  346. }
  347. /* If not, create it and add it */
  348. chan = MEM_callocN(sizeof(bPoseChannel), "verifyPoseChannel");
  349. BLI_strncpy(chan->name, name, sizeof(chan->name));
  350. chan->custom_scale = 1.0f;
  351. /* init vars to prevent math errors */
  352. unit_qt(chan->quat);
  353. unit_axis_angle(chan->rotAxis, &chan->rotAngle);
  354. chan->size[0] = chan->size[1] = chan->size[2] = 1.0f;
  355. chan->scaleIn = chan->scaleOut = 1.0f;
  356. chan->limitmin[0] = chan->limitmin[1] = chan->limitmin[2] = -M_PI;
  357. chan->limitmax[0] = chan->limitmax[1] = chan->limitmax[2] = M_PI;
  358. chan->stiffness[0] = chan->stiffness[1] = chan->stiffness[2] = 0.0f;
  359. chan->ikrotweight = chan->iklinweight = 0.0f;
  360. unit_m4(chan->constinv);
  361. chan->protectflag = OB_LOCK_ROT4D; /* lock by components by default */
  362. BLI_addtail(&pose->chanbase, chan);
  363. BKE_pose_channels_hash_free(pose);
  364. return chan;
  365. }
  366. #ifndef NDEBUG
  367. bool BKE_pose_channels_is_valid(const bPose *pose)
  368. {
  369. if (pose->chanhash) {
  370. bPoseChannel *pchan;
  371. for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
  372. if (BLI_ghash_lookup(pose->chanhash, pchan->name) != pchan) {
  373. return false;
  374. }
  375. }
  376. }
  377. return true;
  378. }
  379. #endif
  380. /**
  381. * Find the active posechannel for an object (we can't just use pose, as layer info is in armature)
  382. *
  383. * \note: Object, not bPose is used here, as we need layer info from Armature)
  384. */
  385. bPoseChannel *BKE_pose_channel_active(Object *ob)
  386. {
  387. bArmature *arm = (ob) ? ob->data : NULL;
  388. bPoseChannel *pchan;
  389. if (ELEM(NULL, ob, ob->pose, arm)) {
  390. return NULL;
  391. }
  392. /* find active */
  393. for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
  394. if ((pchan->bone) && (pchan->bone == arm->act_bone) && (pchan->bone->layer & arm->layer))
  395. return pchan;
  396. }
  397. return NULL;
  398. }
  399. /**
  400. * \see #ED_armature_bone_get_mirrored (edit-mode, matching function)
  401. */
  402. bPoseChannel *BKE_pose_channel_get_mirrored(const bPose *pose, const char *name)
  403. {
  404. char name_flip[MAXBONENAME];
  405. BLI_string_flip_side_name(name_flip, name, false, sizeof(name_flip));
  406. if (!STREQ(name_flip, name)) {
  407. return BKE_pose_channel_find_name(pose, name_flip);
  408. }
  409. return NULL;
  410. }
  411. const char *BKE_pose_ikparam_get_name(bPose *pose)
  412. {
  413. if (pose) {
  414. switch (pose->iksolver) {
  415. case IKSOLVER_STANDARD:
  416. return NULL;
  417. case IKSOLVER_ITASC:
  418. return "bItasc";
  419. }
  420. }
  421. return NULL;
  422. }
  423. /**
  424. * Allocate a new pose on the heap, and copy the src pose and it's channels
  425. * into the new pose. *dst is set to the newly allocated structure, and assumed to be NULL.
  426. *
  427. * \param dst Should be freed already, makes entire duplicate.
  428. */
  429. void BKE_pose_copy_data(bPose **dst, const bPose *src, const bool copy_constraints)
  430. {
  431. bPose *outPose;
  432. bPoseChannel *pchan;
  433. ListBase listb;
  434. if (!src) {
  435. *dst = NULL;
  436. return;
  437. }
  438. outPose = MEM_callocN(sizeof(bPose), "pose");
  439. BLI_duplicatelist(&outPose->chanbase, &src->chanbase);
  440. /* Rebuild ghash here too, so that name lookups below won't be too bad...
  441. * BUT this will have the penalty that the ghash will be built twice
  442. * if BKE_pose_rebuild() gets called after this...
  443. */
  444. if (outPose->chanbase.first != outPose->chanbase.last) {
  445. outPose->chanhash = NULL;
  446. BKE_pose_channels_hash_make(outPose);
  447. }
  448. outPose->iksolver = src->iksolver;
  449. outPose->ikdata = NULL;
  450. outPose->ikparam = MEM_dupallocN(src->ikparam);
  451. outPose->avs = src->avs;
  452. for (pchan = outPose->chanbase.first; pchan; pchan = pchan->next) {
  453. if (pchan->custom) {
  454. id_us_plus(&pchan->custom->id);
  455. }
  456. /* warning, O(n2) here, if done without the hash, but these are rarely used features. */
  457. if (pchan->custom_tx) {
  458. pchan->custom_tx = BKE_pose_channel_find_name(outPose, pchan->custom_tx->name);
  459. }
  460. if (pchan->bbone_prev) {
  461. pchan->bbone_prev = BKE_pose_channel_find_name(outPose, pchan->bbone_prev->name);
  462. }
  463. if (pchan->bbone_next) {
  464. pchan->bbone_next = BKE_pose_channel_find_name(outPose, pchan->bbone_next->name);
  465. }
  466. if (copy_constraints) {
  467. BKE_constraints_copy(&listb, &pchan->constraints, true); // BKE_constraints_copy NULLs listb
  468. pchan->constraints = listb;
  469. pchan->mpath = NULL; /* motion paths should not get copied yet... */
  470. }
  471. if (pchan->prop) {
  472. pchan->prop = IDP_CopyProperty(pchan->prop);
  473. }
  474. }
  475. /* for now, duplicate Bone Groups too when doing this */
  476. if (copy_constraints) {
  477. BLI_duplicatelist(&outPose->agroups, &src->agroups);
  478. }
  479. *dst = outPose;
  480. }
  481. void BKE_pose_itasc_init(bItasc *itasc)
  482. {
  483. if (itasc) {
  484. itasc->iksolver = IKSOLVER_ITASC;
  485. itasc->minstep = 0.01f;
  486. itasc->maxstep = 0.06f;
  487. itasc->numiter = 100;
  488. itasc->numstep = 4;
  489. itasc->precision = 0.005f;
  490. itasc->flag = ITASC_AUTO_STEP | ITASC_INITIAL_REITERATION;
  491. itasc->feedback = 20.0f;
  492. itasc->maxvel = 50.0f;
  493. itasc->solver = ITASC_SOLVER_SDLS;
  494. itasc->dampmax = 0.5;
  495. itasc->dampeps = 0.15;
  496. }
  497. }
  498. void BKE_pose_ikparam_init(bPose *pose)
  499. {
  500. bItasc *itasc;
  501. switch (pose->iksolver) {
  502. case IKSOLVER_ITASC:
  503. itasc = MEM_callocN(sizeof(bItasc), "itasc");
  504. BKE_pose_itasc_init(itasc);
  505. pose->ikparam = itasc;
  506. break;
  507. case IKSOLVER_STANDARD:
  508. default:
  509. pose->ikparam = NULL;
  510. break;
  511. }
  512. }
  513. /* only for real IK, not for auto-IK */
  514. static bool pose_channel_in_IK_chain(Object *ob, bPoseChannel *pchan, int level)
  515. {
  516. bConstraint *con;
  517. Bone *bone;
  518. /* No need to check if constraint is active (has influence),
  519. * since all constraints with CONSTRAINT_IK_AUTO are active */
  520. for (con = pchan->constraints.first; con; con = con->next) {
  521. if (con->type == CONSTRAINT_TYPE_KINEMATIC) {
  522. bKinematicConstraint *data = con->data;
  523. if ((data->rootbone == 0) || (data->rootbone > level)) {
  524. if ((data->flag & CONSTRAINT_IK_AUTO) == 0)
  525. return true;
  526. }
  527. }
  528. }
  529. for (bone = pchan->bone->childbase.first; bone; bone = bone->next) {
  530. pchan = BKE_pose_channel_find_name(ob->pose, bone->name);
  531. if (pchan && pose_channel_in_IK_chain(ob, pchan, level + 1))
  532. return true;
  533. }
  534. return false;
  535. }
  536. bool BKE_pose_channel_in_IK_chain(Object *ob, bPoseChannel *pchan)
  537. {
  538. return pose_channel_in_IK_chain(ob, pchan, 0);
  539. }
  540. /**
  541. * Removes the hash for quick lookup of channels, must
  542. * be done when adding/removing channels.
  543. */
  544. void BKE_pose_channels_hash_make(bPose *pose)
  545. {
  546. if (!pose->chanhash) {
  547. bPoseChannel *pchan;
  548. pose->chanhash = BLI_ghash_str_new("make_pose_chan gh");
  549. for (pchan = pose->chanbase.first; pchan; pchan = pchan->next)
  550. BLI_ghash_insert(pose->chanhash, pchan->name, pchan);
  551. }
  552. }
  553. void BKE_pose_channels_hash_free(bPose *pose)
  554. {
  555. if (pose->chanhash) {
  556. BLI_ghash_free(pose->chanhash, NULL, NULL);
  557. pose->chanhash = NULL;
  558. }
  559. }
  560. /**
  561. * Selectively remove pose channels.
  562. */
  563. void BKE_pose_channels_remove(
  564. Object *ob,
  565. bool (*filter_fn)(const char *bone_name, void *user_data), void *user_data)
  566. {
  567. /* Erase any associated pose channel, along with any references to them */
  568. if (ob->pose) {
  569. bPoseChannel *pchan, *pchan_next;
  570. bConstraint *con;
  571. for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan_next) {
  572. pchan_next = pchan->next;
  573. if (filter_fn(pchan->name, user_data)) {
  574. /* Bone itself is being removed */
  575. BKE_pose_channel_free(pchan);
  576. if (ob->pose->chanhash) {
  577. BLI_ghash_remove(ob->pose->chanhash, pchan->name, NULL, NULL);
  578. }
  579. BLI_freelinkN(&ob->pose->chanbase, pchan);
  580. }
  581. else {
  582. /* Maybe something the bone references is being removed instead? */
  583. for (con = pchan->constraints.first; con; con = con->next) {
  584. const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con);
  585. ListBase targets = {NULL, NULL};
  586. bConstraintTarget *ct;
  587. if (cti && cti->get_constraint_targets) {
  588. cti->get_constraint_targets(con, &targets);
  589. for (ct = targets.first; ct; ct = ct->next) {
  590. if (ct->tar == ob) {
  591. if (ct->subtarget[0]) {
  592. if (filter_fn(ct->subtarget, user_data)) {
  593. con->flag |= CONSTRAINT_DISABLE;
  594. ct->subtarget[0] = 0;
  595. }
  596. }
  597. }
  598. }
  599. if (cti->flush_constraint_targets)
  600. cti->flush_constraint_targets(con, &targets, 0);
  601. }
  602. }
  603. if (pchan->bbone_prev) {
  604. if (filter_fn(pchan->bbone_prev->name, user_data))
  605. pchan->bbone_prev = NULL;
  606. }
  607. if (pchan->bbone_next) {
  608. if (filter_fn(pchan->bbone_next->name, user_data))
  609. pchan->bbone_next = NULL;
  610. }
  611. if (pchan->custom_tx) {
  612. if (filter_fn(pchan->custom_tx->name, user_data))
  613. pchan->custom_tx = NULL;
  614. }
  615. }
  616. }
  617. }
  618. }
  619. /**
  620. * Deallocates a pose channel.
  621. * Does not free the pose channel itself.
  622. */
  623. void BKE_pose_channel_free_ex(bPoseChannel *pchan, bool do_id_user)
  624. {
  625. if (pchan->custom) {
  626. if (do_id_user) {
  627. id_us_min(&pchan->custom->id);
  628. }
  629. pchan->custom = NULL;
  630. }
  631. if (pchan->mpath) {
  632. animviz_free_motionpath(pchan->mpath);
  633. pchan->mpath = NULL;
  634. }
  635. BKE_constraints_free_ex(&pchan->constraints, do_id_user);
  636. if (pchan->prop) {
  637. IDP_FreeProperty(pchan->prop);
  638. MEM_freeN(pchan->prop);
  639. }
  640. }
  641. void BKE_pose_channel_free(bPoseChannel *pchan)
  642. {
  643. BKE_pose_channel_free_ex(pchan, true);
  644. }
  645. /**
  646. * Removes and deallocates all channels from a pose.
  647. * Does not free the pose itself.
  648. */
  649. void BKE_pose_channels_free_ex(bPose *pose, bool do_id_user)
  650. {
  651. bPoseChannel *pchan;
  652. if (pose->chanbase.first) {
  653. for (pchan = pose->chanbase.first; pchan; pchan = pchan->next)
  654. BKE_pose_channel_free_ex(pchan, do_id_user);
  655. BLI_freelistN(&pose->chanbase);
  656. }
  657. BKE_pose_channels_hash_free(pose);
  658. }
  659. void BKE_pose_channels_free(bPose *pose)
  660. {
  661. BKE_pose_channels_free_ex(pose, true);
  662. }
  663. void BKE_pose_free_data_ex(bPose *pose, bool do_id_user)
  664. {
  665. /* free pose-channels */
  666. BKE_pose_channels_free_ex(pose, do_id_user);
  667. /* free pose-groups */
  668. if (pose->agroups.first)
  669. BLI_freelistN(&pose->agroups);
  670. /* free IK solver state */
  671. BIK_clear_data(pose);
  672. /* free IK solver param */
  673. if (pose->ikparam)
  674. MEM_freeN(pose->ikparam);
  675. }
  676. void BKE_pose_free_data(bPose *pose)
  677. {
  678. BKE_pose_free_data_ex(pose, true);
  679. }
  680. /**
  681. * Removes and deallocates all data from a pose, and also frees the pose.
  682. */
  683. void BKE_pose_free_ex(bPose *pose, bool do_id_user)
  684. {
  685. if (pose) {
  686. BKE_pose_free_data_ex(pose, do_id_user);
  687. /* free pose */
  688. MEM_freeN(pose);
  689. }
  690. }
  691. void BKE_pose_free(bPose *pose)
  692. {
  693. BKE_pose_free_ex(pose, true);
  694. }
  695. static void copy_pose_channel_data(bPoseChannel *pchan, const bPoseChannel *chan)
  696. {
  697. bConstraint *pcon, *con;
  698. copy_v3_v3(pchan->loc, chan->loc);
  699. copy_v3_v3(pchan->size, chan->size);
  700. copy_v3_v3(pchan->eul, chan->eul);
  701. copy_v3_v3(pchan->rotAxis, chan->rotAxis);
  702. pchan->rotAngle = chan->rotAngle;
  703. copy_qt_qt(pchan->quat, chan->quat);
  704. pchan->rotmode = chan->rotmode;
  705. copy_m4_m4(pchan->chan_mat, (float(*)[4])chan->chan_mat);
  706. copy_m4_m4(pchan->pose_mat, (float(*)[4])chan->pose_mat);
  707. pchan->flag = chan->flag;
  708. pchan->roll1 = chan->roll1;
  709. pchan->roll2 = chan->roll2;
  710. pchan->curveInX = chan->curveInX;
  711. pchan->curveInY = chan->curveInY;
  712. pchan->curveOutX = chan->curveOutX;
  713. pchan->curveOutY = chan->curveOutY;
  714. pchan->scaleIn = chan->scaleIn;
  715. pchan->scaleOut = chan->scaleOut;
  716. con = chan->constraints.first;
  717. for (pcon = pchan->constraints.first; pcon && con; pcon = pcon->next, con = con->next) {
  718. pcon->enforce = con->enforce;
  719. pcon->headtail = con->headtail;
  720. }
  721. }
  722. /**
  723. * Copy the internal members of each pose channel including constraints
  724. * and ID-Props, used when duplicating bones in editmode.
  725. * (unlike copy_pose_channel_data which only does posing-related stuff).
  726. *
  727. * \note use when copying bones in editmode (on returned value from #BKE_pose_channel_verify)
  728. */
  729. void BKE_pose_channel_copy_data(bPoseChannel *pchan, const bPoseChannel *pchan_from)
  730. {
  731. /* copy transform locks */
  732. pchan->protectflag = pchan_from->protectflag;
  733. /* copy rotation mode */
  734. pchan->rotmode = pchan_from->rotmode;
  735. /* copy bone group */
  736. pchan->agrp_index = pchan_from->agrp_index;
  737. /* ik (dof) settings */
  738. pchan->ikflag = pchan_from->ikflag;
  739. copy_v3_v3(pchan->limitmin, pchan_from->limitmin);
  740. copy_v3_v3(pchan->limitmax, pchan_from->limitmax);
  741. copy_v3_v3(pchan->stiffness, pchan_from->stiffness);
  742. pchan->ikstretch = pchan_from->ikstretch;
  743. pchan->ikrotweight = pchan_from->ikrotweight;
  744. pchan->iklinweight = pchan_from->iklinweight;
  745. /* bbone settings (typically not animated) */
  746. pchan->bboneflag = pchan_from->bboneflag;
  747. pchan->bbone_next = pchan_from->bbone_next;
  748. pchan->bbone_prev = pchan_from->bbone_prev;
  749. /* constraints */
  750. BKE_constraints_copy(&pchan->constraints, &pchan_from->constraints, true);
  751. /* id-properties */
  752. if (pchan->prop) {
  753. /* unlikely but possible it exists */
  754. IDP_FreeProperty(pchan->prop);
  755. MEM_freeN(pchan->prop);
  756. pchan->prop = NULL;
  757. }
  758. if (pchan_from->prop) {
  759. pchan->prop = IDP_CopyProperty(pchan_from->prop);
  760. }
  761. /* custom shape */
  762. pchan->custom = pchan_from->custom;
  763. if (pchan->custom) {
  764. id_us_plus(&pchan->custom->id);
  765. }
  766. pchan->custom_scale = pchan_from->custom_scale;
  767. }
  768. /* checks for IK constraint, Spline IK, and also for Follow-Path constraint.
  769. * can do more constraints flags later
  770. */
  771. /* pose should be entirely OK */
  772. void BKE_pose_update_constraint_flags(bPose *pose)
  773. {
  774. bPoseChannel *pchan, *parchan;
  775. bConstraint *con;
  776. /* clear */
  777. for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
  778. pchan->constflag = 0;
  779. }
  780. pose->flag &= ~POSE_CONSTRAINTS_TIMEDEPEND;
  781. /* detect */
  782. for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
  783. for (con = pchan->constraints.first; con; con = con->next) {
  784. if (con->type == CONSTRAINT_TYPE_KINEMATIC) {
  785. bKinematicConstraint *data = (bKinematicConstraint *)con->data;
  786. pchan->constflag |= PCHAN_HAS_IK;
  787. if (data->tar == NULL || (data->tar->type == OB_ARMATURE && data->subtarget[0] == 0))
  788. pchan->constflag |= PCHAN_HAS_TARGET;
  789. /* negative rootbone = recalc rootbone index. used in do_versions */
  790. if (data->rootbone < 0) {
  791. data->rootbone = 0;
  792. if (data->flag & CONSTRAINT_IK_TIP) parchan = pchan;
  793. else parchan = pchan->parent;
  794. while (parchan) {
  795. data->rootbone++;
  796. if ((parchan->bone->flag & BONE_CONNECTED) == 0)
  797. break;
  798. parchan = parchan->parent;
  799. }
  800. }
  801. }
  802. else if (con->type == CONSTRAINT_TYPE_FOLLOWPATH) {
  803. bFollowPathConstraint *data = (bFollowPathConstraint *)con->data;
  804. /* for drawing constraint colors when color set allows this */
  805. pchan->constflag |= PCHAN_HAS_CONST;
  806. /* if we have a valid target, make sure that this will get updated on frame-change
  807. * (needed for when there is no anim-data for this pose)
  808. */
  809. if ((data->tar) && (data->tar->type == OB_CURVE))
  810. pose->flag |= POSE_CONSTRAINTS_TIMEDEPEND;
  811. }
  812. else if (con->type == CONSTRAINT_TYPE_SPLINEIK)
  813. pchan->constflag |= PCHAN_HAS_SPLINEIK;
  814. else
  815. pchan->constflag |= PCHAN_HAS_CONST;
  816. }
  817. }
  818. pose->flag &= ~POSE_CONSTRAINTS_NEED_UPDATE_FLAGS;
  819. }
  820. void BKE_pose_tag_update_constraint_flags(bPose *pose)
  821. {
  822. pose->flag |= POSE_CONSTRAINTS_NEED_UPDATE_FLAGS;
  823. }
  824. /* Clears all BONE_UNKEYED flags for every pose channel in every pose
  825. * This should only be called on frame changing, when it is acceptable to
  826. * do this. Otherwise, these flags should not get cleared as poses may get lost.
  827. */
  828. void framechange_poses_clear_unkeyed(void)
  829. {
  830. Object *ob;
  831. bPose *pose;
  832. bPoseChannel *pchan;
  833. /* This needs to be done for each object that has a pose */
  834. /* TODO: proxies may/may not be correctly handled here... (this needs checking) */
  835. for (ob = G.main->object.first; ob; ob = ob->id.next) {
  836. /* we only need to do this on objects with a pose */
  837. if ((pose = ob->pose)) {
  838. for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
  839. if (pchan->bone)
  840. pchan->bone->flag &= ~BONE_UNKEYED;
  841. }
  842. }
  843. }
  844. }
  845. /* ************************** Bone Groups ************************** */
  846. /* Adds a new bone-group (name may be NULL) */
  847. bActionGroup *BKE_pose_add_group(bPose *pose, const char *name)
  848. {
  849. bActionGroup *grp;
  850. if (!name) {
  851. name = DATA_("Group");
  852. }
  853. grp = MEM_callocN(sizeof(bActionGroup), "PoseGroup");
  854. BLI_strncpy(grp->name, name, sizeof(grp->name));
  855. BLI_addtail(&pose->agroups, grp);
  856. BLI_uniquename(&pose->agroups, grp, name, '.', offsetof(bActionGroup, name), sizeof(grp->name));
  857. pose->active_group = BLI_listbase_count(&pose->agroups);
  858. return grp;
  859. }
  860. /* Remove the given bone-group (expects 'virtual' index (+1 one, used by active_group etc.))
  861. * index might be invalid ( < 1), in which case it will be find from grp. */
  862. void BKE_pose_remove_group(bPose *pose, bActionGroup *grp, const int index)
  863. {
  864. bPoseChannel *pchan;
  865. int idx = index;
  866. if (idx < 1) {
  867. idx = BLI_findindex(&pose->agroups, grp) + 1;
  868. }
  869. BLI_assert(idx > 0);
  870. /* adjust group references (the trouble of using indices!):
  871. * - firstly, make sure nothing references it
  872. * - also, make sure that those after this item get corrected
  873. */
  874. for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
  875. if (pchan->agrp_index == idx)
  876. pchan->agrp_index = 0;
  877. else if (pchan->agrp_index > idx)
  878. pchan->agrp_index--;
  879. }
  880. /* now, remove it from the pose */
  881. BLI_freelinkN(&pose->agroups, grp);
  882. if (pose->active_group >= idx) {
  883. const bool has_groups = !BLI_listbase_is_empty(&pose->agroups);
  884. pose->active_group--;
  885. if (pose->active_group == 0 && has_groups) {
  886. pose->active_group = 1;
  887. }
  888. else if (pose->active_group < 0 || !has_groups) {
  889. pose->active_group = 0;
  890. }
  891. }
  892. }
  893. /* Remove the indexed bone-group (expects 'virtual' index (+1 one, used by active_group etc.)) */
  894. void BKE_pose_remove_group_index(bPose *pose, const int index)
  895. {
  896. bActionGroup *grp = NULL;
  897. /* get group to remove */
  898. grp = BLI_findlink(&pose->agroups, index - 1);
  899. if (grp) {
  900. BKE_pose_remove_group(pose, grp, index);
  901. }
  902. }
  903. /* ************** F-Curve Utilities for Actions ****************** */
  904. /* Check if the given action has any keyframes */
  905. bool action_has_motion(const bAction *act)
  906. {
  907. FCurve *fcu;
  908. /* return on the first F-Curve that has some keyframes/samples defined */
  909. if (act) {
  910. for (fcu = act->curves.first; fcu; fcu = fcu->next) {
  911. if (fcu->totvert)
  912. return true;
  913. }
  914. }
  915. /* nothing found */
  916. return false;
  917. }
  918. /* Calculate the extents of given action */
  919. void calc_action_range(const bAction *act, float *start, float *end, short incl_modifiers)
  920. {
  921. FCurve *fcu;
  922. float min = 999999999.0f, max = -999999999.0f;
  923. short foundvert = 0, foundmod = 0;
  924. if (act) {
  925. for (fcu = act->curves.first; fcu; fcu = fcu->next) {
  926. /* if curve has keyframes, consider them first */
  927. if (fcu->totvert) {
  928. float nmin, nmax;
  929. /* get extents for this curve
  930. * - no "selected only", since this is often used in the backend
  931. * - no "minimum length" (we will apply this later), otherwise
  932. * single-keyframe curves will increase the overall length by
  933. * a phantom frame (T50354)
  934. */
  935. calc_fcurve_range(fcu, &nmin, &nmax, false, false);
  936. /* compare to the running tally */
  937. min = min_ff(min, nmin);
  938. max = max_ff(max, nmax);
  939. foundvert = 1;
  940. }
  941. /* if incl_modifiers is enabled, need to consider modifiers too
  942. * - only really care about the last modifier
  943. */
  944. if ((incl_modifiers) && (fcu->modifiers.last)) {
  945. FModifier *fcm = fcu->modifiers.last;
  946. /* only use the maximum sensible limits of the modifiers if they are more extreme */
  947. switch (fcm->type) {
  948. case FMODIFIER_TYPE_LIMITS: /* Limits F-Modifier */
  949. {
  950. FMod_Limits *fmd = (FMod_Limits *)fcm->data;
  951. if (fmd->flag & FCM_LIMIT_XMIN) {
  952. min = min_ff(min, fmd->rect.xmin);
  953. }
  954. if (fmd->flag & FCM_LIMIT_XMAX) {
  955. max = max_ff(max, fmd->rect.xmax);
  956. }
  957. break;
  958. }
  959. case FMODIFIER_TYPE_CYCLES: /* Cycles F-Modifier */
  960. {
  961. FMod_Cycles *fmd = (FMod_Cycles *)fcm->data;
  962. if (fmd->before_mode != FCM_EXTRAPOLATE_NONE)
  963. min = MINAFRAMEF;
  964. if (fmd->after_mode != FCM_EXTRAPOLATE_NONE)
  965. max = MAXFRAMEF;
  966. break;
  967. }
  968. /* TODO: function modifier may need some special limits */
  969. default: /* all other standard modifiers are on the infinite range... */
  970. min = MINAFRAMEF;
  971. max = MAXFRAMEF;
  972. break;
  973. }
  974. foundmod = 1;
  975. }
  976. }
  977. }
  978. if (foundvert || foundmod) {
  979. /* ensure that action is at least 1 frame long (for NLA strips to have a valid length) */
  980. if (min == max) max += 1.0f;
  981. *start = min;
  982. *end = max;
  983. }
  984. else {
  985. *start = 0.0f;
  986. *end = 1.0f;
  987. }
  988. }
  989. /* Return flags indicating which transforms the given object/posechannel has
  990. * - if 'curves' is provided, a list of links to these curves are also returned
  991. */
  992. short action_get_item_transforms(bAction *act, Object *ob, bPoseChannel *pchan, ListBase *curves)
  993. {
  994. PointerRNA ptr;
  995. FCurve *fcu;
  996. char *basePath = NULL;
  997. short flags = 0;
  998. /* build PointerRNA from provided data to obtain the paths to use */
  999. if (pchan)
  1000. RNA_pointer_create((ID *)ob, &RNA_PoseBone, pchan, &ptr);
  1001. else if (ob)
  1002. RNA_id_pointer_create((ID *)ob, &ptr);
  1003. else
  1004. return 0;
  1005. /* get the basic path to the properties of interest */
  1006. basePath = RNA_path_from_ID_to_struct(&ptr);
  1007. if (basePath == NULL)
  1008. return 0;
  1009. /* search F-Curves for the given properties
  1010. * - we cannot use the groups, since they may not be grouped in that way...
  1011. */
  1012. for (fcu = act->curves.first; fcu; fcu = fcu->next) {
  1013. const char *bPtr = NULL, *pPtr = NULL;
  1014. /* if enough flags have been found, we can stop checking unless we're also getting the curves */
  1015. if ((flags == ACT_TRANS_ALL) && (curves == NULL))
  1016. break;
  1017. /* just in case... */
  1018. if (fcu->rna_path == NULL)
  1019. continue;
  1020. /* step 1: check for matching base path */
  1021. bPtr = strstr(fcu->rna_path, basePath);
  1022. if (bPtr) {
  1023. /* we must add len(basePath) bytes to the match so that we are at the end of the
  1024. * base path so that we don't get false positives with these strings in the names
  1025. */
  1026. bPtr += strlen(basePath);
  1027. /* step 2: check for some property with transforms
  1028. * - to speed things up, only check for the ones not yet found
  1029. * unless we're getting the curves too
  1030. * - if we're getting the curves, the BLI_genericNodeN() creates a LinkData
  1031. * node wrapping the F-Curve, which then gets added to the list
  1032. * - once a match has been found, the curve cannot possibly be any other one
  1033. */
  1034. if ((curves) || (flags & ACT_TRANS_LOC) == 0) {
  1035. pPtr = strstr(bPtr, "location");
  1036. if (pPtr) {
  1037. flags |= ACT_TRANS_LOC;
  1038. if (curves)
  1039. BLI_addtail(curves, BLI_genericNodeN(fcu));
  1040. continue;
  1041. }
  1042. }
  1043. if ((curves) || (flags & ACT_TRANS_SCALE) == 0) {
  1044. pPtr = strstr(bPtr, "scale");
  1045. if (pPtr) {
  1046. flags |= ACT_TRANS_SCALE;
  1047. if (curves)
  1048. BLI_addtail(curves, BLI_genericNodeN(fcu));
  1049. continue;
  1050. }
  1051. }
  1052. if ((curves) || (flags & ACT_TRANS_ROT) == 0) {
  1053. pPtr = strstr(bPtr, "rotation");
  1054. if (pPtr) {
  1055. flags |= ACT_TRANS_ROT;
  1056. if (curves)
  1057. BLI_addtail(curves, BLI_genericNodeN(fcu));
  1058. continue;
  1059. }
  1060. }
  1061. if ((curves) || (flags & ACT_TRANS_BBONE) == 0) {
  1062. /* bbone shape properties */
  1063. pPtr = strstr(bPtr, "bbone_");
  1064. if (pPtr) {
  1065. flags |= ACT_TRANS_BBONE;
  1066. if (curves)
  1067. BLI_addtail(curves, BLI_genericNodeN(fcu));
  1068. continue;
  1069. }
  1070. }
  1071. if ((curves) || (flags & ACT_TRANS_PROP) == 0) {
  1072. /* custom properties only */
  1073. pPtr = strstr(bPtr, "[\""); /* extra '"' comment here to keep my texteditor functionlist working :) */
  1074. if (pPtr) {
  1075. flags |= ACT_TRANS_PROP;
  1076. if (curves)
  1077. BLI_addtail(curves, BLI_genericNodeN(fcu));
  1078. continue;
  1079. }
  1080. }
  1081. }
  1082. }
  1083. /* free basePath */
  1084. MEM_freeN(basePath);
  1085. /* return flags found */
  1086. return flags;
  1087. }
  1088. /* ************** Pose Management Tools ****************** */
  1089. /* Copy the data from the action-pose (src) into the pose */
  1090. /* both args are assumed to be valid */
  1091. /* exported to game engine */
  1092. /* Note! this assumes both poses are aligned, this isn't always true when dealing with user poses */
  1093. void extract_pose_from_pose(bPose *pose, const bPose *src)
  1094. {
  1095. const bPoseChannel *schan;
  1096. bPoseChannel *pchan = pose->chanbase.first;
  1097. if (pose == src) {
  1098. printf("extract_pose_from_pose source and target are the same\n");
  1099. return;
  1100. }
  1101. for (schan = src->chanbase.first; (schan && pchan); schan = schan->next, pchan = pchan->next) {
  1102. copy_pose_channel_data(pchan, schan);
  1103. }
  1104. }
  1105. /* for do_all_pose_actions, clears the pose. Now also exported for proxy and tools */
  1106. void BKE_pose_rest(bPose *pose)
  1107. {
  1108. bPoseChannel *pchan;
  1109. if (!pose)
  1110. return;
  1111. memset(pose->stride_offset, 0, sizeof(pose->stride_offset));
  1112. memset(pose->cyclic_offset, 0, sizeof(pose->cyclic_offset));
  1113. for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
  1114. zero_v3(pchan->loc);
  1115. zero_v3(pchan->eul);
  1116. unit_qt(pchan->quat);
  1117. unit_axis_angle(pchan->rotAxis, &pchan->rotAngle);
  1118. pchan->size[0] = pchan->size[1] = pchan->size[2] = 1.0f;
  1119. pchan->roll1 = pchan->roll2 = 0.0f;
  1120. pchan->curveInX = pchan->curveInY = 0.0f;
  1121. pchan->curveOutX = pchan->curveOutY = 0.0f;
  1122. pchan->scaleIn = pchan->scaleOut = 1.0f;
  1123. pchan->flag &= ~(POSE_LOC | POSE_ROT | POSE_SIZE | POSE_BBONE_SHAPE);
  1124. }
  1125. }
  1126. /* both poses should be in sync */
  1127. bool BKE_pose_copy_result(bPose *to, bPose *from)
  1128. {
  1129. bPoseChannel *pchanto, *pchanfrom;
  1130. if (to == NULL || from == NULL) {
  1131. printf("Pose copy error, pose to:%p from:%p\n", (void *)to, (void *)from); /* debug temp */
  1132. return false;
  1133. }
  1134. if (to == from) {
  1135. printf("BKE_pose_copy_result source and target are the same\n");
  1136. return false;
  1137. }
  1138. for (pchanfrom = from->chanbase.first; pchanfrom; pchanfrom = pchanfrom->next) {
  1139. pchanto = BKE_pose_channel_find_name(to, pchanfrom->name);
  1140. if (pchanto) {
  1141. copy_m4_m4(pchanto->pose_mat, pchanfrom->pose_mat);
  1142. copy_m4_m4(pchanto->chan_mat, pchanfrom->chan_mat);
  1143. /* used for local constraints */
  1144. copy_v3_v3(pchanto->loc, pchanfrom->loc);
  1145. copy_qt_qt(pchanto->quat, pchanfrom->quat);
  1146. copy_v3_v3(pchanto->eul, pchanfrom->eul);
  1147. copy_v3_v3(pchanto->size, pchanfrom->size);
  1148. copy_v3_v3(pchanto->pose_head, pchanfrom->pose_head);
  1149. copy_v3_v3(pchanto->pose_tail, pchanfrom->pose_tail);
  1150. pchanto->roll1 = pchanfrom->roll1;
  1151. pchanto->roll2 = pchanfrom->roll2;
  1152. pchanto->curveInX = pchanfrom->curveInX;
  1153. pchanto->curveInY = pchanfrom->curveInY;
  1154. pchanto->curveOutX = pchanfrom->curveOutX;
  1155. pchanto->curveOutY = pchanfrom->curveOutY;
  1156. pchanto->scaleIn = pchanfrom->scaleIn;
  1157. pchanto->scaleOut = pchanfrom->scaleOut;
  1158. pchanto->rotmode = pchanfrom->rotmode;
  1159. pchanto->flag = pchanfrom->flag;
  1160. pchanto->protectflag = pchanfrom->protectflag;
  1161. pchanto->bboneflag = pchanfrom->bboneflag;
  1162. }
  1163. }
  1164. return true;
  1165. }
  1166. /* Tag pose for recalc. Also tag all related data to be recalc. */
  1167. void BKE_pose_tag_recalc(Main *bmain, bPose *pose)
  1168. {
  1169. pose->flag |= POSE_RECALC;
  1170. /* Depsgraph components depends on actual pose state,
  1171. * if pose was changed depsgraph is to be updated as well.
  1172. */
  1173. DAG_relations_tag_update(bmain);
  1174. }
  1175. /* For the calculation of the effects of an Action at the given frame on an object
  1176. * This is currently only used for the Action Constraint
  1177. */
  1178. void what_does_obaction(Object *ob, Object *workob, bPose *pose, bAction *act, char groupname[], float cframe)
  1179. {
  1180. bActionGroup *agrp = BKE_action_group_find_name(act, groupname);
  1181. /* clear workob */
  1182. BKE_object_workob_clear(workob);
  1183. /* init workob */
  1184. copy_m4_m4(workob->obmat, ob->obmat);
  1185. copy_m4_m4(workob->parentinv, ob->parentinv);
  1186. copy_m4_m4(workob->constinv, ob->constinv);
  1187. workob->parent = ob->parent;
  1188. workob->rotmode = ob->rotmode;
  1189. workob->trackflag = ob->trackflag;
  1190. workob->upflag = ob->upflag;
  1191. workob->partype = ob->partype;
  1192. workob->par1 = ob->par1;
  1193. workob->par2 = ob->par2;
  1194. workob->par3 = ob->par3;
  1195. workob->constraints.first = ob->constraints.first;
  1196. workob->constraints.last = ob->constraints.last;
  1197. workob->pose = pose; /* need to set pose too, since this is used for both types of Action Constraint */
  1198. if (pose) {
  1199. /* This function is most likely to be used with a temporary pose with a single bone in there.
  1200. * For such cases it makes no sense to create hash since it'll only waste CPU ticks on memory
  1201. * allocation and also will make lookup slower.
  1202. */
  1203. if (pose->chanbase.first != pose->chanbase.last) {
  1204. BKE_pose_channels_hash_make(pose);
  1205. }
  1206. if (pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
  1207. BKE_pose_update_constraint_flags(pose);
  1208. }
  1209. }
  1210. BLI_strncpy(workob->parsubstr, ob->parsubstr, sizeof(workob->parsubstr));
  1211. BLI_strncpy(workob->id.name, "OB<ConstrWorkOb>", sizeof(workob->id.name)); /* we don't use real object name, otherwise RNA screws with the real thing */
  1212. /* if we're given a group to use, it's likely to be more efficient (though a bit more dangerous) */
  1213. if (agrp) {
  1214. /* specifically evaluate this group only */
  1215. PointerRNA id_ptr;
  1216. /* get RNA-pointer for the workob's ID */
  1217. RNA_id_pointer_create(&workob->id, &id_ptr);
  1218. /* execute action for this group only */
  1219. animsys_evaluate_action_group(&id_ptr, act, agrp, NULL, cframe);
  1220. }
  1221. else {
  1222. AnimData adt = {NULL};
  1223. /* init animdata, and attach to workob */
  1224. workob->adt = &adt;
  1225. adt.recalc = ADT_RECALC_ANIM;
  1226. adt.action = act;
  1227. /* execute effects of Action on to workob (or it's PoseChannels) */
  1228. BKE_animsys_evaluate_animdata(NULL, &workob->id, &adt, cframe, ADT_RECALC_ANIM);
  1229. }
  1230. }