1
$\begingroup$

There are two classes A_H1 and A_V1 whose meaning is to add different objects in the top and fronts view respectively, now I want to build a third class A_H1V1 whose role is to execute the contents of the first two classes in order, what if I write the third class and simplify the code?

class A_V1(bpy.types.Operator):
    bl_idname = "align.v1"
    bl_label = "A_V1"
    cam_name = "A_V1"

    def execute(self, context):

        viewmode = "none"

        view_matrix = ((0.41, -0.4017, 0.8188, 0.0),  # This is the view matrix from Factory settings
                       (0.912, 0.1936, -0.3617, 0.0),
                       (-0.0133, 0.8959, 0.4458, 0.0),
                       (0.0, 0.0, -14.9892, 1.0))

        for area in bpy.context.screen.areas:
            if area.type == 'VIEW_3D':
                rv3d = area.spaces[0].region_3d
                if rv3d is not None:
                    # --------------------------- check for views -----------------------------------------------------------
                    # We check if the view is in top, left, etc. by comparing the quaternion of the region3d.view_rotation                    
                    viewrot = area.spaces[0].region_3d.view_rotation
                    viewrot = area.spaces[0].region_3d.view_rotation
                    viewrot = tuple(map(lambda x: round(x, 4), viewrot))  # 使用舍入
                    print(viewrot)

        vlist = [
            ((1.0000,-0.0000,-0.0000,-0.0000), "Top"),
            ((0.7071, 0.7071, -0.0, -0.0), "Front"),]

        for v, vm in vlist:
            if viewrot == v:
                viewmode = vm
                print(viewmode)
                if viewmode == "Top":
                    bpy.ops.mesh.primitive_cube_add()
                if viewmode == "Front":
                    bpy.ops.mesh.primitive_uv_sphere_add()

        return {'FINISHED'}
                
class A_H1(bpy.types.Operator):
    bl_idname = "align.h1"
    bl_label = "A_H1"
    cam_name = "A_H1"

    def execute(self, context):

        viewmode = "none"

        view_matrix = ((0.41, -0.4017, 0.8188, 0.0),  # This is the view matrix from Factory settings
                       (0.912, 0.1936, -0.3617, 0.0),
                       (-0.0133, 0.8959, 0.4458, 0.0),
                       (0.0, 0.0, -14.9892, 1.0))

        for area in bpy.context.screen.areas:
            if area.type == 'VIEW_3D':
                rv3d = area.spaces[0].region_3d
                if rv3d is not None:
                    # --------------------------- check for views -----------------------------------------------------------
                    # We check if the view is in top, left, etc. by comparing the quaternion of the region3d.view_rotation                    
                    viewrot = area.spaces[0].region_3d.view_rotation
                    viewrot = area.spaces[0].region_3d.view_rotation
                    viewrot = tuple(map(lambda x: round(x, 4), viewrot))
                    print(viewrot)

        vlist = [
            ((1.0000,-0.0000,-0.0000,-0.0000), "Top"),
            ((0.7071, 0.7071, -0.0, -0.0), "Front"),]

        for v, vm in vlist:
            if viewrot == v:
                viewmode = vm
                print(viewmode)
                if viewmode == "Top":
                    bpy.ops.mesh.primitive_cylinder_add()
                if viewmode == "Front":
                    bpy.ops.mesh.primitive_cone_add()

        return {'FINISHED'}

class A_H1V1(bpy.types.Operator):
    bl_idname = "align.h1v1"
    bl_label = "A_H1V1"
    cam_name = "A_H1V1"

    def execute(self, context):

         execute class A_H1 and class A_V1 
                    
        return {'FINISHED'}

```
$\endgroup$

1 Answer 1

0
$\begingroup$

i think there is a redundancy problem here, I have no knowledge of adding things to the front view but i can tell that this should not be done in operator classes, it should be 1 operator class that calls plain python functions that receive (self, context etc)

doing so simplifies both registration, future expansions and complexity of the code, regardless of what is done in those functions, the "UI adding logic" should be separated in plain functions that allow for custom return values, expanding the range of what can be done in those, as Operator Types are often limiting when it comes to basically the same process if done in plain py

simply adapting the AV1 and other into functions should do the trick, with an operator passing, receiving and calling all of the "UI elements functions" should do the trick

$\endgroup$

You must log in to answer this question.

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