WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rating: Thread Rating: 15 votes, 5.00 average. Display Modes
  #1  
Old 11-04-2004, 12:38 AM
vjosh vjosh is offline
Member
 
Join Date: Sep 2004
Posts: 17
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
Reply With Quote
  #2  
Old 11-04-2004, 12:39 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
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)
Reply With Quote
  #3  
Old 11-04-2004, 11:31 PM
vjosh vjosh is offline
Member
 
Join Date: Sep 2004
Posts: 17
Thanks, farshizzo... that should help a lot.

-Josh
Reply With Quote
  #4  
Old 02-12-2005, 06:33 PM
vadrian vadrian is offline
Member
 
Join Date: Sep 2004
Posts: 32
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?
Reply With Quote
  #5  
Old 02-12-2005, 07:51 PM
vadrian vadrian is offline
Member
 
Join Date: Sep 2004
Posts: 32
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)
Reply With Quote
  #6  
Old 02-13-2005, 02:10 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
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)
Reply With Quote
  #7  
Old 05-13-2005, 05:39 PM
vadrian vadrian is offline
Member
 
Join Date: Sep 2004
Posts: 32
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?
Reply With Quote
  #8  
Old 05-13-2005, 05:55 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
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)
Reply With Quote
  #9  
Old 05-19-2005, 08:13 PM
vadrian vadrian is offline
Member
 
Join Date: Sep 2004
Posts: 32
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?
Reply With Quote
  #10  
Old 05-23-2005, 12:13 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
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
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 09:47 PM.


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