![]() |
#1
|
|||
|
|||
Creating Subclasses of Vizard Classes
Hey,
I might be in a little over my head here, but is there any convenient way to make a class that inherits all the <node3d> methods? Lets say I'm designing a class that contains a Vizard <node3d> object as one of its data members. Is there a way to allow clients to transform the object in an instance of the class without making them reach in and directly access the object itself? (I suppose another inconvenient alternative is to define wrapper methods that do nothing but call the corresponding method on the <node3d> object, but I'd rather use inheritance if it isn't too much trouble.) There's also another potential complication: I want the <node3d> object contained within the class to be an object created on-the-fly with viz.startlayer/viz.endlayer. Thanks ahead of time! -Josh |
#2
|
|||
|
|||
Hi,
What you want to do is very easy. Here's a simple example that should get you started: Code:
import viz viz.go() #Define class that inherits from VizNode class MyNodeClass(viz.VizNode): def __init__(self): #Create some node viz.startlayer(viz.LINES) viz.vertexcolor(viz.RED) viz.vertex(-1,0,0) viz.vertex(1,0,0) self.node = viz.endlayer() #Initialize base class viz.VizNode.__init__(self,self.node.id) #Create an instance of my custom class myclass = MyNodeClass() #Perform basic node operations on custom class myclass.translate(0,1,4) myclass.spin(0,1,0,90) |
#3
|
|||
|
|||
Thanks, farshizzo... that should help a lot.
-Josh |
#4
|
|||
|
|||
I want to do a similar sub class, but use an imported object as the node. should I just put
self.node = viz.add("ball.wrl") or viz.VizNode.__init__(self,"ball.wrl") or something else? |
#5
|
|||
|
|||
also, can I have an object that inherits from both an event class and a node? that is, i want something that both has a visual representation and controls its own animations. To do so would I call
class ObjectWithEvent(viz.EventClass,viz.VizNode) or would I subclass the node first also, how would I initialize such an object (if it doesn't conflict with itself) |
#6
|
|||
|
|||
To subclass from an imported object you would do the same thing as subclassing from an OTF object. I'm posting an example script that shows how to do this. It also shows how to subclass from an EventClass also. One thing to note when subclassing from a VizNode and EventClass is that both classes have a callback function, so you have to explicitly specify which version you want to call when using it. The sample script shows how to do this.
Code:
import viz viz.go() #Define class that inherits from VizNode and EventClass class MyNodeClass(viz.VizNode,viz.EventClass): def __init__(self): #Create some node self.node = viz.add('ball.wrl') #Initialize base classes viz.VizNode.__init__(self,self.node.id) viz.EventClass.__init__(self) #Since both VizNode and EventClass have a "callback" function #we have to explicitly specify the EventClass "callback" function viz.EventClass.callback(self,viz.TIMER_EVENT,self.mytimer) self.starttimer(0,0.01,viz.FOREVER) def mytimer(self,num): self.node.rotate(0,1,0,90*viz.elapsed(),viz.RELATIVE_LOCAL) #Create an instance of my custom class myclass = MyNodeClass() #Perform basic node operations on custom class myclass.translate(0,1,4) |
#7
|
|||
|
|||
i guess i'm really into sublcassing
![]() anyway, now i want to make a subclass of an Avatar. I want to have the same functionality of the vizard class, but also add my own custom actions. right now i just have a generic class with an avatar as an instance variable, but i have to remake a lot of function wrappers for every action i want to add. is there a way to add behaviors/actions to a vizard-class or sub-classes avatar? that is, i want to have calls like .state(27) or .execute(29) with 27/29 being my own. should i subclass off vizavatar, node3d? does this make sense? |
#8
|
|||
|
|||
Hi,
The avatar class is already a subclass of the node class, so you just need to subclass from the avatar. Here is a small sample: Code:
class MyAvatar(viz.VizAvatar): def __init__(self): #Create some avatar self.avatar = viz.add('female.cfg') #Initialize base class viz.VizAvatar.__init__(self,self.avatar.id) #Override certain functions def execute(self,animation,delay_in=viz.AVATAR_DELAY,delay_out=viz.AVATAR_DELAY): if animation in [27,29]: #Do my own thing pass else: #Pass it on to base class viz.VizAvatar.execute(self,animation,delay_in,delay_out) def state(self,animation,delay=viz.AVATAR_DELAY): if animation in [27,29]: #Do my own thing pass else: #Pass it on to base class viz.VizAvatar.state(self,animation,delay) |
#9
|
|||
|
|||
if i have a list of xyz rotations (from head tracking file) and i want to make an action that is generic (for any avatar) and repeatable, should I make a vizact object via:
Code:
actionList = [] for line in file: actionList.append( vizact.headto(x, y, z) ) action = vizact.sequence(actionList ) Code:
avatar = viz.add('avatar.cfg') head = avatar.getbone('skel_Head') #Lock the head bone so that we can manually control it head.lock() def mytimer(num): #Apply some rotation to the head head.rotate(ori) |
#10
|
|||
|
|||
Hi,
If you wanted to create something that is easy to use and reusable then I would suggest creating your own vizact Action. Here is a basic template for creating the action: Code:
class MyHeadAction(viz.ActionClass): def begin(self,object): #Get the list of ori data self.oridata = self._actiondata_.data #Get the head bone and lock it self.bone = object.getbone('skel_Head') self.bone.lock() def update(self,elapsed,object): #Called every frame to update action if self.finished(): return #Update the head rotation self.bone.rotate(x,y,z) #When the action is finished call the following self.end(object) def animatehead(filename): action = viz.ActionData() #Read orientation data from file and add it to action data action.data.append([x,y,z]) action.actionclass = MyHeadAction return action Code:
headAction = animatehead('headdata.txt') avatar.add(headAction) |
![]() |
Thread Tools | |
Display Modes | Rate This Thread |
|
|