Python bpy.types 模块,Operator() 实例源码

我们从Python开源项目中,提取了以下19个代码示例,用于说明如何使用bpy.types.Operator()

项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def invoke(self, context, event):
        wm = context.window_manager

        data_path = self.data_path
        prop_string = self.prop_string

        # same as eval("bpy.ops." + data_path)
        op_mod_str, ob_id_str = data_path.split(".", 1)
        op = getattr(getattr(bpy.ops, op_mod_str), ob_id_str)
        del op_mod_str, ob_id_str

        try:
            op_rna = op.get_rna()
        except KeyError:
            self.report({'ERROR'}, "Operator not found: bpy.ops.%s" % data_path)
            return {'CANCELLED'}

        def draw_cb(self, context):
            layout = self.layout
            pie = layout.menu_pie()
            pie.operator_enum(data_path, prop_string)

        wm.popup_menu_pie(draw_func=draw_cb, title=op_rna.bl_rna.name, event=event)

        return {'FINISHED'}
项目:archipack    作者:s-leger    | 项目源码 | 文件源码
def execute(self, context):
        d = archipack_fence.datablock(context.active_object)

        if d is None:
            self.report({'WARNING'}, "Archipack: Operator only valid with fence")
            return {'CANCELLED'}

        if self.part == "SUB":
            part_obj = bpy.data.objects.get(d.user_defined_subs)
            if part_obj is None:
                self.report({'WARNING'}, "Archipack: User defined sub object not found")
                return {'CANCELLED'}
            d.subs_x, d.subs_y, d.subs_z = part_obj.dimensions.x, part_obj.dimensions.y, part_obj.dimensions.z
        else:
            part_obj = bpy.data.objects.get(d.user_defined_post)
            if part_obj is None:
                self.report({'WARNING'}, "Archipack: User defined post object not found")
                return {'CANCELLED'}
            d.post_x, d.post_y, d.post_z = part_obj.dimensions.x, part_obj.dimensions.y, part_obj.dimensions.z

        return {'FINISHED'}
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def menu_func_import(self, context):
    self.layout.operator(ImportSomeData.bl_idname, text="Text Import Operator")
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def menu_func_export(self, context):
    self.layout.operator(ExportSomeData.bl_idname, text="Text Export Operator")
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def draw(self, context):
        layout = self.layout
        space = bpy.context.space_data
        if not space.local_view:
            # Imperial units warning
            if bpy.context.scene.unit_settings.system == "IMPERIAL":
                row = layout.row()
                row.label("Warning: Imperial units not supported", icon='COLOR_RED')

            box = layout.box()
            row = box.row()
            row.prop(self, 'width')
            row.prop(self, 'height')

            box = layout.box()
            if not context.scene.render.engine == 'CYCLES':
                box.enabled = False
            box.prop(self, 'crt_mat')
            if self.crt_mat:
                box.label("* Remember to verify fabric texture folder")
        else:
            row = layout.row()
            row.label("Warning: Operator does not work in local view mode", icon='ERROR')

    # -----------------------------------------------------
    # Execute
    # -----------------------------------------------------
    # noinspection PyUnusedLocal
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def error_handlers(self, op_name, error, reports="ERROR", func=False):
    if self and reports:
        self.report({'WARNING'}, reports + " (See Console for more info)")

    is_func = "Function" if func else "Operator"
    print("\n[Btrace]\n{}: {}\nError: {}\n".format(op_name, is_func, error))


# Object Trace
# creates a curve with a modulated radius connecting points of a mesh
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def create_mix_node(TreeNodes, links, nodes, loc, start, median_point, row, frame):
    mix_node = TreeNodes.nodes.new('ShaderNodeMixRGB')
    mix_node.name = "MIX level: " + str(loc)
    mix_node.label = "MIX level: " + str(loc)
    mix_node.use_custom_color = True
    mix_node.color = NODE_COLOR_MIX
    mix_node.hide = True
    mix_node.width_hidden = 75

    if frame:
        mix_node.parent = frame
    mix_node.location = -(start - loc * 175), ((median_point / 4) + (row * 50))

    try:
        if len(nodes) > 1:
            links.new(nodes[0].outputs[0], mix_node.inputs["Color2"])
            links.new(nodes[1].outputs[0], mix_node.inputs["Color1"])
        elif len(nodes) == 1:
            links.new(nodes[0].outputs[0], mix_node.inputs["Color1"])
    except:
        collect_report("ERROR: Link failed for mix node {}".format(mix_node.label))
    return mix_node


# -----------------------------------------------------------------------------
# Operator Classes
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def preset_values(self):
        properties_blacklist = Operator.bl_rna.properties.keys()

        prefix, suffix = self.operator.split("_OT_", 1)
        op = getattr(getattr(bpy.ops, prefix.lower()), suffix)
        operator_rna = op.get_rna().bl_rna
        del op

        ret = []
        for prop_id, prop in operator_rna.properties.items():
            if not (prop.is_hidden or prop.is_skip_save):
                if prop_id not in properties_blacklist:
                    ret.append("op.%s" % prop_id)

        return ret
项目:fujiwara_toolbox    作者:YusukeFujiwara    | 项目源码 | 文件源码
def get_pref():
    addon_prefs = bpy.context.user_preferences.addons[__package__].preferences
    return addon_prefs


# class OBJECT_OT_addon_prefs_example(Operator):
#     """Display example preferences"""
#     bl_idname = "object.addon_prefs_example"
#     bl_label = "Addon Preferences Example"
#     bl_options = {'REGISTER', 'UNDO'}

#     def execute(self, context):
#         user_preferences = context.user_preferences
#         addon_prefs = user_preferences.addons[prefname].preferences

#         info = ("Path: %s, Number: %d, Boolean %r" %
#                 (addon_prefs.filepath, addon_prefs.number, addon_prefs.boolean))

#         self.report({'INFO'}, info)
#         print(info)

#         return {'FINISHED'}




#maintools
项目:blender_addons_collection    作者:manorius    | 项目源码 | 文件源码
def draw(self, context):
    '''
      Operator options.
    '''

    # layout
    layout = self.layout

    # extend
    layout.prop(self, 'extend')

    # view
    layout.prop(self, 'view')

  # execute
项目:ToolPlus    作者:mkbreuer    | 项目源码 | 文件源码
def separate():
    context = bpy.context
    sce = context.scene
    obj = context.active_object


    def object_duplicate(ob):
        ops_ob = bpy.ops.object
        ops_ob.select_all(action="DESELECT")
        ops_ob.select_pattern(pattern=ob.name)
        ops_ob.duplicate()
        return context.selected_objects[0]


    object_prepare()

    obj.select = False
    ob = context.selected_objects[0]

    obj_copy = object_duplicate(obj)
    ob_copy = object_duplicate(ob)

    mode = 'INTERSECT'
    mesh_selection(obj_copy, 'SELECT')
    mesh_selection(ob, 'DESELECT')
    sce.objects.active = ob
    modifier_boolean(ob, obj_copy, mode)

    mode = 'DIFFERENCE'
    mesh_selection(ob_copy, 'SELECT')
    mesh_selection(obj, 'DESELECT')
    sce.objects.active = obj
    modifier_boolean(obj, ob_copy, mode)
    obj.select = True



### Operator ###
项目:ToolPlus    作者:mkbreuer    | 项目源码 | 文件源码
def separate():
    context = bpy.context
    sce = context.scene
    obj = context.active_object


    def object_duplicate(ob):
        ops_ob = bpy.ops.object
        ops_ob.select_all(action="DESELECT")
        ops_ob.select_pattern(pattern=ob.name)
        ops_ob.duplicate()
        return context.selected_objects[0]


    object_prepare()

    obj.select = False
    ob = context.selected_objects[0]

    obj_copy = object_duplicate(obj)
    ob_copy = object_duplicate(ob)

    mode = 'INTERSECT'
    mesh_selection(obj_copy, 'SELECT')
    mesh_selection(ob, 'DESELECT')
    sce.objects.active = ob
    modifier_boolean(ob, obj_copy, mode)

    mode = 'DIFFERENCE'
    mesh_selection(ob_copy, 'SELECT')
    mesh_selection(obj, 'DESELECT')
    sce.objects.active = obj
    modifier_boolean(obj, ob_copy, mode)
    obj.select = True



### Operator ###
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def draw(self, context):
        layout = self.layout
        space = bpy.context.space_data
        if not space.local_view:
            # Imperial units warning
            if bpy.context.scene.unit_settings.system == "IMPERIAL":
                row = layout.row()
                row.label("Warning: Imperial units not supported", icon='COLOR_RED')

            box = layout.box()
            row = box.row()
            row.prop(self, 'model')
            if self.model == "2":
                row.prop(self, 'radio')

            box.prop(self, 'step_num')
            row = box.row()
            row.prop(self, 'max_width')
            row.prop(self, 'depth')
            row.prop(self, 'shift')
            row = box.row()
            row.prop(self, 'back')
            row.prop(self, 'sizev')
            row = box.row()
            row.prop(self, 'curve')
            # all equal
            if self.sizev is True:
                row.prop(self, 'min_width')

            box = layout.box()
            row = box.row()
            row.prop(self, 'thickness')
            row.prop(self, 'height')
            row = box.row()
            row.prop(self, 'front_gap')
            if self.model == "1":
                row.prop(self, 'side_gap')

            box = layout.box()
            if not context.scene.render.engine == 'CYCLES':
                box.enabled = False
            box.prop(self, 'crt_mat')
        else:
            row = layout.row()
            row.label("Warning: Operator does not work in local view mode", icon='ERROR')

    # -----------------------------------------------------
    # Execute
    # -----------------------------------------------------
    # noinspection PyUnusedLocal
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def draw(self, context):
        o = context.object
        # noinspection PyBroadException
        try:
            if 'VenetianObjectGenerator' not in o:
                return
        except:
            return

        layout = self.layout
        if bpy.context.mode == 'EDIT_MESH':
            layout.label('Warning: Operator does not work in edit mode.', icon='ERROR')
        else:
            myobjdat = o.VenetianObjectGenerator[0]
            space = bpy.context.space_data
            if not space.local_view:
                # Imperial units warning
                if bpy.context.scene.unit_settings.system == "IMPERIAL":
                    row = layout.row()
                    row.label("Warning: Imperial units not supported", icon='COLOR_RED')

            box = layout.box()
            row = box.row()
            row.prop(myobjdat, 'width')
            row.prop(myobjdat, 'height')
            row.prop(myobjdat, 'depth')
            row = box.row()
            row.prop(myobjdat, 'angle', slider=True)
            row.prop(myobjdat, 'ratio', slider=True)

            box = layout.box()
            if not context.scene.render.engine == 'CYCLES':
                box.enabled = False
            box.prop(myobjdat, 'crt_mat')
            if myobjdat.crt_mat:
                row = box.row()
                row.prop(myobjdat, 'objcol')
            else:
                row = layout.row()
                row.label("Warning: Operator does not work in local view mode", icon='ERROR')


# ------------------------------------------------------------------------------
# Create rectangular base
#
# x: size x axis
# y: size y axis
# z: size z axis
# ------------------------------------------------------------------------------
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def draw(self, context):
        layout = self.layout
        space = bpy.context.space_data
        if not space.local_view:
            # Imperial units warning
            if bpy.context.scene.unit_settings.system == "IMPERIAL":
                row = layout.row()
                row.label("Warning: Imperial units not supported", icon='COLOR_RED')
            box = layout.box()
            box.prop(self, 'model')
            box.prop(self, 'roof_width')
            box.prop(self, 'roof_height')
            box.prop(self, 'roof_scale')

            tilesize_x = 0
            tilesize_y = 0
            if self.model == "1":
                tilesize_x = 0.184
                tilesize_y = 0.413

            if self.model == "2":
                tilesize_x = 0.103
                tilesize_y = 0.413

            if self.model == "3":
                tilesize_x = 0.184
                tilesize_y = 0.434

            if self.model == "4":
                tilesize_x = 0.231
                tilesize_y = 0.39

            x = tilesize_x * self.roof_scale * self.roof_width
            y = tilesize_y * self.roof_scale * self.roof_height

            buf = 'Size: {0:.2f} * {1:.2f} aprox.'.format(x, y)
            box.label(buf)

            box = layout.box()
            box.prop(self, 'roof_thick')
            box.prop(self, 'roof_angle')

            box = layout.box()
            if not context.scene.render.engine == 'CYCLES':
                box.enabled = False
            box.prop(self, 'crt_mat')
        else:
            row = layout.row()
            row.label("Warning: Operator does not work in local view mode", icon='ERROR')

    # -----------------------------------------------------
    # Execute
    # -----------------------------------------------------
    # noinspection PyUnusedLocal
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def draw(self, context):
        o = context.object
        # If the selected object didn't be created with the group 'RoomGenerator', this panel is not created.
        # noinspection PyBroadException
        try:
            if 'RoomGenerator' not in o:
                return
        except:
            return

        layout = self.layout
        if bpy.context.mode == 'EDIT_MESH':
            layout.label('Warning: Operator does not work in edit mode.', icon='ERROR')
        else:
            room = o.RoomGenerator[0]
            row = layout.row()
            row.prop(room, 'room_height')
            row.prop(room, 'wall_width')
            row.prop(room, 'inverse')

            row = layout.row()
            if room.wall_num > 1:
                row.prop(room, 'ceiling')
                row.prop(room, 'floor')
                row.prop(room, 'merge')

            # Wall number
            row = layout.row()
            row.prop(room, 'wall_num')

            # Add menu for walls
            if room.wall_num > 0:
                for wall_index in range(0, room.wall_num):
                    box = layout.box()
                    add_wall(wall_index + 1, box, room.walls[wall_index])

            box = layout.box()
            box.prop(room, 'baseboard')
            if room.baseboard is True:
                row = box.row()
                row.prop(room, 'base_width')
                row.prop(room, 'base_height')

            box = layout.box()
            box.prop(room, 'shell')
            if room.shell is True:
                row = box.row()
                row.prop(room, 'shell_height')
                row.prop(room, 'shell_thick')
                row = box.row()
                row.prop(room, 'shell_factor', slider=True)
                row.prop(room, 'shell_bfactor', slider=True)

            box = layout.box()
            if not context.scene.render.engine == 'CYCLES':
                box.enabled = False
            box.prop(room, 'crt_mat')
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def draw(self, context):
        layout = self.layout
        space = bpy.context.space_data
        if not space.local_view:
            # Imperial units warning
            if bpy.context.scene.unit_settings.system == "IMPERIAL":
                row = layout.row()
                row.label("Warning: Imperial units not supported", icon='COLOR_RED')

            box = layout.box()
            row = box.row()
            row.prop(self, 'type_cabinet')

            row.prop(self, 'thickness')
            row = box.row()
            row.prop(self, 'depth')
            row.prop(self, 'height')
            row = box.row()
            row.prop(self, 'handle')
            if self.handle != "9":
                row.prop(self, 'handle_x')
                row.prop(self, 'handle_z')

            if self.type_cabinet == "1":
                row = box.row()
                row.prop(self, "countertop")
                if self.countertop:
                    row.prop(self, "counterheight")
                    row.prop(self, "counterextend")
                row = box.row()
                row.prop(self, 'baseboard')
                if self.baseboard:
                    row.prop(self, 'baseheight')
                    row.prop(self, 'basefactor', slider=True)

            row = box.row()
            row.prop(self, 'fitZ')
            if self.type_cabinet == "2":
                row.prop(self, 'moveZ')

            # Cabinet number
            row = box.row()
            row.prop(self, 'cabinet_num')
            # Add menu for cabinets
            if self.cabinet_num > 0:
                for idx in range(0, self.cabinet_num):
                    box = layout.box()
                    add_cabinet(self, box, idx + 1, self.cabinets[idx])

            box = layout.box()
            if not context.scene.render.engine == 'CYCLES':
                box.enabled = False
            box.prop(self, 'crt_mat')
        else:
            row = layout.row()
            row.label("Warning: Operator does not work in local view mode", icon='ERROR')

    # -----------------------------------------------------
    # Execute
    # -----------------------------------------------------
    # noinspection PyUnusedLocal
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def draw(self, context):
        layout = self.layout
        space = bpy.context.space_data
        if not space.local_view:
            # Imperial units warning
            if bpy.context.scene.unit_settings.system == "IMPERIAL":
                row = layout.row()
                row.label("Warning: Imperial units not supported", icon='COLOR_RED')

            box = layout.box()
            row = box.row()
            row.prop(self, 'thickness')
            row.prop(self, 'sthickness')
            row = box.row()
            row.prop(self, 'depth')
            row.prop(self, 'height')
            row = box.row()
            row.prop(self, 'top')
            row.prop(self, 'bottom')
            row = box.row()
            row.prop(self, 'stype')
            row.prop(self, 'fitZ')

            # Furniture number
            row = layout.row()
            row.prop(self, 'shelves_num')
            # Add menu for shelves
            if self.shelves_num > 0:
                for idx in range(0, self.shelves_num):
                    box = layout.box()
                    add_shelves(self, box, idx + 1, self.shelves[idx])

            box = layout.box()
            if not context.scene.render.engine == 'CYCLES':
                box.enabled = False
            box.prop(self, 'crt_mat')
        else:
            row = layout.row()
            row.label("Warning: Operator does not work in local view mode", icon='ERROR')

    # -----------------------------------------------------
    # Execute
    # -----------------------------------------------------
    # noinspection PyUnusedLocal
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def draw(self, context):
        layout = self.layout
        space = bpy.context.space_data
        if not space.local_view:
            # Imperial units warning
            if bpy.context.scene.unit_settings.system == "IMPERIAL":
                row = layout.row()
                row.label("Warning: Imperial units not supported", icon='COLOR_RED')

            box = layout.box()
            row = box.row()
            row.prop(self, 'width')
            row.prop(self, 'height')
            row = box.row()
            row.prop(self, 'num')
            row.prop(self, 'palnum')

            if self.num >= 1:
                row = box.row()
                row.prop(self, 'open01', slider=True)
            if self.num >= 2:
                row = box.row()
                row.prop(self, 'open02', slider=True)
            if self.num >= 3:
                row = box.row()
                row.prop(self, 'open03', slider=True)
            if self.num >= 4:
                row = box.row()
                row.prop(self, 'open04', slider=True)
            if self.num >= 5:
                row = box.row()
                row.prop(self, 'open05', slider=True)

            box = layout.box()
            if not context.scene.render.engine == 'CYCLES':
                box.enabled = False
            box.prop(self, 'crt_mat')
            if self.crt_mat:
                box.label("* Remember to verify fabric texture folder")
        else:
            row = layout.row()
            row.label("Warning: Operator does not work in local view mode", icon='ERROR')

    # -----------------------------------------------------
    # Execute
    # -----------------------------------------------------
    # noinspection PyUnusedLocal