3
$\begingroup$

I found this code and tried to understand it. As far as I understand macro allows step by step execution. But I struggle to make it work right.

As far as I understand one has to define what each macro command is and then generate an operator that works off each macro command - right?

Sadly the Blender Python manual does not explain the use of macro well, or I did not find it.

Here is the code I have:

import bpy
from bpy.types import Operator, Macro


# Our finalizing operator, shall run after transform
class Finalize(Operator):
    bl_idname = "test.finalize"
    bl_label = "Finalize"

    def execute(self, context):
        print("DONE!")
        return {'FINISHED'}

class Make(Operator):
    bl_idname = "test.make"
    bl_label = "Make"

    def execute(self, context):
        bpy.ops.object.empty_add()
        return {'FINISHED'}



# Macro operator to concatenate transform and our finalization
class Test(Macro):
    bl_idname = "TEST_OT_Test"
    bl_label = "Test Macro"

    def execute(self, context):
        # Note that we have to register classes first before populating
        # the Macro operator    
        bpy.utils.register_class(Finalize)
        bpy.utils.register_class(Make)
        bpy.utils.register_class(Test)


        # The important bit: populate the macro operator with a sequence
        # of existing other operators
        Test.define("test.make")
        Test.define("test.finalize")
        return {'FINISHED'}

# BUTTON
class ObjectData(bpy.types.Panel):
    bl_label = "Object + Mesh Data"
    bl_idname = "ObjectData"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
    bl_category = "Modeling Toolbox"

    def draw(self, context):
        layout = self.layout

        obj = context.object

        row = layout.row()
        self.layout.operator("TEST_OT_Test")



# END     
def register(): bpy.utils.register_module(__name__)
def unregister(): bpy.utils.unregister_module(__name__)


if __name__ == "__main__":
    register()
$\endgroup$
3
  • $\begingroup$ I think it should be more like this: gist.github.com/zeffii/6cff6d0f2d98ccdb97e7 $\endgroup$
    – zeffii
    Jan 14, 2016 at 18:37
  • 1
    $\begingroup$ I pasted the code into the text editor and executed it - it generates the button but when pressing it nothing happens. $\endgroup$ Jan 14, 2016 at 19:27
  • $\begingroup$ yeah, it only works from spacebar as far as i can tell.. (as the file name suggested) $\endgroup$
    – zeffii
    Jan 14, 2016 at 20:29

1 Answer 1

5
$\begingroup$

Here is a corrected version: the Test.define needed different values. Works perfect now - even including calling the transform and waiting till that is done so you script can continue with the next command/macro!

The transformation is started via calling Test.define("TRANSFORM_OT_translate") between the first and last macro step.

import bpy
from bpy.types import Operator, Macro


#End : Snap 3D Cursor and delete empty
class Finalize(Operator):
    bl_idname = "s3dc.finalize"
    bl_label = "Finalize S3DC"

    def execute(self, context):
        bpy.ops.view3d.snap_cursor_to_selected()
        bpy.ops.object.delete();
        print("DONE!")
        return {'FINISHED'}

#Start : Create the empty
class Make(Operator):
    bl_idname = "s3dc.make"
    bl_label = "Make S3DC"

    def execute(self, context):
        bpy.ops.object.select_all(action='DESELECT')       
        bpy.ops.object.empty_add()       
        print('Started')
        return {'FINISHED'}

#Execute : Start the macro sequence
class S3DC(Macro):
    bl_idname = "s3dc.trigger_macro"
    bl_label = "S3DC Macro Sequence"

# UI Button in Tool Shelfe
class ObjectData(bpy.types.Panel):
    bl_label = "Object + Mesh Data"
    bl_idname = "ObjectData"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
    bl_category = "Modeling Toolbox"

    def draw(self, context):
        layout = self.layout
        row = layout.row()
        row.operator("s3dc.trigger_macro", text="Run Set 3D Cursos macro")

# END     
def register(): 
    bpy.utils.register_module(__name__)

    S3DC.define("S3DC_OT_make")
    S3DC.define("TRANSFORM_OT_translate")  # This starts the transformation command 
    S3DC.define("S3DC_OT_finalize")    

def unregister(): 
    bpy.utils.unregister_module(__name__)

if __name__ == "__main__":
    register()
$\endgroup$
9
  • 1
    $\begingroup$ you have called a modal op inside non-modal, so the snapping was done before the end of the transform op ( cursor doesn't move ) $\endgroup$
    – Chebhou
    Jan 15, 2016 at 19:17
  • $\begingroup$ Oh yes true - maybe I made a mistake because the functions where working. I noticed that sometimes you have to restart Blender to flush out some behaviors. $\endgroup$ Jan 16, 2016 at 18:46
  • $\begingroup$ I cannot in the script find any command for the transformation. You added "Test.define("TRANSFORM_OT_translate")". This this line itself call the translate/grab mode? $\endgroup$ Jan 16, 2016 at 18:48
  • $\begingroup$ yes it starts the transform.translate operator $\endgroup$
    – Chebhou
    Jan 16, 2016 at 18:52
  • $\begingroup$ How did you know that? $\endgroup$ Jan 16, 2016 at 19:02

You must log in to answer this question.

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