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.