Advertisement
Guest User

Untitled

a guest
Nov 26th, 2023
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | None | 0 0
  1. import bpy
  2. from math import sqrt
  3. from mathutils import Vector
  4. from random import random
  5.  
  6. class ModalTimerOperator(bpy.types.Operator):
  7.     """Operator which runs its self from a timer"""
  8.     bl_idname = "wm.orbitsim"
  9.     bl_label = "Orbit visualiser"
  10.    
  11.     _obj = None
  12.     _dt = None
  13.     _p = None
  14.     _v = None
  15.     _mu = None
  16.     def modal(self, context, event):
  17.         if event.type in {'RIGHTMOUSE', 'ESC'}:
  18.             print("Stopping script")
  19.             return {'CANCELLED'}
  20.        
  21.         if event.type == 'TIMER':
  22.             a = -self._mu/self._p.magnitude**3*self._p
  23.             self._p = self._p + self._v*self._dt + 0.5*a*self._dt**2
  24.             self._v = self._v + a*self._dt
  25.             self._obj.location = self._p
  26.         return {'PASS_THROUGH'}
  27.  
  28.     def execute(self, context):
  29.         print("NEW RUN")
  30.         self._dt = 0.04
  31.         self._mu = 5
  32.         wm = context.window_manager
  33.         self._timer = wm.event_timer_add(self._dt, window=context.window)
  34.         wm.modal_handler_add(self)
  35.        
  36.         self._p = Vector((10,0,0))
  37. #Circular orbit:
  38.         #self._v = sqrt(self._mu/self._p.magnitude)*Vector((random(),random(),random())).cross(self._p).normalized()
  39. #Elliptic orbit:
  40.         semi_major = (random()+0.6)*self._p.magnitude
  41.         self._v = sqrt(self._mu*(2/self._p.magnitude-1/semi_major))*Vector((random(),random(),random())).cross(self._p).normalized()
  42.        
  43.         self._obj = bpy.data.objects['Cube']
  44.         self._obj.location = self._p
  45.        
  46.         return {'RUNNING_MODAL'}
  47.    
  48.  
  49.     def cancel(self, context):
  50.         wm = context.window_manager
  51.         wm.event_timer_remove(self._timer)
  52.  
  53. def register():
  54.     bpy.utils.register_class(ModalTimerOperator)
  55.  
  56.  
  57. def unregister():
  58.     bpy.utils.unregister_class(ModalTimerOperator)
  59.  
  60. if __name__ == "__main__":
  61.     register()
  62.     bpy.ops.wm.orbitsim()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement