Vray Mesh Light

Hello, I tried to make a script that creates a VRay Mesh Light in the place of the currently selected geometry that has the same functionality as the V-ray Mesh Light Button on the V-Ray Toolbar:

for obj in selection as array do
mesh_pos = obj.pivot
VRayLight type:3 pos:mesh_pos mesh_replaceOnPick:off mesh_source:obj isSelected:on
$.mesh_source = obj
delete obj

However, the last line causes the mesh light's source to be deleted as well.
If I click the V-ray Mesh Light Button, the original geometry gets deleted, but the Mesh Light's Source remains intact. How should I modify my code so that the mesh source remains unchanged but I don't have to keep any unnecessary geometry?