1
$\begingroup$

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

$\endgroup$
3
  • $\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

2
$\begingroup$

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.object.mode_set(mode='EDIT')
            bpy.ops.mesh.sort_elements(type='VIEW_ZAXIS') #this is what you're looking for
            bpy.ops.object.mode_set(mode='OBJECT')

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

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

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

def register():
    bpy.utils.register_class(SimpleOperator)

def unregister():
    bpy.utils.unregister_class(SimpleOperator)

if __name__ == "__main__":
    register()

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

$\endgroup$
1
  • $\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 .