WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   Creating Subclasses of Vizard Classes (https://forum.worldviz.com/showthread.php?t=241)

vjosh 11-03-2004 11:38 PM

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 methods? Lets say I'm designing a class that contains a Vizard 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 object, but I'd rather use inheritance if it isn't too much trouble.) There's also another potential complication: I want the object contained within the class to be an object created on-the-fly with viz.startlayer/viz.endlayer.

Thanks ahead of time!

-Josh

farshizzo 11-04-2004 11:39 AM

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)


vjosh 11-04-2004 10:31 PM

Thanks, farshizzo... that should help a lot.

-Josh

vadrian 02-12-2005 05:33 PM

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?

vadrian 02-12-2005 06:51 PM

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)

farshizzo 02-13-2005 01:10 PM

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)


vadrian 05-13-2005 04:39 PM

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?

farshizzo 05-13-2005 04:55 PM

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)


vadrian 05-19-2005 07:13 PM

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 )

or would this timer/list iteration be a better plan (from another thread)

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)

I may have up to 20 avatars in the script executing an action (but not all will be in view at once, and not all will be acting at once). which would be faster?

farshizzo 05-23-2005 11:13 AM

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

Now you would use the following code to perform your custom action:
Code:

headAction = animatehead('headdata.txt')

avatar.add(headAction)

Let me know if you need anymore help with this


All times are GMT -7. The time now is 04:06 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC