Blender: Walk around sphere - python

In order to understand blender python game scripting, I currently try to build a scene in which one can walk around a sphere, using the FPSController structure from this link. For gravity and FPSController orientation I tried to construct a python Controller, which currently looks like this:
def main():
print("Started")
controller = bge.logic.getCurrentController()
me = controller.owner
distance, loc, glob = me.getVectTo((0,0,0))
grav = controller.actuators['Gravity']
strength = me['Gravity']
force = strength*(distance*distance)*glob
grav.force = force
try:
rot = Vector((0,0,-1)).rotation_difference(glob).to_matrix()
except Exception as E:
print(E)
rot = (0,0,0)
rotZ = me.orientation
me.orientation = rot*rotZ
controller.activate(grav)
main()
which roughly works until any angle goes over 180 degrees, and looks discontinuous then. I assume this comes from rotation_difference being discontinuous – blender documentation on Math Types & Utilities does not say anything, and I have not thought enough about quaternionic representations yet to see if a continuous map would be possible – and I guess there is a more elegant way to achieve that the local Z orientation is continuously mouse-dependent, while local X and Y orientations depend continuously on some given vector, but how?

The consensus seems to be that you should accomplish such rotations using quaternions.
See this for the api: http://www.blender.org/documentation/249PythonDoc/Mathutils.Quaternion-class.html
See this for an introduction to the maths: http://en.wikipedia.org/wiki/Rotation_formalisms_in_three_dimensions#Quaternions

There is a allign-function. If the game-object is called own it should be something like own.alignAxisToVect(vector, 2, 1) with 2 being the index for the Z-axis(x=0,y=1,z=2) and 1 being the speed of allignment (between 0 and 1)

Related

math domain error doesnt add up using various trig functions with variable

so I am new to python, im using it for a couple classes, I am making programs to run calculations. here I have my code for 3 dimensional vectors, I ran into an issue however when I input a vector with Fx=0 Fy=-6 and Fz=8, it gave me a math domain error when computing the variable s. I tried the math by hand, works fine, had it print all the variables used for s just prior to the error and did the line of math code by hand using the numbers and it checks out, and it works for just about every other 3d vector ive thrown at it, im not sure why it crashes there. also if you have any advice on how to do it better, I am all ears (though I appreciate any links to code or pips to import, I have to write the code itself to be able to use it for class).
code below:
#blue triangle
#if else
import math
a=input('what is the altitude angle theta z:')
s=input('what is the swing angle phi:')
F=input('what is the magnitude:')
Fx=input('what is the Fx component:')
Fy=input('what is the Fy component:')
Fz=input('what is the Fz component:')
#Blue triangle functions (Magnitude, Theta z, phi)
if a !='' and s!='' and F!='':
print('---------------------------------------------------------------------------------------')
#a = input('what is the altitude angle theta z:')
#s = input('what is the swing angle phi:')
#F = input('what is the magnitude:')
a=float(a)
s=float(s)
F=float(F)
Sa = math.sin(math.radians(a))
Ca = math.cos(math.radians(a))
Ss = math.sin(math.radians(s))
Cs = math.cos(math.radians(s))
Fx = F*Sa*Cs
Fy = F*Sa*Ss
Fz = F*Ca
print(' Fx=',Fx)
print(' Fy=',Fy)
print(' Fz=',Fz)
print(' Unit vector <',round(Fx/F,4), round(Fy/F,4), round(Fz/F,4), '>')
Fx=''
#Blue triangle functions (Fx,Fy,Fz)
if Fx!='' and Fy!='' and Fz!='':
print('---------------------------------------------------------------------------------------')
Fx=float(Fx)
Fy=float(Fy)
Fz=float(Fz)
F=math.sqrt((Fx**2)+(Fy**2)+(Fz**2))
a=math.degrees(math.acos(Fz/F))
s=math.degrees(math.asin((Fy/(F*math.sin(math.radians(a))))))
print(' Force=',F)
print(' Altitude of theta z=',a)
print(' planar swing angle phi=',s)
print(' Unit vector <',round(Fx/F,4), round(Fy/F,4), round(Fz/F,4), '>')
print('done')
The error you are getting comes from floating point math being inexact - as documented in the official python docs too
If you break your statement up into a few parts and print the intermediate arguments you can see 1) which command the error is from, and 2) what the input values are for that command.
arg = Fy/(F*math.sin(math.radians(a)))
print(arg)
arg2 = math.asin(arg) # the error is on this line
s = math.degrees(arg2)
Analytically, arg should be exactly -1 here, but when you print it you will see it has a value of -1.0000000000000002 (see the link above about floating point math being inexact...). Since you cannot take the asin of a number > 1 or < -1, you get a math domain error. One solution would just be to clip it to a valid range before passing it to the asin function.
arg = Fy/(F*math.sin(math.radians(a)))
arg_clipped = max([-1, min([1, arg])])
arg2 = math.asin(arg_clipped)
s = math.degrees(arg2)
Most of the time the clipping won't do anything, except for these corner cases where you are at exact right angles and at argument values of exactly 1 or -1.
I would also recommend keeping things in radians through the calculations, and if you need to show the value in degrees just convert it to degrees at the point where you print it.
See also Accurate trig in python and Python cosine function precision.

Colour Difference in The Foundry NUKE

So I am trying to make a painterly node group with Python code straight so I can use it for future projects but I can't seem to get the power part of the formula in nuke to work from this colour difference formula( I'm also new to Nuke so if there is a better way of writing this please let me know it would be awesome thank you, or if I'm doing this wrong completely also let me know)
The following formula for color difference is used to create the
difference image: |(r1,g1,b1) – (r2,g2,b2)| = ((r1 – r2)^2 + (g1
–g2)^2 + (b1 – b2)^2)^1/2.
nRedShuffle = nuke.nodes.Shuffle()
nRedShuffle['red'].setValue('red')
nRedShuffle['green'].setValue('red')
nRedShuffle['blue'].setValue('red')
nRedShuffle['alpha'].setValue('red')
nGreenShuffle = nuke.nodes.Shuffle()
nGreenShuffle['red'].setValue('green')
nGreenShuffle['green'].setValue('green')
nGreenShuffle['blue'].setValue('green')
nGreenShuffle['alpha'].setValue('green')
#...(so on for the rest of rgba1 and rgba2)
nGreenShuffle2 = nuke.nodes.Shuffle()
nGreenShuffle2['red'].setValue('green')
nGreenShuffle2['green'].setValue('green')
nGreenShuffle2['blue'].setValue('green')
nGreenShuffle2['alpha'].setValue('green')
nBlueShuffle2 = nuke.nodes.Shuffle()
nBlueShuffle2['red'].setValue('blue')
nBlueShuffle2['green'].setValue('blue')
nBlueShuffle2['blue'].setValue('blue')
nBlueShuffle2['alpha'].setValue('blue')
#I am having troubles with the powers below
redDiff = nuke.nodes.Merge2(operation='minus', inputs=[nRedShuffle2, nRedShuffle])
redDiffMuli = nuke.nodes.Merge2(operation='multiply', inputs=[redDiff, redDiff])
greenDiff = nuke.nodes.Merge2(operation='minus', inputs=[nGreenShuffle2, nGreenShuffle])
greenDiffMuli = nuke.nodes.Merge2(operation='multiply', inputs=[greenDiff, greenDiff])
blueDiff = nuke.nodes.Merge2(operation='minus', inputs=[nBlueShuffle2, nBlueShuffle])
blueDiffMuli = nuke.nodes.Merge2(operation='multiply', inputs=[blueDiff, blueDiff])
redGreenAdd = nuke.nodes.Merge2(operation='plus', inputs=[redDiffMuli, greenDiffMuli])
redGreenBlueAdd = nuke.nodes.Merge2(operation='plus', inputs=[redGreenAdd, blueDiffMuli])
Here are at least two ways to implement Color Difference formula for two images. You can use difference op in Merge node or you can write a formula in field for each channel inside MergeExpression node:
Expression for each channel is as simple as this:
abs(Ar-Br)
abs(Ag-Bg)
abs(Ab-Bb)
Python commands
You can use .nodes.MergeExpression methodology:
import nuke
merge = nuke.nodes.MergeExpression(expr0='abs(Ar-Br)',
expr1='abs(Ag-Bg)',
expr2='abs(Ab-Bb)')
or regular .createNode syntax:
merge = nuke.createNode('MergeExpression')
merge['expr0'].setValue('abs(Ar-Br)')
merge['expr1'].setValue('abs(Ag-Bg)')
merge['expr2'].setValue('abs(Ab-Bb)')
Full code version
import nuke
import nukescripts
red = nuke.createNode("Constant")
red['color'].setValue([1,0,0,1])
merge = nuke.createNode('MergeExpression')
merge['expr0'].setValue('abs(Ar-Br)')
merge['expr1'].setValue('abs(Ag-Bg)')
merge['expr2'].setValue('abs(Ab-Bb)')
yellow = nuke.createNode("Constant")
yellow['color'].setValue([1,1,0,1])
merge.connectInput(0, yellow)
nuke.toNode('MergeExpression1').setSelected(True)
nukescripts.connect_selected_to_viewer(0)
# Auto-alignment in Node Graph
for everyNode in nuke.allNodes():
everyNode.autoplace()
Consider! MergeExpression node is much slower that regular Merge(difference) node.

PsychoPy simulate Focus Of Expansion using Random Dot Kinematogram (RDK)

I need to simulate a focus of expansion using PsychoPy's RDK functionality.
I have the following code so far. However this only created a RDK that moves in a certain direction.
from psychopy import visual, event, core
win = visual.Window([1000,1000], rgb=(255,255,255), fullscr=False)
fixSpot = visual.GratingStim(win,tex=None, mask="gauss", size=(0.05,0.05),color='black')
rdk = visual.DotStim(win, units='', nDots=1000, coherence=1.0,
fieldPos=(0,0),
fieldSize=(1,1),
fieldShape='sqr', dotSize=6.0,
dotLife=150, dir=0, speed=0.01,
rgb=None, color=(0,0,0),
colorSpace='rgb255', opacity=1.0,
contrast=1.0, depth=0, element=None,
signalDots='different',
noiseDots='direction', name='',
autoLog=True)
stop = False
while stop == False:
fixSpot.draw()
rdk.draw()
win.flip()
if event.getKeys("a"):
win.close()
stop = True
I need to create an RDK where the dots move away from a specific position in the window.
i.e.
I tried changing parameters however I cant mimic the desired functionality.
I also looked through and searched the psychopy documentation, however there was no mention of 'focus of expansion'.
Is there any way to do this using PsychoPy? If not, what is the best alternative?
There's a demo called starField in PsychoPy Coder view. That has random speeds coming from a single point (traditional "simulations" of space travel have used this to indicate stars being at different distances). You should be able to work out how to give all dots the same speed.
The demo uses ElementArrayStim rather than DotStim because DotStim has its own methods to control dot motions and I don't think you want that.
Fun question. A way to do it is:
from psychopy import visual
win = visual.Window()
stim = visual.DotStim(win, nDots=50, dotLife=60, speed=0) # a non-moving DotStim
for frame in range(100):
stim._dotsXY *= 1.02 # accelerating X-Y expansion
#stim.dotsXY *= stim.dotsXY * [1.02, 1.05] # faster acceleration in y-direction
stim.draw()
win.flip()
This goes "behind the scenes" and manipulates the internal attribute called visual.DotStim._dotsXY. It is just a 2 x nDots numpy array like this:
print stim._dotsXY # look at coordinates
[[ 0.02306344 -0.33223609]
[ 0.30596334 -0.0300994 ]
[-0.10165172 -0.08354835]
[ 0.21854653 -0.07456332]
[-0.39262477 -0.21594382]
...etc
... on which you do all sorts of operations. I can't quite figure out how to do constant-speed expansion in a neat way.

Improve the speed of the script with threads

I am trying this code, and it works well, however is really slow, because the number of iterations is high.
I am thinking about threads, that should increase the performance of this script, right? Well, the question is how can I change this code to works with synchronized threads.
def get_duplicated(self):
db_pais_origuem = self.country_assoc(int(self.Pais_origem))
db_pais_destino = self.country_assoc(int(self.Pais_destino))
condicao = self.condition_assoc(int(self.Condicoes))
origem = db_pais_origuem.query("xxx")
destino = db_pais_destino.query("xxx")
origem_result = origem.getresult()
destino_result = destino.getresult()
for i in origem_result:
for a in destino_result:
text1 = i[2]
text2 = a[2]
vector1 = self.text_to_vector(text1)
vector2 = self.text_to_vector(text2)
cosine = self.get_cosine(vector1, vector2)
origem_result and destino_result structure:
[(382360, 'name abcd', 'some data'), (361052, 'name abcd', 'some data'), (361088, 'name abcd', 'some data')]
From what I can see you are computing a distance function between pairs of vectors. Given a list of vectors, v1, ..., vn, and a second list w1,...wn you want the distance/similarity between all pairs from v and w. This is usually highly amenable to parallel computations, and is sometimes referred to as an embarassingly parallel computation. IPython works very well for this.
If your distance function distance(a,b) is independent and does not depend on results from other distance function values (this is usually the case that I have seen), then you can easily use ipython parallel computing toolbox. I would recommend it over threads, queues, etc... for a wide variety of tasks, especially exploratory. However, the same principles could be extended to threads or queue module in python.
I recommend following along with http://ipython.org/ipython-doc/stable/parallel/parallel_intro.html#parallel-overview and http://ipython.org/ipython-doc/stable/parallel/parallel_task.html#quick-and-easy-parallelism It provides a very easy, gentle introduction to parallelization.
In the simple case, you simply will use the threads on your computer (or network if you want a bigger speed up), and let each thread compute as many of the distance(a,b) as it can.
Assuming a command prompt that can see the ipcluster executable command type
ipcluster start -n 3
This starts the cluster. You will want to adjust the number of cores/threads depending on your specific circumstances. Consider using n-1 cores, to allow one core to handle the scheduling.
The hello world examples goes as follows:
serial_result = map(lambda z:z**10, range(32))
from IPython.parallel import Client
rc = Client()
rc
rc.ids
dview = rc[:] # use all engines
parallel_result = dview.map_sync(lambda z: z**10, range(32))
#a couple of caveats, are this template will not work directly
#for our use case of computing distance between a matrix (observations x variables)
#because the allV data matrix and the distance function are not visible to the nodes
serial_result == parallel_result
For the sake of simplicity I will show how to compute the distance between all pairs of vectors specified in allV. Assume that each row represents a data point (observation) that has three dimensions.
Also I am not going to present this the "pedagoically corret" way, but the way that I stumbled through it wrestling with the visiblity of my functions and data on the remote nodes. I found that to be the biggest hurdle to entry
dataPoints = 10
allV = numpy.random.rand(dataPoints,3)
mesh = list(itertools.product(arange(dataPoints),arange(dataPoints)))
#given the following distance function we can evaluate locally
def DisALocal(a,b):
return numpy.linalg.norm(a-b)
serial_result = map(lambda z: DisALocal(allV[z[0]],allV[z[1]]),mesh)
parallel_result = dview.map_sync(lambda z: DisALocal(allV[z[0]],allV[z[1]]),mesh)
#will not work as DisALocal is not visible to the nodes
#also will not work as allV is not visible to the nodes
There are a few ways to define remote functions.
Depending on whether we want to send our data matrix to the nodes or not.
There are tradeoffs as to how big the matrix is, whether you want to
send lots of vectors individually to the nodes or send the entire matrix
upfront...
#in first case we send the function def to the nodes via autopx magic
%autopx
def DisARemote(a,b):
import numpy
return numpy.linalg.norm(a-b)
%autopx
#It requires us to push allV. Also note the import numpy in the function
dview.push(dict(allV=allV))
parallel_result = dview.map_sync(lambda z: DisARemote(allV[z[0]],allV[z[1]]),mesh)
serial_result == parallel_result
#here we will generate the vectors to compute differences between
#and pass the vectors only, so we do not need to load allV across the
#nodes. We must pre compute the vectors, but this could, perhaps, be
#done more cleverly
z1,z2 = zip(*mesh)
z1 = array(z1)
z2 = array(z2)
allVectorsA = allV[z1]
allVectorsB = allV[z2]
#dview.parallel(block=True)
def DisB(a,b):
return numpy.linalg.norm(a-b)
parallel_result = DisB.map(allVectorsA,allVectorsB)
serial_result == parallel_result
In the final case we will do the following
#this relies on the allV data matrix being pre loaded on the nodes.
#note with DisC we do not import numpy in the function, but
#import it via sync_imports command
with dview.sync_imports():
import numpy
#dview.parallel(block=True)
def DisC(a):
return numpy.linalg.norm(allV[a[0]]-allV[a[1]])
#the data structure must be passed to all threads
dview.push(dict(allV=allV))
parallel_result = DisC.map(mesh)
serial_result == parallel_result
All the above can be easily extended to work in a load balanced fashion
Of course, the easiest speedup (assuming if distance(a,b) = distance(b,a)) would be the following. It will only cut run time in half, but can be used with the above parallelization ideas to compute only the upper triangle of the distance matrix.
for vIndex,currentV in enumerate(v):
for wIndex,currentW in enumerate(w):
if vIndex > wIndex:
continue#we can skip the other half of the computations
distance[vIndex,wIndex] = get_cosine(currentV, currentW)
#if distance(a,b) = distance(b,a) then use this trick
distance[wIndex,vIndex] = distance[vIndex,wIndex]

Calculation star position in the sky, PyEphem

I have difficulties with finding current coordinates (RA, DEC) for star in sky.
In net I have found only this one tutorial, how to use ephem library: http://asimpleweblog.wordpress.com/2010/07/04/astrometry-in-python-with-pyephem/
As I understood I need to:
create observer
telescope = ephem.Observer()
telescope.long = ephem.degrees('10')
telescope.lat = ephem.degrees('60')
telescope.elevation = 200
Create a body Object star
here is trouble, I have only (RA,DEC) coordinates for star
Calculate position by .calculate(now())
by new coordinates find altitude
One more question about accuracy of this library, how accurate it is? I have compared juliandate and sidestreal time between this program and kstars, looks like quite similar.
and this http://www.jgiesen.de/astro/astroJS/siderealClock/
PS! Or may be some one can reccomend better library for this purposes.
I guess you're looking for FixedBody?
telescope = ephem.Observer()
telescope.long = ephem.degrees('10')
telescope.lat = ephem.degrees('60')
telescope.elevation = 200
star = ephem.FixedBody()
star._ra = 123.123
star._dec = 45.45
star.compute(telescope)
print star.alt, star.az
I don't know about the accuracy; pyephem uses the same code as xephem, and eg the positions of the planets are given by rounded-down VSOP87 solutions (accuracy better than 1 arcsecond); kstars appears to use the full VSOP solution.
But this will really depend on your need; eg don't rely on it blindly guiding your telescope, there are better solutions for that.
star = ephem.FixedBody(ra=123.123, dec=45.45)
in my case fixedbody creation does not work, should be
star = ephem.FixedBody()
star._ra = ephem.hours('10:10:10')
star._dec = ephem.degrees('10:10:10')

Categories