Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
"scene.gravity" and orientation
-
I'm still kinda wondering about the
scene.gravity
. I mentioned something about it here. How can I get thescene.gracity
to follow my device's orientation? -
scene.gravity
is deprecated - you should usemotion.get_gravity
instead, which is independent of thescene
module.Basic usage looks like this:
import motion motion.start_updates() print(motion.get_gravity()) motion.stop_updates()
All code that makes use of motion data needs to go between
start_updates
andstop_updates
. If the code inside might raise an exception, you should put the entire thing in atry
-finally
block to make sure that motion updates are stopped so your battery doesn't drain too quickly.import motion motion.start_updates() try: pass # Do motion things finally: motion.stop_updates()
The
try
block will not catch any exceptions, it only ensures that thefinally
block is run whether an exception is raised or not. -
I'm having the same issues with the motion module as well...
I'm relying on the accelerometer to move around in my game, and I need to get the
motion.get_gravity
relative to the device orientation. -
You will have to convert the values yourself. It shouldn't be that hard, if I'm not mistaken – you'd just have to swap the x and y coordinates.
-
That's what I've been doing right now. Thanks for the answers guys!
Using the following code, I managed to see which orientation is being used.# coding: utf-8 import scene import motion class MyScene (scene.Scene): def setup(self): self.label_node = scene.LabelNode('', ('Arial', 12), position=self.size*0.5, parent=self) motion.start_updates() self.orientation = '?' def update(self): x, y, z = motion.get_gravity() if abs(x) > abs(y): if x > 0: self.label_node.text = 'LANDSCAPE, RIGHT' else: self.label_node.text = 'LANDSCAPE, LEFT' else: if y < 0: self.label_node.text = 'PORTRAIT' def did_change_size(self): self.label_node.position = self.size*0.5 def stop(self): motion.stop_updates() scene.run(MyScene())