Oh, I made a python class for that a while back...
Here it is:
- Code: Select all
from Plasma import *
from PlasmaTypes import *
from PlasmaNetConstants import *
from xPsnlVaultSDL import *
import math
import time
class ScnObjManipulator:
def __init__(self,object,agename):
try:
self.scnobj = PtFindSceneobject(object, agename)
except:
self.scnobj = None
self.initialMtx = self.getMatrix()
# resets the object to the initial position and scale
def reset(self):
self.setMatrix(self.initialMtx)
def getMatrix(self):
return self.scnobj.getLocalToWorld()
def setMatrix(self,l2w):
# this is a weird way of calculating an inverse matrix, the getInverse() function
# actually stores the inverse matrix into the matrix that is used as an argument.
w2l = ptMatrix44()
l2w.getInverse(w2l)
self.scnobj.setTransform(l2w,w2l)
# Scales an object - relative to current scale
def scale(self,scale):
l2w = self.getMatrix()
self.setScale(scale,l2w)
# Scales an object - relative to initial scale
def setScale(self,scale,initialmtx=None):
if initialmtx is None:
initialmtx = self.initialMtx
scalemtx = ptMatrix44()
scalemtx.makeScaleMat(scale)
initialmtx *= scalemtx
self.setMatrix(initialmtx)
# Moves an object - relative to current position
def move(self,vector):
l2w = self.getMatrix()
self.setMove(vector,l2w)
# Moves an object - relative to initial position
def setMove(self,vector,initialmtx=None):
if initialmtx is None:
initialmtx = self.initialMtx
trltmtx = ptMatrix44()
trltmtx.makeTranslateMat(vector)
initialmtx *= trltmtx
self.setMatrix(initialmtx)
# Rotates an object - relative to current rotation
def rotate(self,anglevector,radians):
l2w = self.getMatrix()
self.setRotation(anglevector,radians,l2w)
# Rotates an object - relative to initial rotation
def setRotation(self,anglevector,radians,initialmtx=None):
if initialmtx is None:
initialmtx = self.initialMtx
fullrotmtx = ptMatrix44()
# now, apply relative transformations for the anglevector
anglevector.normalize() # normalize before using it for scale
# for the X part
xfactor = anglevector.getX()
rotmat = ptMatrix44()
rotmat.makeRotateMat(0,radians*xfactor)
fullrotmtx *= rotmat
# for the Y part
yfactor = anglevector.getY()
rotmat = ptMatrix44()
rotmat.makeRotateMat(2,radians*yfactor)
fullrotmtx *= rotmat
# for the Z part
zfactor = anglevector.getZ()
rotmat = ptMatrix44()
rotmat.makeRotateMat(1,radians*zfactor)
fullrotmtx *= rotmat
initialmtx *= fullrotmtx
self.setMatrix(initialmtx)
The following example shows how to use it. I've stripped it down to only the most neccesary lines...
- Code: Select all
class FuncChk01(ptResponder,):
__module__ = __name__
def __init__(self):
ptResponder.__init__(self)
self.RotTestObj = none
def OnServerInitComplete(self):
# Obtain the piston objects
self.RotTestObj = ScnObjManipulator("Rotate1",self.AgeName)
# Register a timer callback for moving the piston objects
PtAtTimeCallback(self.key, self.MovePistonInterval, self.MovePistonTimerId)
def OnTimer(self, id):
# Check for the piston timer
if (id == self.MovePistonTimerId):
# Calculate the height offset of the pistons, based on the time
currentTime = time.clock()
# Warp the pistons to the new height
# Rotational object
rotOffset = (math.sin(2.0*currentTime) *0.05) + 1.0
angle = ptVector3(0.0,0.0,1.0)
self.RotTestObj.rotate(angle,rotOffset);
# Register for another piston update
PtAtTimeCallback(self.key, self.MovePistonInterval, self.MovePistonTimerId)
Ok, it mayt not be all that clear, I admit, but basically, you can do the following things with the sceneobjects through this wrapper class:
- Do relative rotation
- Set absolute rotation (relative to the original position of the object)
- Do relative scaling
- Set absolute scales (relative to the original position of the object)
- Move
- Reposition
The comments in the code file should help you get the correct function
Note:"Relative to the original position of the object" means that it can scale it relative to when the wrapper class was constructed.
If you create the wrapper object again each time you want to do an operation, you can't reset it to the original settings.
It's best to create the object just once - in the initialization - and just reference it from there (just like in the example code)
One day I ran through the cleft for the fiftieth time, and found that uru held no peace for me anymore.