Example #1
0
int id_copy(ID *id, ID **newid, int test)
{
	if(!test) *newid= NULL;

	/* conventions:
	 * - make shallow copy, only this ID block
	 * - id.us of the new ID is set to 1 */
	switch(GS(id->name)) {
		case ID_SCE:
			return 0; /* can't be copied from here */
		case ID_LI:
			return 0; /* can't be copied from here */
		case ID_OB:
			if(!test) *newid= (ID*)copy_object((Object*)id);
			return 1;
		case ID_ME:
			if(!test) *newid= (ID*)copy_mesh((Mesh*)id);
			return 1;
		case ID_CU:
			if(!test) *newid= (ID*)copy_curve((Curve*)id);
			return 1;
		case ID_MB:
			if(!test) *newid= (ID*)copy_mball((MetaBall*)id);
			return 1;
		case ID_MA:
			if(!test) *newid= (ID*)copy_material((Material*)id);
			return 1;
		case ID_TE:
			if(!test) *newid= (ID*)copy_texture((Tex*)id);
			return 1;
		case ID_IM:
			if(!test) *newid= (ID*)copy_image((Image*)id);
			return 1;
		case ID_LT:
			if(!test) *newid= (ID*)copy_lattice((Lattice*)id);
			return 1;
		case ID_LA:
			if(!test) *newid= (ID*)copy_lamp((Lamp*)id);
			return 1;
		case ID_SPK:
			if(!test) *newid= (ID*)copy_speaker((Speaker*)id);
			return 1;
		case ID_CA:
			if(!test) *newid= (ID*)copy_camera((Camera*)id);
			return 1;
		case ID_IP:
			return 0; /* deprecated */
		case ID_KE:
			if(!test) *newid= (ID*)copy_key((Key*)id);
			return 1;
		case ID_WO:
			if(!test) *newid= (ID*)copy_world((World*)id);
			return 1;
		case ID_SCR:
			return 0; /* can't be copied from here */
		case ID_VF:
			return 0; /* not implemented */
		case ID_TXT:
			if(!test) *newid= (ID*)copy_text((Text*)id);
			return 1;
		case ID_SCRIPT:
			return 0; /* deprecated */
		case ID_SO:
			return 0; /* not implemented */
		case ID_GR:
			if(!test) *newid= (ID*)copy_group((Group*)id);
			return 1;
		case ID_AR:
			if(!test) *newid= (ID*)copy_armature((bArmature*)id);
			return 1;
		case ID_AC:
			if(!test) *newid= (ID*)copy_action((bAction*)id);
			return 1;
		case ID_NT:
			if(!test) *newid= (ID*)ntreeCopyTree((bNodeTree*)id);
			return 1;
		case ID_BR:
			if(!test) *newid= (ID*)copy_brush((Brush*)id);
			return 1;
		case ID_PA:
			if(!test) *newid= (ID*)psys_copy_settings((ParticleSettings*)id);
			return 1;
		case ID_WM:
			return 0; /* can't be copied from here */
		case ID_GD:
			return 0; /* not implemented */
	}
	
	return 0;
}
Example #2
0
/* settings: 0 - preview, 1 - render */
Mesh *rna_Object_to_mesh(Object *ob, ReportList *reports, Scene *sce, int apply_modifiers, int settings)
{
	Mesh *tmpmesh;
	Curve *tmpcu = NULL;
	Object *tmpobj = NULL;
	int render = settings == eModifierMode_Render, i;
	int cage = !apply_modifiers;

	/* perform the mesh extraction based on type */
	switch (ob->type) {
	case OB_FONT:
	case OB_CURVE:
	case OB_SURF:

		/* copies object and modifiers (but not the data) */
		tmpobj= copy_object(ob);
		tmpcu = (Curve *)tmpobj->data;
		tmpcu->id.us--;

		/* if getting the original caged mesh, delete object modifiers */
		if( cage )
			object_free_modifiers(tmpobj);

		/* copies the data */
		tmpobj->data = copy_curve( (Curve *) ob->data );

#if 0
		/* copy_curve() sets disp.first null, so currently not need */
		{
			Curve *cu;
			cu = (Curve *)tmpobj->data;
			if( cu->disp.first )
				MEM_freeN( cu->disp.first );
			cu->disp.first = NULL;
		}
	
#endif

		/* get updated display list, and convert to a mesh */
		makeDispListCurveTypes( sce, tmpobj, 0 );
		nurbs_to_mesh( tmpobj );
		
		/* nurbs_to_mesh changes the type to a mesh, check it worked */
		if (tmpobj->type != OB_MESH) {
			free_libblock_us( &(G.main->object), tmpobj );
			BKE_report(reports, RPT_ERROR, "cant convert curve to mesh. Does the curve have any segments?");
			return NULL;
		}
		tmpmesh = tmpobj->data;
		free_libblock_us( &G.main->object, tmpobj );
		break;

	case OB_MBALL: {
		/* metaballs don't have modifiers, so just convert to mesh */
		Object *basis_ob = find_basis_mball(sce, ob);
		/* todo, re-generatre for render-res */
		/* metaball_polygonize(scene, ob) */

		if(ob != basis_ob)
			return NULL; /* only do basis metaball */

		tmpmesh = add_mesh("Mesh");
			
		if(render) {
			ListBase disp = {NULL, NULL};
			makeDispListMBall_forRender(sce, ob, &disp);
			mball_to_mesh(&disp, tmpmesh);
			freedisplist(&disp);
		}
		else
			mball_to_mesh(&ob->disp, tmpmesh);
		break;

	}
	case OB_MESH:
		/* copies object and modifiers (but not the data) */
		if (cage) {
			/* copies the data */
			tmpmesh = copy_mesh( ob->data );
		/* if not getting the original caged mesh, get final derived mesh */
		} else {
			/* Make a dummy mesh, saves copying */
			DerivedMesh *dm;
			/* CustomDataMask mask = CD_MASK_BAREMESH|CD_MASK_MTFACE|CD_MASK_MCOL; */
			CustomDataMask mask = CD_MASK_MESH; /* this seems more suitable, exporter,
												   for example, needs CD_MASK_MDEFORMVERT */
			
			/* Write the display mesh into the dummy mesh */
			if (render)
				dm = mesh_create_derived_render( sce, ob, mask );
			else
				dm = mesh_create_derived_view( sce, ob, mask );
			
			tmpmesh = add_mesh( "Mesh" );
			DM_to_mesh( dm, tmpmesh );
			dm->release( dm );
		}
		
		break;
	default:
		BKE_report(reports, RPT_ERROR, "Object does not have geometry data");
		return NULL;
	}

	/* Copy materials to new mesh */
	switch (ob->type) {
	case OB_SURF:
	case OB_FONT:
	case OB_CURVE:
		tmpmesh->totcol = tmpcu->totcol;		
		
		/* free old material list (if it exists) and adjust user counts */
		if( tmpcu->mat ) {
			for( i = tmpcu->totcol; i-- > 0; ) {
				/* are we an object material or data based? */

				tmpmesh->mat[i] = ob->matbits[i] ? ob->mat[i] : tmpcu->mat[i];

				if (tmpmesh->mat[i]) {
					tmpmesh->mat[i]->id.us++;
				}
			}
		}
		break;

#if 0
	/* Crashes when assigning the new material, not sure why */
	case OB_MBALL:
		tmpmb = (MetaBall *)ob->data;
		tmpmesh->totcol = tmpmb->totcol;
		
		/* free old material list (if it exists) and adjust user counts */
		if( tmpmb->mat ) {
			for( i = tmpmb->totcol; i-- > 0; ) {
				tmpmesh->mat[i] = tmpmb->mat[i]; /* CRASH HERE ??? */
				if (tmpmesh->mat[i]) {
					tmpmb->mat[i]->id.us++;
				}
			}
		}
		break;
#endif

	case OB_MESH:
		if (!cage) {
			Mesh *origmesh= ob->data;
			tmpmesh->flag= origmesh->flag;
			tmpmesh->mat = MEM_dupallocN(origmesh->mat);
			tmpmesh->totcol = origmesh->totcol;
			tmpmesh->smoothresh= origmesh->smoothresh;
			if( origmesh->mat ) {
				for( i = origmesh->totcol; i-- > 0; ) {
					/* are we an object material or data based? */
					tmpmesh->mat[i] = ob->matbits[i] ? ob->mat[i] : origmesh->mat[i];

					if (tmpmesh->mat[i]) {
						tmpmesh->mat[i]->id.us++;
					}
				}
			}
		}
		break;
	} /* end copy materials */

	/* we don't assign it to anything */
	tmpmesh->id.us--;
	
	/* make sure materials get updated in objects */
	test_object_materials( ( ID * ) tmpmesh );

	return tmpmesh;
}