Lepes
28-01-2009 20:12:17
Hi, I'm newbiew on pyOgre, I would like that you tell me if you consider this class useful or not. I know that to inherited from a SceneNode is a hard task, so, like a newbiew that I am, I have build two classes to work in easy way with Ogre. The targe is to use a code like this:
Main tip is to convert from ogre.Vector3 and ogre.Quaternion to python types in a easy way (tuples, List, String List). Comment what do you think,
The class is here (use freely if you think it worth
)
Main tip is to convert from ogre.Vector3 and ogre.Quaternion to python types in a easy way (tuples, List, String List). Comment what do you think,
point1 = positionClass()
point1.asStrList = ["1", "2", "3"] # readed from a text file
#myOgreNode is obviously a pre-exist ogre.Node
myOgreNode.setPosition(point1.asTuple)
point1.asVector3 = myOgreNode.getPosition() + myOgreNode2.getPosition()
print point1.asStrList
point1.x, point1.y, point1.z = 1, 1, 1
print point1.asStrList
The class is here (use freely if you think it worth

from types import *
import ogre.OGRE as ogre
class posRotClass(object):
# values = [] don't touch !!!
def clear(self):
del self.values[0: len(self.values)]
def setValues(self, value):
""" accept Tuple, None, List, Vector3 and Quaternion
returng a List.
If you use a asQuaternion you must be coherent """
self.clear()
if type(value) is TupleType or type(value) is ListType:
for i in value:
self.values.append(i)
elif isinstance(value, NoneType):
self.values.append(None)
self.values.append(None)
self.values.append(None)
self.values.append(None)
elif isinstance(value, ogre.Vector3):
self.values.append(value.x)
self.values.append(value.y)
self.values.append(value.z)
elif isinstance (value, ogre.Quaternion) :
self.values.append(value.w)
self.values.append(value.x)
self.values.append(value.y)
self.values.append(value.z)
else :
raise Exception("Instance Type not allowed in posRotClass.setValues")
def getasVector3(self):
if self.values is None:
self.values = []
if len(self.values) < 3:
self.append(0)
self.append(0)
self.append(0)
return ogre.Vector3(self.values[0], self.values[1], self.values[2])
def setasVector3(self, value):
self.setValues(value)
def getasTuple(self):
return self.values[0], self.values[1], self.values[2]
def setasTuple(self, value):
self.setValues(value)
def getasList(self):
return self.values
def setasList(self, value):
self.setValues(value)
def getasQuaternion(self):
if len(self.values) !=4:
raise Exception("we don't have 4 values to convert to Quaternion")
return ogre.Quaternion(self.values[0], self.values[1], self.values[2], self.values[3])
def setasQuaternion(self, value):
self.setValues(value)
def isNone(self):
return self.values[0] == None
def hasValues(self):
return self.values[0] != None
def nearZero(self, num):
""" check if num is near Zero < 0.0001 due
floats imprecision"""
return num < 0.0001 and num > -0.0001
def isZero(self):
return not self.isNone() and len(self.values) > 2 and self.nearZero(self.values[0]) \
and self.nearZero(self.values[1]) and self.nearZero(self.values[2])
def _getasStrList(self):
l = []
for i in range(0, len(self.values)):
l.append(str(self.values[i]))
return l
def _setasStrList(self, value):
self.clear()
for i in range(0, len(value)):
self.values.append(float(value[i]))
asQuaternion = property(getasQuaternion,setasQuaternion,
doc= """You must be coherent.
If you set up as Quaternion, then you
shouldn't read as a Tuple """)
asList = property(getasList,setasList,
doc= """""")
asStrList = property(_getasStrList,_setasStrList,
doc= """""")
asVector3 = property(getasVector3,setasVector3,
doc= """""")
asTuple = property(getasTuple,setasTuple,
doc= """""")
class positionClass(posRotClass):
def __str__(self):
return " " + ", ".join(self.values)
def __init__(self):
self.values = []
self.values = [0,0,0,0]
def _getx(self):
return self.values[0]
def _setx(self, value):
self.self.values[0] = value
def _gety(self):
return self.values[1]
def _sety(self, value):
self.values[1] = value
def _getz(self):
return self.values[2]
def _setz(self, value):
self.values[2]= value
z = property(_getz,_setz,
doc= """""")
y = property(_gety,_sety,
doc= """""")
x = property(_getx,_setx,
doc= """""")
class rotationClass(posRotClass):
def __str__(self):
return " " + ", ".join(self.values)
def __init__(self):
self.values= []
self.values = [0,0,0,0]
def _getrotx(self):
return self.values[0]
def _setrotx(self, value):
self.values[0] = value
def _getroty(self):
return self.values[1]
def _setroty(self, value):
self.values[1] = value
def _getrotz(self):
return self.values[2]
def _setrotz(self, value):
self.values[2]= value
rotz = property(_getrotz,_setrotz,
doc= """""")
roty = property(_getroty,_setroty,
doc= """""")
rotx = property(_getrotx,_setrotx,
doc= """""")
#test class
if __name__=="__main__":
""" easy class to use with ogre in example:
point1 = positionClass()
point1.asStrList = ["1", "2", "3"] # readed from a text file
myOgreNode.setPosition(point1.asTuple)
point1.asVector3 = myOgreNode.getPosition() + myOgreNode2.getPosition()
print point1.asStrList
"""
x = positionClass()
x.asList = [1,2,3]
print x.x
y = positionClass()
y.asList = [3,3,3]
print " ".join(x.asStrList)
print " ".join(y.asStrList)
print y.x
y.asList = [4,4,4]
print y.x
print x.x