Its in the title. How do I store an objects world or local rotation in a python dictionary?
ast.literal_eval() can be used to convert a string to a list
To go the other way
list(object.worldOrientation.to_euler())
Says NameError: name ast is not defined.
I’ve never used that before, but you probably need to start with this line:
import ast
PS: Out of curiosity what are you trying to accomplish?
import ast
then ur ast.code
also you can convert orientation like this:
[list(vector) for vector in obj.localOrientation]
or take a look on how i convert stuff to be saved right here:
Isn’t that just like storing everything else in a dictionary?
myDictionary['myKey'] = object.worldOrientation
You can’t pickle / recal orientation matrix afaik
worldOrientation is a 3x3 matrix which is just an array of floats. The easiest thing to do(IMO) without importing anything, would be to copy the component values to python built in type. You could make a list of 3 lists(representing each matrix row). Save that list. When you want to write it back, loop over orientation matrix and write the values.
[edited typo on line 4]
def get_orient(obj):
orient = []
for row in obj.worldOrientation:
orient.append(list(row)) # casting vector to list
return orient
def set_orient(obj, orient):
for row in range(3): # 3 rows
for comp in range(3): # of 3 components
obj.worldOrientation[row][comp] = orient[row][comp]
such a script explaining what mine does already
#creates a list
ori = [list(vector) for vector in obj.localOrientation]
#set ori
obj.localOrientation = ori
Use list to store a euler
On startup recover the list and build a euler and use it =D
No external libs required
sorry didn’t bother to read your reply since you started out with having him import ast for some reason
Then read the TS his post…
Says NameError: name ast is not defined.
That’s great. Still didn’t read your reply.
the example cotax gives is the simplest way to do it, no need for complex functions.
#creates a list
ori = [list(vector) for vector in obj.localOrientation]#set ori
obj.localOrientation = ori
and for worldOrientation
#creates a list
ori = [list(vector) for vector in obj.worldOrientation]#set ori
obj.worldOrientation = ori
ast is nice for ast.literal_eval to pack a bunch of data as a list turned into a string - and then recover it back or a list of tuple or ??? you can pack a whole inventory into 1 string if you know what you are doing.
and euler is 3 values
rot =list(own.worldOrientation.to_euler())
is storable
and recalled with
object.worldOrientation = mathutils.Euler(stored_rot_list, ‘XYZ’)
import ast
ast.literal_eval('data_to_convert')
is indeed a good function, i use it to convert stuff(save file in above post) back into a dict.
But it’s kinda overkill for this simple task.
object.worldOrientation = mathutils.Euler(stored_rot_list, ‘XYZ’)
You know that you don’t have to recalculate to_euler() right?
for example
rot = obj.worldOrientation.to_euler()
rot.x += 10
obj2.worldOrientation = rot
works perfectly fine.
So once more there would be no need to import an api
import mathutils
rot = list(own.worldOrientation.to_euler) #this is to make the data easily stored in a pickle or json or whatever (string even)
object.worldOrientation = mathutils.Euler(stored_rot_list, ‘XYZ’) <- this is to recover a rotation you pickled as a list
it is all about context.
if you have a big data set, yes pack it as best you can, and if all you are going to store is what way the players nose is pointing, then there is no need to convert back and forth, those few bytes does not matter.