Jump to content

[SOLVED] Replace material on many objects, "material selector"


photo

Recommended Posts

Hi all
 
I need to be able to replace materials on the objects through code.
 
Example:
I have material "sample_01", which is assigned to many objects and surfaces in the current scene. Now I want all of these objects to assign material "sample_02", then sample_03 etc.
These materials are in the same library, and have a clear structure by name.

 

My idea:
temp = sample_01.getObjects ();
temp.setMaterial (sample_02);
 
But the functions of type material :: getObjects in documentation is not found.
 
Now I look in the direction of Material :: saveState and Material :: restoreState.
 
Maybe you have any other solutions to this issue?

 

thanks in advance

 

 

Link to comment

Anton, what effect do you want to achive ? In general modification of the assigned material instance (in your case "sample_01") instead of trying to replace it on all surfaces should be the way to follow, therefore save()/restore() sounds promissing     

Link to comment
  1. For each material sub-type create a reference material instance e.g. roof_type, wall_type in addition to your _1, _2, _3... material variants
  2. Asssign these reference materials to your geometry
  3. Swap materials on-the-fly by:
  4. serialization of selected variant _1. _2, .. material to Buffer stream (in-memory) via Material::saveState( buffer )
  5. Call Material::restoreState( buffer ) on reference material instance

This should swap your scene materials without much effort 

Link to comment

	void select(Material curMat, Material varMat) {
		Buffer buffer = new Buffer();					// new memory buffer
		curMat = engine.materials.findMaterial(curMat);			// current material on objects
		Material temp_mat = engine.materials.findMaterial(varMat);	// temp material to store variants
		temp_mat.saveState(buffer);					// store material in temp
	
		curMat.restoreState(buffer);					// restore material settings to current material
		//buffer.clear();
		log.message("select_yes\n");
	}

and then

	select("roof_type","roof_type_2");

but don't work

Link to comment

BTW, just as a hint: you define select(Material curMat, Material varMat), but actually string parameters are provided. Then curMat string parameter will be used to find material and curMat then will be assigned a Material instance (or NULL). Though UNIGINE as untyped script language supports this, I wouldn't recommend it.

Link to comment
×
×
  • Create New...