refactor bpy code