Copy some code from model-xs branch

(fixes for set_material and implementation of duplicate_objects_grid).
Switch back to Perl implementation for duplicate_objects_grid.

(ModelObject is still missing a few necessary methods for it to work.)