How to change the material of a facet?

Hi, I tried to add a material (surface) to an existing object and "move" some facets to it. Unfortunately it doesn't work. Here is what I tried ...

(function(){	try {		var oNode = Scene.getPrimarySelection();		var oObject = oNode.getObject();		var oShape = oObject.getCurrentShape();		var oFacetMesh = oShape.getFacetMesh();				var oMaterial = new DzDefaultMaterial();		oMaterial.name = "My Material";		oMaterial.setLabel(oMaterial.name);		oShape.addMaterial(oMaterial); // will work		if(!oFacetMesh.createMaterialGroup(oMaterial.name)) {			return;		}				var aGroups = [];		for (var i = 0; i < oFacetMesh.getNumMaterialGroups(); i++) {			aGroups.push(oFacetMesh.getMaterialGroup(i));		}				oFacetMesh.beginEdit();		aGroups[0].removeIndex(0);		aGroups[1].insertIndex(0);		oFacetMesh.getFacet(0).faceGroupIndex = 1; // won't work		oFacetMesh.getFacet(0).materialIndex = 1; // won't work		oFacetMesh.makePersistent();		oFacetMesh.finishEdit();		oFacetMesh.makePersistent();	} catch (err) {		debug("ERROR in MyScript (" + err.lineNumber + "): " + err);	}})();

Any suggestions how it could work? Thanks a lot.

Comments

  • OmnifluxOmniflux Posts: 377

    I think you need to select the facets and then use addSelectedFacetsToMaterialGroup(...)

    See Select facet by id?

  • This seems to work, but feels like there must be a better way to do it.  Executed on a new object, which has one material named "Default", it creates a second materia and assigns every third facet to it, then colors the two materials red and green respectively for a visual check.

    (function(){	try {		var oNode = Scene.getPrimarySelection();		var oObject = oNode.getObject();		var oShape = oObject.getCurrentShape();		var oFacetMesh = oShape.getFacetMesh();				// Create a face group		oFacetMesh.createFaceGroup("MyFaceGroup");		print("Facets = ", oFacetMesh.getNumFacets(), " face groups ", 			oFacetMesh.getNumFaceGroups());				print("Face groups...");		for (var i = 0; i < oFacetMesh.getNumFaceGroups(); i++) {			var faceGroup = oFacetMesh.getFaceGroup(i);			print(faceGroup.objectName);		}				// Add some facet indices to face group		for (var i = 0; i < oFacetMesh.getNumFacets(); i++) {			if (i % 3 == 0) { 				faceGroup.addIndex(i)			};		}		print("Face Group ", faceGroup.getName(), " count ", faceGroup.count(), "face group contents ", faceGroup.toList());				var oMaterial = new DzDefaultMaterial();		oMaterial.name = "My Material";		oMaterial.setLabel(oMaterial.name);		oShape.addMaterial(oMaterial); // will work		if(!oFacetMesh.createMaterialGroup(oMaterial.name)) {			print("Failed to create mat group");			return;		}				// Set contrasting colors on the two materials		var col = Color(255,0,0);		oShape.findMaterial("Default").setDiffuseColor(col);		col = Color(0,255,0);		oShape.findMaterial("My Material").setDiffuseColor(col);				oFacetMesh.deselectAllFacets();		// Select faces as defined in the face group				oFacetMesh.selectFacets(faceGroup);		selFacets = oFacetMesh.getSelectedFacets();		print("Facets selected via face group ", faceGroup.getName(), selFacets);				// Add the facets currently selected to the newly created material group		var newMgIdx;		for (i = 0; i < oFacetMesh.getNumMaterialGroups(); i++) {			if (oFacetMesh.getMaterialGroup(i).getName() == "My Material") {				newMgIdx = i;			}		}		oFacetMesh.addSelectedFacetsToMaterialGroup(newMgIdx);		print("Material group ", oFacetMesh.getMaterialGroup(newMgIdx).getName(), " contains facets ", 			oFacetMesh.getMaterialGroup(newMgIdx).toList());					// What is the result?		for (var i = 0; i < oFacetMesh.getNumFacets(); i++) {			f = oFacetMesh.getFacet(i);			print("Facet ", i, 				"face group index ", f.faceGroupIndex, 				"material index ", f.materialIndex,				"material group ", oFacetMesh.getMaterialGroup(f.materialIndex).name)		}			} catch (err) {		debug("ERROR in MyScript (" + err.lineNumber + "): " + err);	}})();

    Adding indices to the face group is painful, but to do it in one go you need to use a DzIndexList, and I can't figure out how to get one of those.

  • Thanks a lot for all the help!

Sign In or Register to comment.