0
$\begingroup$

In my Python script, I would like to use the selected object to choose a method from the appropriate class. Here is an example that explains what I want to do:

import bpy

class CheckShape(bpy.types.Operator):
    bl_idname = "myops.check_shape"
    bl_label = "Check Shape" 

    def execute(self,context):
       obj=bpy.context.active_object
       print("what shape is this?")
       obj.check_shape()
       return {'FINISHED'} 

class DrawCube(bpy.types.Operator):
    bl_idname = "myops.draw_cube"
    bl_label = "Draw Cube" 

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

    def check_shape(self,context):
        print ("a cube")
        return {'FINISHED'}

class DrawCylinder(bpy.types.Operator):
    bl_idname = "myops.draw_cylinder"
    bl_label = "Draw Cylinder" 

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

    def check_shape(self,context):
        print ("a cylinder")
        return {'FINISHED'}          
   
classes = (
    CheckShape,
    DrawCube,
    DrawCylinder)


def register():
    for cls in classes:
        bpy.utils.register_class(cls)

def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)

if __name__ == "__main__":
    register()

So, if I draw a cube with the operator bpy.ops.myops.draw_cube(), I would like to be able to run other methods in the same class when I select the cube. For instance, I would like the operator bpy.ops.myops.check_shape() to call the method in the appropriate class depending on the selected object. When my myops.check_shape() operator is called, I get the following error message: AttributeError: 'Object' object has no attribute 'check_shape' Is it possible to fix my example so that it would work?

Also, is there an update function of some sort that will be run if the cube created with the custom operator is transformed?

Thanks.

$\endgroup$
2

1 Answer 1

0
$\begingroup$

I am trying to write a Python code to save a 3-d model created in Blender so that a simulation program can read it. In order to save the data from the different types of objects, I have a list of if statements. I was thinking I can avoid that if I could take advantage of the fact that Python is an object-oriented language and each object could have its own method to save data.

After reading the following question and answer, Is it possible to create an own object data type through Python? I think that the answer to my question is no, that will not work. I may try to use a custom property to store the information I need for each object when I create it. Then I could just get the value of the custom property instead of having a bunch of if statements.

I think custom properties have an update function, so that if the object is modified, I could catch that in my Python code and adjust the custom property. Since I am only creating 3d models and not using other aspects of Blender, hopefully a custom property would not cause problems with memory use and execution time in Blender.

$\endgroup$

You must log in to answer this question.

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