I want to create locator and move it with my mouse, and eventually correct its position with floatProperty. But I get error in execute : ReferenceError: StructRNA of type Object has been removed What did I miss ? (Blender 2.8)
import bpy
from bpy.props import IntProperty, FloatProperty
class ModalOperator(bpy.types.Operator):
"""Move an object with the mouse, example"""
bl_idname = "object.modal_operator"
bl_label = "Simple Modal Operator"
bl_options = {"REGISTER","UNDO"}
x_translation = FloatProperty()
def execute(self, context):
print (self.locator.name)
self.locator.location.x += self.x_translation
return {'FINISHED'}
def modal(self, context, event):
if event.type == 'MOUSEMOVE':
self.x_translation = (self.first_mouse_x - event.mouse_x) * 0.01
context.object.location.x = self.first_value + self.x_translation
elif event.type == 'LEFTMOUSE':
return {'FINISHED'}
elif event.type in {'RIGHTMOUSE', 'ESC'}:
context.object.location.x = self.first_value
return {'CANCELLED'}
return {'RUNNING_MODAL'}
def invoke(self, context, event):
bpy.ops.object.empty_add(type='ARROWS')
context.active_object.name = "locator_start"
self.locator = context.active_object
print (self.locator.name)
if context.object:
self.first_mouse_x = event.mouse_x
self.first_value = context.object.location.x
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
else:
self.report({'WARNING'}, "No active object, could not finish")
return {'CANCELLED'}
def register():
bpy.utils.register_class(ModalOperator)
def unregister():
bpy.utils.unregister_class(ModalOperator)
if __name__ == "__main__":
register()