| 
									
										
										
										
											2010-07-08 16:24:24 +00:00
										 |  |  | import bpy | 
					
						
							| 
									
										
										
										
											2011-02-27 15:25:24 +00:00
										 |  |  | from bpy.props import IntProperty, FloatProperty | 
					
						
							| 
									
										
										
										
											2010-02-21 16:20:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-26 07:54:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-21 16:20:32 +00:00
										 |  |  | class ModalOperator(bpy.types.Operator): | 
					
						
							| 
									
										
										
										
											2012-07-01 07:55:44 +00:00
										 |  |  |     """Move an object with the mouse, example""" | 
					
						
							| 
									
										
										
										
											2010-02-21 16:20:32 +00:00
										 |  |  |     bl_idname = "object.modal_operator" | 
					
						
							|  |  |  |     bl_label = "Simple Modal Operator" | 
					
						
							| 
									
										
										
										
											2010-02-22 23:32:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-21 16:20:32 +00:00
										 |  |  |     first_mouse_x = IntProperty() | 
					
						
							|  |  |  |     first_value = FloatProperty() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def modal(self, context, event): | 
					
						
							|  |  |  |         if event.type == 'MOUSEMOVE': | 
					
						
							| 
									
										
										
										
											2010-09-09 18:03:57 +00:00
										 |  |  |             delta = self.first_mouse_x - event.mouse_x | 
					
						
							|  |  |  |             context.object.location.x = self.first_value + delta * 0.01 | 
					
						
							| 
									
										
										
										
											2010-02-21 16:20:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         elif event.type == 'LEFTMOUSE': | 
					
						
							|  |  |  |             return {'FINISHED'} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-08 05:21:37 +00:00
										 |  |  |         elif event.type in {'RIGHTMOUSE', 'ESC'}: | 
					
						
							| 
									
										
										
										
											2010-09-09 18:03:57 +00:00
										 |  |  |             context.object.location.x = self.first_value | 
					
						
							| 
									
										
										
										
											2010-02-21 16:20:32 +00:00
										 |  |  |             return {'CANCELLED'} | 
					
						
							| 
									
										
										
										
											2010-02-22 23:32:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-21 16:20:32 +00:00
										 |  |  |         return {'RUNNING_MODAL'} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def invoke(self, context, event): | 
					
						
							|  |  |  |         if context.object: | 
					
						
							| 
									
										
										
										
											2010-09-09 18:03:57 +00:00
										 |  |  |             self.first_mouse_x = event.mouse_x | 
					
						
							|  |  |  |             self.first_value = context.object.location.x | 
					
						
							| 
									
										
										
										
											2012-09-05 00:11:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             context.window_manager.modal_handler_add(self) | 
					
						
							| 
									
										
										
										
											2010-02-21 16:20:32 +00:00
										 |  |  |             return {'RUNNING_MODAL'} | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.report({'WARNING'}, "No active object, could not finish") | 
					
						
							|  |  |  |             return {'CANCELLED'} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-12 08:04:32 +00:00
										 |  |  | def register(): | 
					
						
							|  |  |  |     bpy.utils.register_class(ModalOperator) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def unregister(): | 
					
						
							|  |  |  |     bpy.utils.unregister_class(ModalOperator) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-21 16:20:32 +00:00
										 |  |  | if __name__ == "__main__": | 
					
						
							| 
									
										
										
										
											2011-02-12 08:04:32 +00:00
										 |  |  |     register() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # test call | 
					
						
							| 
									
										
										
										
											2011-08-10 09:16:35 +00:00
										 |  |  |     bpy.ops.object.modal_operator('INVOKE_DEFAULT') |