How to change the material of a facet?
kabexef
Posts: 77
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
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.
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!