automate mundane tasks

Hello lovelies,

I’m new to python out of the game engine, and was wondering how you automate tasks.

I want to go separate all faces into individual object, then set the origin to the center of the new object.

I found this:

bpy.ops.object.editmode_toggle()
bpy.ops.view3d.snap_cursor_to_selected()
bpy.ops.object.editmode_toggle()
bpy.ops.object.origin_set(type='ORIGIN_CURSOR', center='MEDIAN')

But where do I put it and how do I make a single button to do the said actions?

Thanks for the help.

Your code example, as far as I understand, does not split an object into different objects (each face one object is your wish?!).
The script assumes that you are in object-mode, then the first three commands move the cursor to … dependant of selected parts.
The last moves the origin …
The second command can not be done in the python-console (context problems) so it has to be done (I think) via an apropriate operator.

To learn that, make a text-window in Blender and look at the ‘templates’ or go directly to the templates directory with an editor to learn …

thanks PKHG.

Ok, so I grabbed the operator_simple.py and popped in my functions but it seems to struggle. Any Ideas?

import bpy


def main(context):
    for ob in context.scene.objects:
        print(ob)


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):
        for honeycomb in context.scene:
            bpy.ops.object.editmode_toggle()
            bpy.ops.view3d.snap_cursor_to_selected()
            bpy.ops.object.editmode_toggle()
            bpy.ops.object.origin_set(type='ORIGIN_CURSOR', center='MEDIAN')
            main(context)
        return {'FINISHED'}


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


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


if __name__ == "__main__":
    register()

    # test call
    bpy.ops.object.simple_operator()

console says: for line 19 in execute TypeError: ‘Scene’ object is not iterable

got it SORTED… Separating the faces is easy in blender, and then running the script with all the ob’s selected and just using bpy.ops.object.origin_set(type=‘ORIGIN_GEOMETRY’, center=‘MEDIAN’)

Thanks :eyebrowlift2:

Will look a bit later …(have to go shopping)

Learn to use the python console and do e.g.
dir(bpy.context.scene) and you see it is not not something like a list.
type e.g with after the . Ctrl space:
bpy.context.scene.
and guess what you may mean : objects!
so bpy.context.scene.objects you may want to use, BUT you have to check if it is a mesh not a lamp or camera !!!


   for honeycomb in context.scene.objects:
     if honeycomb.type == "MESH":
       #do what you want to do with a mesh