I've many objects that i need to Sort its faces with the same criteria(As example according to VIEW Z Axis. I do iterate through selected objects through:

import bpy
for obj in bpy.context.selected_objects:
bpy.context.scene.objects.active = obj

but haven't succeeded performing the view dependant part. enter image description here

  • $\begingroup$ Are you trying to do this without using bpy.ops? $\endgroup$
    – cmomoney
    Jan 2, 2017 at 23:40
  • $\begingroup$ No i don't, my question is all about the _bpy.ops.._part. $\endgroup$
    – justlotfy
    Jan 3, 2017 at 16:37
  • 1
    $\begingroup$ It will have to be written as an operator, so it can be run in the 3D viewport, else it will error(won't have a view to work with). $\endgroup$
    – cmomoney
    Jan 3, 2017 at 17:02

1 Answer 1


The answer is in your picture. I modified the Simple Operator template:

import bpy

def main(context):
    for obj in bpy.context.selected_objects:
        if obj.type == 'MESH': #make sure its a mesh
            context.scene.objects.active = obj
            bpy.ops.mesh.sort_elements(type='VIEW_ZAXIS') #this is what you're looking for

class SimpleOperator(bpy.types.Operator):
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        return {'FINISHED'}

def register():

def unregister():

if __name__ == "__main__":

Press spacebar in 3D viewport and type Simple Operator in the text field.

  • $\begingroup$ Just added (bpy.) at the beginning of the first line under (if obj.type...) Worked perfectly, seems my problem that i was trying to run the script from the text editor, while it must run through the view itself.Thanks a lot $\endgroup$
    – justlotfy
    Jan 8, 2017 at 12:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .