예제 #1
0
void BKE_mesh_make_local(Mesh *me)
{
	Main *bmain = G.main;
	Object *ob;
	int is_local = FALSE, is_lib = FALSE;

	/* - only lib users: do nothing
	 * - only local users: set flag
	 * - mixed: make copy
	 */

	if (me->id.lib == NULL) return;
	if (me->id.us == 1) {
		id_clear_lib_data(bmain, &me->id);
		expand_local_mesh(me);
		return;
	}

	for (ob = bmain->object.first; ob && ELEM(0, is_lib, is_local); ob = ob->id.next) {
		if (me == ob->data) {
			if (ob->id.lib) is_lib = TRUE;
			else is_local = TRUE;
		}
	}

	if (is_local && is_lib == FALSE) {
		id_clear_lib_data(bmain, &me->id);
		expand_local_mesh(me);
	}
	else if (is_local && is_lib) {
		Mesh *me_new = BKE_mesh_copy(me);
		me_new->id.us = 0;


		/* Remap paths of new ID using old library as base. */
		BKE_id_lib_local_paths(bmain, me->id.lib, &me_new->id);

		for (ob = bmain->object.first; ob; ob = ob->id.next) {
			if (me == ob->data) {
				if (ob->id.lib == NULL) {
					BKE_mesh_assign_object(ob, me_new);
				}
			}
		}
	}
}
예제 #2
0
Object *MeshImporter::create_mesh_object(COLLADAFW::Node *node, COLLADAFW::InstanceGeometry *geom,
                                         bool isController,
                                         std::map<COLLADAFW::UniqueId, Material *>& uid_material_map,
                                         std::map<Material *, TexIndexTextureArrayMap>& material_texture_mapping_map)
{
	const COLLADAFW::UniqueId *geom_uid = &geom->getInstanciatedObjectId();
	
	// check if node instanciates controller or geometry
	if (isController) {
		
		geom_uid = armature_importer->get_geometry_uid(*geom_uid);
		
		if (!geom_uid) {
			fprintf(stderr, "Couldn't find a mesh UID by controller's UID.\n");
			return NULL;
		}
	}
	else {
		
		if (uid_mesh_map.find(*geom_uid) == uid_mesh_map.end()) {
			// this could happen if a mesh was not created
			// (e.g. if it contains unsupported geometry)
			fprintf(stderr, "Couldn't find a mesh by UID.\n");
			return NULL;
		}
	}
	if (!uid_mesh_map[*geom_uid]) return NULL;
	
	// name Object
	const std::string& id = node->getName().size() ? node->getName() : node->getOriginalId();
	const char *name = (id.length()) ? id.c_str() : NULL;
	
	// add object
	Object *ob = bc_add_object(scene, OB_MESH, name);
	bc_set_mark(ob); // used later for material assignement optimization


	// store object pointer for ArmatureImporter
	uid_object_map[*geom_uid] = ob;
	imported_objects.push_back(ob);
	
	// replace ob->data freeing the old one
	Mesh *old_mesh = (Mesh *)ob->data;
	Mesh *new_mesh = uid_mesh_map[*geom_uid];

	BKE_mesh_assign_object(ob, new_mesh);
	BKE_mesh_calc_normals(new_mesh);

	if (old_mesh->id.us == 0) BKE_libblock_free(G.main, old_mesh);
	
	char layername[100];
	layername[0] = '\0';
	MTFace *texture_face = NULL;
	
	COLLADAFW::MaterialBindingArray& mat_array =
	    geom->getMaterialBindings();
	
	// loop through geom's materials
	for (unsigned int i = 0; i < mat_array.getCount(); i++) {
		
		if (mat_array[i].getReferencedMaterial().isValid()) {
			texture_face = assign_material_to_geom(mat_array[i], uid_material_map, ob, geom_uid,
			                                       layername, texture_face,
			                                       material_texture_mapping_map, i);
		}
		else {
			fprintf(stderr, "invalid referenced material for %s\n", mat_array[i].getName().c_str());
		}
	}

	return ob;
}