Exemple #1
0
std::string ArmatureExporter::add_joints_source(Object *ob_arm, ListBase *defbase, const std::string& controller_id)
{
	std::string source_id = controller_id + JOINTS_SOURCE_ID_SUFFIX;

	int totjoint = 0;
	bDeformGroup *def;
	for (def = (bDeformGroup*)defbase->first; def; def = def->next) {
		if (is_bone_defgroup(ob_arm, def))
			totjoint++;
	}

	COLLADASW::NameSource source(mSW);
	source.setId(source_id);
	source.setArrayId(source_id + ARRAY_ID_SUFFIX);
	source.setAccessorCount(totjoint);
	source.setAccessorStride(1);
	
	COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
	param.push_back("JOINT");

	source.prepareToAppendValues();

	for (def = (bDeformGroup*)defbase->first; def; def = def->next) {
		Bone *bone = get_bone_from_defgroup(ob_arm, def);
		if (bone)
			source.appendValues(get_joint_sid(bone, ob_arm));
	}

	source.finish();

	return source_id;
}
Exemple #2
0
std::string ArmatureExporter::add_inv_bind_mats_source(Object *ob_arm, ListBase *defbase, const std::string& controller_id)
{
	std::string source_id = controller_id + BIND_POSES_SOURCE_ID_SUFFIX;

	COLLADASW::FloatSourceF source(mSW);
	source.setId(source_id);
	source.setArrayId(source_id + ARRAY_ID_SUFFIX);
	source.setAccessorCount(BLI_countlist(defbase));
	source.setAccessorStride(16);
	
	source.setParameterTypeName(&COLLADASW::CSWC::CSW_VALUE_TYPE_FLOAT4x4);
	COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
	param.push_back("TRANSFORM");

	source.prepareToAppendValues();

	bPose *pose = ob_arm->pose;
	bArmature *arm = (bArmature*)ob_arm->data;

	int flag = arm->flag;

	// put armature in rest position
	if (!(arm->flag & ARM_RESTPOS)) {
		arm->flag |= ARM_RESTPOS;
		where_is_pose(scene, ob_arm);
	}

	for (bDeformGroup *def = (bDeformGroup*)defbase->first; def; def = def->next) {
		if (is_bone_defgroup(ob_arm, def)) {

			bPoseChannel *pchan = get_pose_channel(pose, def->name);

			float mat[4][4];
			float world[4][4];
			float inv_bind_mat[4][4];

			// make world-space matrix, arm_mat is armature-space
			mul_m4_m4m4(world, pchan->bone->arm_mat, ob_arm->obmat);
			
			invert_m4_m4(mat, world);
			converter.mat4_to_dae(inv_bind_mat, mat);

			source.appendValues(inv_bind_mat);
		}
	}

	// back from rest positon
	if (!(flag & ARM_RESTPOS)) {
		arm->flag = flag;
		where_is_pose(scene, ob_arm);
	}

	source.finish();

	return source_id;
}
Exemple #3
0
void ArmatureExporter::add_vertex_weights_element(const std::string& weights_source_id, const std::string& joints_source_id, Mesh *me,
								Object *ob_arm, ListBase *defbase)
{
	COLLADASW::VertexWeightsElement weights(mSW);
	COLLADASW::InputList &input = weights.getInputList();

	int offset = 0;
	input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::JOINT, // constant declared in COLLADASWInputList.h
									 COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, joints_source_id), offset++));
	input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::WEIGHT,
									 COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, weights_source_id), offset++));

	weights.setCount(me->totvert);

	// write number of deformers per vertex
	COLLADASW::PrimitivesBase::VCountList vcount;
	int i;
	for (i = 0; i < me->totvert; i++) {
		vcount.push_back(me->dvert[i].totweight);
	}

	weights.prepareToAppendVCountValues();
	weights.appendVertexCount(vcount);

	// def group index -> joint index
	std::map<int, int> joint_index_by_def_index;
	bDeformGroup *def;
	int j;
	for (def = (bDeformGroup*)defbase->first, i = 0, j = 0; def; def = def->next, i++) {
		if (is_bone_defgroup(ob_arm, def))
			joint_index_by_def_index[i] = j++;
		else
			joint_index_by_def_index[i] = -1;
	}

	weights.CloseVCountAndOpenVElement();

	// write deformer index - weight index pairs
	int weight_index = 0;
	for (i = 0; i < me->totvert; i++) {
		MDeformVert *dvert = &me->dvert[i];
		for (int j = 0; j < dvert->totweight; j++) {
			weights.appendValues(joint_index_by_def_index[dvert->dw[j].def_nr]);
			weights.appendValues(weight_index++);
		}
	}

	weights.finish();
}
std::string ControllerExporter::add_inv_bind_mats_source(Object *ob_arm, ListBase *defbase, const std::string& controller_id)
{
	std::string source_id = controller_id + BIND_POSES_SOURCE_ID_SUFFIX;

	int totjoint = 0;
	for (bDeformGroup *def = (bDeformGroup *)defbase->first; def; def = def->next) {
		if (is_bone_defgroup(ob_arm, def))
			totjoint++;
	}

	COLLADASW::FloatSourceF source(mSW);
	source.setId(source_id);
	source.setArrayId(source_id + ARRAY_ID_SUFFIX);
	source.setAccessorCount(totjoint); //BLI_listbase_count(defbase));
	source.setAccessorStride(16);
	
	source.setParameterTypeName(&COLLADASW::CSWC::CSW_VALUE_TYPE_FLOAT4x4);
	COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
	param.push_back("TRANSFORM");

	source.prepareToAppendValues();

	bPose *pose = ob_arm->pose;
	bArmature *arm = (bArmature *)ob_arm->data;

	int flag = arm->flag;

	// put armature in rest position
	if (!(arm->flag & ARM_RESTPOS)) {
		arm->flag |= ARM_RESTPOS;
		BKE_pose_where_is(scene, ob_arm);
	}

	for (bDeformGroup *def = (bDeformGroup *)defbase->first; def; def = def->next) {
		if (is_bone_defgroup(ob_arm, def)) {
			bPoseChannel *pchan = BKE_pose_channel_find_name(pose, def->name);

			float mat[4][4];
			float world[4][4];
			float inv_bind_mat[4][4];

			
			// SL/OPEN_SIM COMPATIBILITY
			if (export_settings->open_sim) {
				// Only translations, no rotation vs armature
				float temp[4][4];
				unit_m4(temp);
				copy_v3_v3(temp[3], pchan->bone->arm_mat[3]);
				mul_m4_m4m4(world, ob_arm->obmat, temp);

				// Add Maya restpose matrix (if defined as properties)
				float restpose_mat[4][4];
				create_restpose_mat(pchan->bone, restpose_mat);
				mul_m4_m4m4(world, world, restpose_mat);

			}
			else {
				// make world-space matrix, arm_mat is armature-space
				mul_m4_m4m4(world, ob_arm->obmat, pchan->bone->arm_mat);
			}


			invert_m4_m4(mat, world);
			converter.mat4_to_dae(inv_bind_mat, mat);

			source.appendValues(inv_bind_mat);
		}
	}

	// back from rest positon
	if (!(flag & ARM_RESTPOS)) {
		arm->flag = flag;
		BKE_pose_where_is(scene, ob_arm);
	}

	source.finish();

	return source_id;
}