How Use Macro in 2.8?


#1

How Use Macro in 2.8?
I want my script to work for 2.8
https://pastebin.com/BmYH6wVX


#2

Who can give me an example?


#3

You can register an empty Macro first, then define the operators during registration.

Here’s an example with 2 operators in script plus an internal operator. This will print “Hello” and “World” from each operator, and select all objects using the idname of bpy.ops.object.select_all().

import bpy

class BarOperator(bpy.types.Operator):
    bl_idname =  "wm.bar_operator"
    bl_label = "Bar Operator"

    def execute(self, context):
        print("Hello")
        return {'FINISHED'}
        
class BazOperator(bpy.types.Operator):
    bl_idname =  "wm.baz_operator"
    bl_label = "Baz Operator"
    
    def execute(self, context):
        print("World")
        return {'FINISHED'}
        
class FooMacro(bpy.types.Macro): # <<- empty macro
    bl_idname = "wm.foo_macro"
    bl_label = "Foo Macro"

classes = (BarOperator, BazOperator, FooMacro)
    
def register():
    for c in classes:
        bpy.utils.register_class(c)
    FooMacro.define("WM_OT_bar_operator")
    FooMacro.define("WM_OT_baz_operator")
    FooMacro.define("OBJECT_OT_select_all")
    
def unregister():
    for c in reversed(classes):
        bpy.utils.unregister_class(c)

if __name__ == "__main__":
    register()

#4

Hello! your answer was correct thanks, but I ran into another problem. My macro does not want to call view3d.select_box via hotkey. At that time, if you call it through the search menu, everything works. Maybe you can tell me what the problem is.
Code https://github.com/Darcvizer/Border-Occlusion/blob/master/BorderOcclusion28.py

Can you tell me how to make OcclusionMacro.define (“view3d.select_lasso”) or OcclusionMacro.define (“view3d.select_box”) called depending on the global variable?


#5

They need to use the fully qualified ‘idname’:

def register():
	global Mode
	global ModS
	for c in classes:
		bpy.utils.register_class(c)
	OcclusionMacro.define("VIEW3D_OT_border_on")
#	#OcclusionMacro.define("view3d.select_lasso")
	OcclusionMacro.define("VIEW3D_OT_select_box")
	OcclusionMacro.define('VIEW3D_OT_border_off')

Macros aren’t flexible with program flow. They don’t have a useful method for this and items in the macro can’t be changed without re-registering the class.

For this maybe it’s better to make a new operator that invokes the appropriate box/lasso depending on the global variable or property. Then use this new operator when you define during registration instead of box/lasso.


#6

Thank you very much! Works great !
if you’re interested