Materials
Creating a Material
Section titled “Creating a Material”let model = await SketchUpApi.getActiveModel();await model.performOperation(async operation => { const materialRef = operation.createMaterial('Some Material'); const material = await operation.entityForRef(materialRef); console.log(material);}, 'Create a material');let model = await SketchUpApi.getActiveModel()let materials = await model.getMaterials();let material = materials.findMaterialByName('<SOME_MATERIAL_NAME>');let model = await SketchUpApi.getActiveModel();await model.performOperation(async operation => { const materialRef = operation.createMaterial('Some Material'); const material = await operation.entityForRef(materialRef);
const allMaterials = await model.getMaterials(); console.log('Found a material', allMaterials.findMaterialByName(material.name));}, 'Finding a material');Deleting a Material
Section titled “Deleting a Material”let model = await SketchUpApi.getActiveModel();await model.performOperation(async operation => { const materialRef = operation.createMaterial('Some Material to Delete'); const material = await operation.entityForRef(materialRef);
let materials = await model.getMaterials(); console.log('Found a material', materials.findMaterialByName(material.name));
operation.removeMaterial(materialRef); materials = await model.getMaterials(); console.log('Material was deleted', materials.findMaterialByName(material.name));
}, 'Deleting a Material');Purge unused Materials
Section titled “Purge unused Materials”let model = await SketchUpApi.getActiveModel();let materialBaseName = 'MaterialTest';let [preserved, removed] = await model.performOperation(async operation => { const mat1 = await operation.entityForRef(operation.createMaterial(materialBaseName + '- 1')); const mat2 = await operation.entityForRef(operation.createMaterial(materialBaseName + '- 2')); const face = operation.createFace(model, [ [-10, -10, 0], [-10, 0, 0], [0, 0, 0], ]); operation.faceSetFrontMaterial(face, mat1); operation.purgeUnusedMaterials(); return [mat1, mat2];}, materialBaseName);let allMaterials = await model.getMaterials();console.log('Preserved Material', allMaterials.findMaterialById(preserved.id));console.log('Material was removed', allMaterials.findMaterialById(removed.id));let model = await SketchUpApi.getActiveModel();let loadedMaterial = await model.performOperation(async operation => { const materialRef = await operation.loadMaterial({ resource: { request: '/material.skm' }, }); return operation.entityForRef(materialRef);}, 'load material');