Copy some code from model-xs branch

(fixes for set_material and implementation of duplicate_objects_grid).