1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
|
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Nathan Letwory.
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/collada/collada_utils.cpp
* \ingroup collada
*/
/* COLLADABU_ASSERT, may be able to remove later */
#include "COLLADABUPlatform.h"
#include "COLLADAFWGeometry.h"
#include "COLLADAFWMeshPrimitive.h"
#include "COLLADAFWMeshVertexData.h"
#include <set>
extern "C" {
#include "DNA_modifier_types.h"
#include "DNA_customdata_types.h"
#include "DNA_object_types.h"
#include "DNA_mesh_types.h"
#include "DNA_scene_types.h"
#include "DNA_armature_types.h"
#include "BLI_math.h"
#include "BLI_linklist.h"
#include "BKE_context.h"
#include "BKE_customdata.h"
#include "BKE_object.h"
#include "BKE_global.h"
#include "BKE_layer.h"
#include "BKE_mesh.h"
#include "BKE_scene.h"
#include "BKE_DerivedMesh.h"
#include "BKE_main.h"
#include "ED_armature.h"
#include "WM_api.h" // XXX hrm, see if we can do without this
#include "WM_types.h"
#include "bmesh.h"
#include "bmesh_tools.h"
}
#include "DEG_depsgraph.h"
#include "DEG_depsgraph_query.h"
#include "collada_utils.h"
#include "ExportSettings.h"
float bc_get_float_value(const COLLADAFW::FloatOrDoubleArray& array, unsigned int index)
{
if (index >= array.getValuesCount())
return 0.0f;
if (array.getType() == COLLADAFW::MeshVertexData::DATA_TYPE_FLOAT)
return array.getFloatValues()->getData()[index];
else
return array.getDoubleValues()->getData()[index];
}
// copied from /editors/object/object_relations.c
int bc_test_parent_loop(Object *par, Object *ob)
{
/* test if 'ob' is a parent somewhere in par's parents */
if (par == NULL) return 0;
if (ob == par) return 1;
return bc_test_parent_loop(par->parent, ob);
}
// a shortened version of parent_set_exec()
// if is_parent_space is true then ob->obmat will be multiplied by par->obmat before parenting
int bc_set_parent(Object *ob, Object *par, bContext *C, bool is_parent_space)
{
Object workob;
Depsgraph *depsgraph = CTX_data_depsgraph(C);
Scene *sce = CTX_data_scene(C);
if (!par || bc_test_parent_loop(par, ob))
return false;
ob->parent = par;
ob->partype = PAROBJECT;
ob->parsubstr[0] = 0;
if (is_parent_space) {
float mat[4][4];
// calc par->obmat
BKE_object_where_is_calc(depsgraph, sce, par);
// move child obmat into world space
mul_m4_m4m4(mat, par->obmat, ob->obmat);
copy_m4_m4(ob->obmat, mat);
}
// apply child obmat (i.e. decompose it into rot/loc/size)
BKE_object_apply_mat4(ob, ob->obmat, 0, 0);
// compute parentinv
BKE_object_workob_calc_parent(depsgraph, sce, ob, &workob);
invert_m4_m4(ob->parentinv, workob.obmat);
DEG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA);
DEG_id_tag_update(&par->id, OB_RECALC_OB);
return true;
}
Scene *bc_get_scene(bContext *C)
{
return CTX_data_scene(C);
}
void bc_update_scene(Main *bmain, Depsgraph *depsgraph, Scene *scene, float ctime)
{
BKE_scene_frame_set(scene, ctime);
BKE_scene_graph_update_for_newframe(depsgraph, bmain);
}
Object *bc_add_object(Main *bmain, Scene *scene, ViewLayer *view_layer, int type, const char *name)
{
Object *ob = BKE_object_add_only_object(bmain, type, name);
ob->data = BKE_object_obdata_add_from_type(bmain, type, name);
ob->lay = scene->lay;
DEG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
LayerCollection *layer_collection = BKE_layer_collection_get_active(view_layer);
BKE_collection_object_add(bmain, layer_collection->collection, ob);
Base *base = BKE_view_layer_base_find(view_layer, ob);
BKE_view_layer_base_select(view_layer, base);
return ob;
}
Mesh *bc_get_mesh_copy(
Main *bmain, Depsgraph *depsgraph, Scene *scene, Object *ob, BC_export_mesh_type export_mesh_type, bool apply_modifiers, bool triangulate)
{
Mesh *tmpmesh;
CustomDataMask mask = CD_MASK_MESH;
Mesh *mesh = (Mesh *)ob->data;
DerivedMesh *dm = NULL;
if (apply_modifiers) {
switch (export_mesh_type) {
case BC_MESH_TYPE_VIEW:
{
dm = mesh_create_derived_view(depsgraph, scene, ob, mask);
break;
}
case BC_MESH_TYPE_RENDER:
{
dm = mesh_create_derived_render(depsgraph, scene, ob, mask);
break;
}
}
}
else {
dm = mesh_create_derived((Mesh *)ob->data, NULL);
}
tmpmesh = BKE_mesh_add(bmain, "ColladaMesh"); // name is not important here
DM_to_mesh(dm, tmpmesh, ob, CD_MASK_MESH, true);
tmpmesh->flag = mesh->flag;
if (triangulate) {
bc_triangulate_mesh(tmpmesh);
}
BKE_mesh_tessface_ensure(tmpmesh);
return tmpmesh;
}
Object *bc_get_assigned_armature(Object *ob)
{
Object *ob_arm = NULL;
if (ob->parent && ob->partype == PARSKEL && ob->parent->type == OB_ARMATURE) {
ob_arm = ob->parent;
}
else {
ModifierData *mod;
for (mod = (ModifierData *)ob->modifiers.first; mod; mod = mod->next) {
if (mod->type == eModifierType_Armature) {
ob_arm = ((ArmatureModifierData *)mod)->object;
}
}
}
return ob_arm;
}
// Returns the highest selected ancestor
// returns NULL if no ancestor is selected
// IMPORTANT: This function expects that
// all exported objects have set:
// ob->id.tag & LIB_TAG_DOIT
Object *bc_get_highest_selected_ancestor_or_self(LinkNode *export_set, Object *ob)
{
Object *ancestor = ob;
while (ob->parent && bc_is_marked(ob->parent)) {
ob = ob->parent;
ancestor = ob;
}
return ancestor;
}
bool bc_is_base_node(LinkNode *export_set, Object *ob)
{
Object *root = bc_get_highest_selected_ancestor_or_self(export_set, ob);
return (root == ob);
}
bool bc_is_in_Export_set(LinkNode *export_set, Object *ob)
{
return (BLI_linklist_index(export_set, ob) != -1);
}
bool bc_has_object_type(LinkNode *export_set, short obtype)
{
LinkNode *node;
for (node = export_set; node; node = node->next) {
Object *ob = (Object *)node->link;
/* XXX - why is this checking for ob->data? - we could be looking for empties */
if (ob->type == obtype && ob->data) {
return true;
}
}
return false;
}
int bc_is_marked(Object *ob)
{
return ob && (ob->id.tag & LIB_TAG_DOIT);
}
void bc_remove_mark(Object *ob)
{
ob->id.tag &= ~LIB_TAG_DOIT;
}
void bc_set_mark(Object *ob)
{
ob->id.tag |= LIB_TAG_DOIT;
}
// Use bubble sort algorithm for sorting the export set
void bc_bubble_sort_by_Object_name(LinkNode *export_set)
{
bool sorted = false;
LinkNode *node;
for (node = export_set; node->next && !sorted; node = node->next) {
sorted = true;
LinkNode *current;
for (current = export_set; current->next; current = current->next) {
Object *a = (Object *)current->link;
Object *b = (Object *)current->next->link;
if (strcmp(a->id.name, b->id.name) > 0) {
current->link = b;
current->next->link = a;
sorted = false;
}
}
}
}
/* Check if a bone is the top most exportable bone in the bone hierarchy.
* When deform_bones_only == false, then only bones with NO parent
* can be root bones. Otherwise the top most deform bones in the hierarchy
* are root bones.
*/
bool bc_is_root_bone(Bone *aBone, bool deform_bones_only)
{
if (deform_bones_only) {
Bone *root = NULL;
Bone *bone = aBone;
while (bone) {
if (!(bone->flag & BONE_NO_DEFORM))
root = bone;
bone = bone->parent;
}
return (aBone == root);
}
else
return !(aBone->parent);
}
int bc_get_active_UVLayer(Object *ob)
{
Mesh *me = (Mesh *)ob->data;
return CustomData_get_active_layer_index(&me->fdata, CD_MTFACE);
}
std::string bc_url_encode(std::string data)
{
/* XXX We probably do not need to do a full encoding.
* But in case that is necessary,then it can be added here.
*/
return bc_replace_string(data,"#", "%23");
}
std::string bc_replace_string(std::string data, const std::string& pattern,
const std::string& replacement)
{
size_t pos = 0;
while ((pos = data.find(pattern, pos)) != std::string::npos) {
data.replace(pos, pattern.length(), replacement);
pos += replacement.length();
}
return data;
}
/**
* Calculate a rescale factor such that the imported scene's scale
* is preserved. I.e. 1 meter in the import will also be
* 1 meter in the current scene.
*/
void bc_match_scale(Object *ob, UnitConverter &bc_unit, bool scale_to_scene)
{
if (scale_to_scene) {
mul_m4_m4m4(ob->obmat, bc_unit.get_scale(), ob->obmat);
}
mul_m4_m4m4(ob->obmat, bc_unit.get_rotation(), ob->obmat);
BKE_object_apply_mat4(ob, ob->obmat, 0, 0);
}
void bc_match_scale(std::vector<Object *> *objects_done,
UnitConverter &bc_unit,
bool scale_to_scene)
{
for (std::vector<Object *>::iterator it = objects_done->begin();
it != objects_done->end();
++it)
{
Object *ob = *it;
if (ob -> parent == NULL) {
bc_match_scale(*it, bc_unit, scale_to_scene);
}
}
}
/*
Convenience function to get only the needed components of a matrix
*/
void bc_decompose(float mat[4][4], float *loc, float eul[3], float quat[4], float *size)
{
if (size) {
mat4_to_size(size, mat);
}
if (eul) {
mat4_to_eul(eul, mat);
}
if (quat) {
mat4_to_quat(quat, mat);
}
if (loc) {
copy_v3_v3(loc, mat[3]);
}
}
/*
* Create rotation_quaternion from a delta rotation and a reference quat
*
* Input:
* mat_from: The rotation matrix before rotation
* mat_to : The rotation matrix after rotation
* qref : the quat corresponding to mat_from
*
* Output:
* rot : the calculated result (quaternion)
*
*/
void bc_rotate_from_reference_quat(float quat_to[4], float quat_from[4], float mat_to[4][4])
{
float qd[4];
float matd[4][4];
float mati[4][4];
float mat_from[4][4];
quat_to_mat4(mat_from, quat_from);
// Calculate the difference matrix matd between mat_from and mat_to
invert_m4_m4(mati, mat_from);
mul_m4_m4m4(matd, mati, mat_to);
mat4_to_quat(qd, matd);
mul_qt_qtqt(quat_to, qd, quat_from); // rot is the final rotation corresponding to mat_to
}
void bc_triangulate_mesh(Mesh *me)
{
bool use_beauty = false;
bool tag_only = false;
int quad_method = MOD_TRIANGULATE_QUAD_SHORTEDGE; /* XXX: The triangulation method selection could be offered in the UI */
const struct BMeshCreateParams bm_create_params = {0};
BMesh *bm = BM_mesh_create(
&bm_mesh_allocsize_default,
&bm_create_params);
BMeshFromMeshParams bm_from_me_params = {0};
bm_from_me_params.calc_face_normal = true;
BM_mesh_bm_from_me(bm, me, &bm_from_me_params);
BM_mesh_triangulate(bm, quad_method, use_beauty, tag_only, NULL, NULL, NULL);
BMeshToMeshParams bm_to_me_params = {0};
bm_to_me_params.calc_object_remap = false;
BM_mesh_bm_to_me(NULL, bm, me, &bm_to_me_params);
BM_mesh_free(bm);
}
/*
* A bone is a leaf when it has no children or all children are not connected.
*/
bool bc_is_leaf_bone(Bone *bone)
{
for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
if (child->flag & BONE_CONNECTED)
return false;
}
return true;
}
EditBone *bc_get_edit_bone(bArmature * armature, char *name) {
EditBone *eBone;
for (eBone = (EditBone *)armature->edbo->first; eBone; eBone = eBone->next) {
if (STREQ(name, eBone->name))
return eBone;
}
return NULL;
}
int bc_set_layer(int bitfield, int layer)
{
return bc_set_layer(bitfield, layer, true); /* enable */
}
int bc_set_layer(int bitfield, int layer, bool enable)
{
int bit = 1u << layer;
if (enable)
bitfield |= bit;
else
bitfield &= ~bit;
return bitfield;
}
/*
| This method creates a new extension map when needed.
| Note: The ~BoneExtensionManager destructor takes care
| to delete the created maps when the manager is removed.
*/
BoneExtensionMap &BoneExtensionManager::getExtensionMap(bArmature *armature)
{
std::string key = armature->id.name;
BoneExtensionMap *result = extended_bone_maps[key];
if (result == NULL)
{
result = new BoneExtensionMap();
extended_bone_maps[key] = result;
}
return *result;
}
BoneExtensionManager::~BoneExtensionManager()
{
std::map<std::string, BoneExtensionMap *>::iterator map_it;
for (map_it = extended_bone_maps.begin(); map_it != extended_bone_maps.end(); ++map_it)
{
BoneExtensionMap *extended_bones = map_it->second;
for (BoneExtensionMap::iterator ext_it = extended_bones->begin(); ext_it != extended_bones->end(); ++ext_it) {
if (ext_it->second != NULL)
delete ext_it->second;
}
extended_bones->clear();
delete extended_bones;
}
}
/**
* BoneExtended is a helper class needed for the Bone chain finder
* See ArmatureImporter::fix_leaf_bones()
* and ArmatureImporter::connect_bone_chains()
*/
BoneExtended::BoneExtended(EditBone *aBone)
{
this->set_name(aBone->name);
this->chain_length = 0;
this->is_leaf = false;
this->tail[0] = 0.0f;
this->tail[1] = 0.5f;
this->tail[2] = 0.0f;
this->use_connect = -1;
this->roll = 0;
this->bone_layers = 0;
this->has_custom_tail = false;
this->has_custom_roll = false;
}
char *BoneExtended::get_name()
{
return name;
}
void BoneExtended::set_name(char *aName)
{
BLI_strncpy(name, aName, MAXBONENAME);
}
int BoneExtended::get_chain_length()
{
return chain_length;
}
void BoneExtended::set_chain_length(const int aLength)
{
chain_length = aLength;
}
void BoneExtended::set_leaf_bone(bool state)
{
is_leaf = state;
}
bool BoneExtended::is_leaf_bone()
{
return is_leaf;
}
void BoneExtended::set_roll(float roll)
{
this->roll = roll;
this->has_custom_roll = true;
}
bool BoneExtended::has_roll()
{
return this->has_custom_roll;
}
float BoneExtended::get_roll()
{
return this->roll;
}
void BoneExtended::set_tail(float vec[])
{
this->tail[0] = vec[0];
this->tail[1] = vec[1];
this->tail[2] = vec[2];
this->has_custom_tail = true;
}
bool BoneExtended::has_tail()
{
return this->has_custom_tail;
}
float *BoneExtended::get_tail()
{
return this->tail;
}
inline bool isInteger(const std::string & s)
{
if (s.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) return false;
char * p;
strtol(s.c_str(), &p, 10);
return (*p == 0);
}
void BoneExtended::set_bone_layers(std::string layerString, std::vector<std::string> &layer_labels)
{
std::stringstream ss(layerString);
std::string layer;
int pos;
while (ss >> layer) {
/* Blender uses numbers to specify layers*/
if (isInteger(layer))
{
pos = atoi(layer.c_str());
if (pos >= 0 && pos < 32) {
this->bone_layers = bc_set_layer(this->bone_layers, pos);
continue;
}
}
/* layer uses labels (not supported by blender). Map to layer numbers:*/
pos = find(layer_labels.begin(), layer_labels.end(), layer) - layer_labels.begin();
if (pos >= layer_labels.size()) {
layer_labels.push_back(layer); /* remember layer number for future usage*/
}
if (pos > 31)
{
fprintf(stderr, "Too many layers in Import. Layer %s mapped to Blender layer 31\n", layer.c_str());
pos = 31;
}
/* If numeric layers and labeled layers are used in parallel (unlikely),
* we get a potential mixup. Just leave as is for now.
*/
this->bone_layers = bc_set_layer(this->bone_layers, pos);
}
}
std::string BoneExtended::get_bone_layers(int bitfield)
{
std::string result = "";
std::string sep = "";
int bit = 1u;
std::ostringstream ss;
for (int i = 0; i < 32; i++)
{
if (bit & bitfield)
{
ss << sep << i;
sep = " ";
}
bit = bit << 1;
}
return ss.str();
}
int BoneExtended::get_bone_layers()
{
return (bone_layers == 0) ? 1 : bone_layers; // ensure that the bone is in at least one bone layer!
}
void BoneExtended::set_use_connect(int use_connect)
{
this->use_connect = use_connect;
}
int BoneExtended::get_use_connect()
{
return this->use_connect;
}
/**
* Stores a 4*4 matrix as a custom bone property array of size 16
*/
void bc_set_IDPropertyMatrix(EditBone *ebone, const char *key, float mat[4][4])
{
IDProperty *idgroup = (IDProperty *)ebone->prop;
if (idgroup == NULL)
{
IDPropertyTemplate val = { 0 };
idgroup = IDP_New(IDP_GROUP, &val, "RNA_EditBone ID properties");
ebone->prop = idgroup;
}
IDPropertyTemplate val = { 0 };
val.array.len = 16;
val.array.type = IDP_FLOAT;
IDProperty *data = IDP_New(IDP_ARRAY, &val, key);
float *array = (float *)IDP_Array(data);
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
array[4 * i + j] = mat[i][j];
IDP_AddToGroup(idgroup, data);
}
#if 0
/**
* Stores a Float value as a custom bone property
*
* Note: This function is currently not needed. Keep for future usage
*/
static void bc_set_IDProperty(EditBone *ebone, const char *key, float value)
{
if (ebone->prop == NULL)
{
IDPropertyTemplate val = { 0 };
ebone->prop = IDP_New(IDP_GROUP, &val, "RNA_EditBone ID properties");
}
IDProperty *pgroup = (IDProperty *)ebone->prop;
IDPropertyTemplate val = { 0 };
IDProperty *prop = IDP_New(IDP_FLOAT, &val, key);
IDP_Float(prop) = value;
IDP_AddToGroup(pgroup, prop);
}
#endif
/*
* Get a custom property when it exists.
* This function is also used to check if a property exists.
*/
IDProperty *bc_get_IDProperty(Bone *bone, std::string key)
{
return (bone->prop == NULL) ? NULL : IDP_GetPropertyFromGroup(bone->prop, key.c_str());
}
/**
* Read a custom bone property and convert to float
* Return def if the property does not exist.
*/
float bc_get_property(Bone *bone, std::string key, float def)
{
float result = def;
IDProperty *property = bc_get_IDProperty(bone, key);
if (property) {
switch (property->type) {
case IDP_INT:
result = (float)(IDP_Int(property));
break;
case IDP_FLOAT:
result = (float)(IDP_Float(property));
break;
case IDP_DOUBLE:
result = (float)(IDP_Double(property));
break;
default:
result = def;
}
}
return result;
}
/**
* Read a custom bone property and convert to matrix
* Return true if conversion was succesfull
*
* Return false if:
* - the property does not exist
* - is not an array of size 16
*/
bool bc_get_property_matrix(Bone *bone, std::string key, float mat[4][4])
{
IDProperty *property = bc_get_IDProperty(bone, key);
if (property && property->type == IDP_ARRAY && property->len == 16) {
float *array = (float *)IDP_Array(property);
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
mat[i][j] = array[4 * i + j];
return true;
}
return false;
}
/**
* get a vector that is stored in 3 custom properties (used in Blender <= 2.78)
*/
void bc_get_property_vector(Bone *bone, std::string key, float val[3], const float def[3])
{
val[0] = bc_get_property(bone, key + "_x", def[0]);
val[1] = bc_get_property(bone, key + "_y", def[1]);
val[2] = bc_get_property(bone, key + "_z", def[2]);
}
/**
* Check if vector exist stored in 3 custom properties (used in Blender <= 2.78)
*/
static bool has_custom_props(Bone *bone, bool enabled, std::string key)
{
if (!enabled)
return false;
return (bc_get_IDProperty(bone, key + "_x")
|| bc_get_IDProperty(bone, key + "_y")
|| bc_get_IDProperty(bone, key + "_z"));
}
/**
* Check if custom information about bind matrix exists and modify the from_mat
* accordingly.
*
* Note: This is old style for Blender <= 2.78 only kept for compatibility
*/
void bc_create_restpose_mat(const ExportSettings *export_settings, Bone *bone, float to_mat[4][4], float from_mat[4][4], bool use_local_space)
{
float loc[3];
float rot[3];
float scale[3];
static const float V0[3] = { 0, 0, 0 };
if (!has_custom_props(bone, export_settings->keep_bind_info, "restpose_loc") &&
!has_custom_props(bone, export_settings->keep_bind_info, "restpose_rot") &&
!has_custom_props(bone, export_settings->keep_bind_info, "restpose_scale"))
{
/* No need */
copy_m4_m4(to_mat, from_mat);
return;
}
bc_decompose(from_mat, loc, rot, NULL, scale);
loc_eulO_size_to_mat4(to_mat, loc, rot, scale, 6);
if (export_settings->keep_bind_info) {
bc_get_property_vector(bone, "restpose_loc", loc, loc);
if (use_local_space && bone->parent) {
Bone *b = bone;
while (b->parent) {
b = b->parent;
float ploc[3];
bc_get_property_vector(b, "restpose_loc", ploc, V0);
loc[0] += ploc[0];
loc[1] += ploc[1];
loc[2] += ploc[2];
}
}
}
if (export_settings->keep_bind_info) {
if (bc_get_IDProperty(bone, "restpose_rot_x"))
rot[0] = DEG2RADF(bc_get_property(bone, "restpose_rot_x", 0));
if (bc_get_IDProperty(bone, "restpose_rot_y"))
rot[1] = DEG2RADF(bc_get_property(bone, "restpose_rot_y", 0));
if (bc_get_IDProperty(bone, "restpose_rot_z"))
rot[2] = DEG2RADF(bc_get_property(bone, "restpose_rot_z", 0));
}
if (export_settings->keep_bind_info) {
bc_get_property_vector(bone, "restpose_scale", scale, scale);
}
loc_eulO_size_to_mat4(to_mat, loc, rot, scale, 6);
}
/*
Make 4*4 matrices better readable
*/
void bc_sanitize_mat(float mat[4][4], int precision)
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
mat[i][j] = double_round(mat[i][j], precision);
}
void bc_sanitize_mat(double mat[4][4], int precision)
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
mat[i][j] = double_round(mat[i][j], precision);
}
void bc_copy_m4_farray(float r[4][4], float *a)
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
r[i][j] = *a++;
}
void bc_copy_farray_m4(float *r, float a[4][4])
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
*r++ = a[i][j];
}
/*
* Returns name of Active UV Layer or empty String if no active UV Layer defined
*/
std::string bc_get_active_uvlayer_name(Mesh *me)
{
int num_layers = CustomData_number_of_layers(&me->fdata, CD_MTFACE);
if (num_layers) {
char *layer_name = bc_CustomData_get_active_layer_name(&me->fdata, CD_MTFACE);
if (layer_name) {
return std::string(layer_name);
}
}
return "";
}
/*
* Returns name of Active UV Layer or empty String if no active UV Layer defined.
* Assuming the Object is of type MESH
*/
std::string bc_get_active_uvlayer_name(Object *ob)
{
Mesh *me = (Mesh *)ob->data;
return bc_get_active_uvlayer_name(me);
}
/*
* Returns UV Layer name or empty string if layer index is out of range
*/
std::string bc_get_uvlayer_name(Mesh *me, int layer)
{
int num_layers = CustomData_number_of_layers(&me->fdata, CD_MTFACE);
if (num_layers && layer < num_layers) {
char *layer_name = bc_CustomData_get_layer_name(&me->fdata, CD_MTFACE, layer);
if (layer_name) {
return std::string(layer_name);
}
}
return "";
}
|