macroScript SubMaterialPicker category:"Stack" ( -- Assigns the selected material to the SME view or CME depending on the user's state fn setMaterial theMat = ( if SME.isOpen() and sme.ActiveView > 0 then ( local nodePos = [0,0] local selView = sme.GetView sme.ActiveView selView.CreateNode theMat &nodePos SME.setMtlInParamEditor theMat ) else ( meditMaterials[medit.GetActiveMtlSlot()] = theMat ) ) -- Gets the view's position. fn getViewportTM worldPos = ( local offset = getViewFov() * 10 local tm = inverse (viewport.getTM()) local pos = tm.pos case viewport.getType() of ( #view_left:pos = worldPos + [-offset,0,0] #view_right:pos = worldPos + [offset,0,0] #view_front:pos = worldPos + [0,-offset,0] #view_back:pos = worldPos + [0,offset,0] #view_top:pos = worldPos + [0,0,offset] #view_bottom:pos = worldPos + [0,0,-offset] #view_iso_user:pos = worldPos + tm.row3 * offset ) pos ) -- Main function. -- If shift is pressed, it loads the whole MultiMaterial otherwise it just loads the material in the face id. fn pickMaterial worldPos shiftKey = ( -- Gets the viewport position, creates and shoots a ray and finds the closest hit local viewPos = getViewportTM worldPos local myRay = ray viewPos (normalize (worldPos-viewPos)) local closestHit local closestHitDistance for hit in intersectRayScene myRay do ( local d = distance hit[2].pos viewPos if closestHitDistance == undefined or closestHitDistance > d then ( closestHit = hit[1] closestHitDistance = d ) ) if closestHitDistance != undefined then ( local mat = closestHit.material if mat != undefined then ( if classof mat == multimaterial and not shiftKey then ( local matID if classof closestHit != editable_mesh then with redraw off ( local snap = snapshot closestHit local hit = intersectRayEx snap myRay matID = getFaceMatID snap hit[2] delete snap ) else ( local hit = intersectRayEx closestHit myRay matID = getFaceMatID closestHit hit[2] ) if matID != undefined then ( for i=1 to mat.numsubs do ( if mat.materialIDList[i] == matID then ( local pickedMat = mat.materialList[i] if pickedMat != undefined then ( setMaterial pickedMat ) ) ) ) ) else ( setMaterial mat ) ) ) ) -- Tool registration tool pickTool ( on mousePoint clickno do ( if clickno == 1 then pickMaterial worldPoint shiftKey else #stop ) ) -- Execution on execute do ( startTool pickTool ) )