Blender 2.8 Apply scale after creating object

I am working on a file format importer with the folks over at Mcneel and I would like to handle unit conversion on import. So far so good everything is working well and I can manage to scale the objects however when i attempt to apply the transformation I am running into errors. It seems my object is not currently in the view layer. Any help would be greatly appreciated.

    blender_object.scale = scale_factor
   
    bpy.ops.object.select_all(action='DESELECT')
    
    if(blender_object.select_get() is False):
       blender_object.select_set(True)

    bpy.context.view_layer.objects.active = blender_object
    bpy.ops.object.transform_apply(location = False, scale = True, rotation = False)
    bpy.ops.object.select_all(action='DESELECT')

Joel

This has been solved. Nathon over at McNeel has given me some input.

joel

Is this the same error you’re getting? For the longest I couldn’t figure out what was giving it. Don’t know if this is a python bug or what.

Yes that was one of the errors I was receiving. It seems that it was due to the fact that I was attempting to apply a transform before the object was in the view layer and the scene.