View Single Post
  #3  
Old 08-27-2007, 10:10 AM
shivanangel shivanangel is offline
Member
 
Join Date: Feb 2006
Location: New Jersey
Posts: 182
Sorry about the incorrect formatting procedures. Here is the code, broken down:

Here is the main file, with the Viz.go.
Code:
import viz
import vizact
import vizinfo

from Visualizer import Visualizer
from Element import Element
from ElementArray import ElementArray
from CameraControls import CameraControls

	#Program Start
viz.setMultiSample(2)
viz.go()
#viz.splashscreen('Splash.bmp')

	#Viewport Settings
mview = viz.MainView
mview.setPosition(0,0,-10)
viz.sensitivity(4.5,0.7)

	#Create Element Array, Elements and Visualizers
elementArray = ElementArray()

def onUpdate(e):

	#Selection Loop
	SelectionLoop(elementArray)
	#Mouse Navigation Loop
	if viz.iskeydown(viz.KEY_ALT_L):
		vizact.onkeydown( viz.KEY_ALT_L, viz.cam.setHandler, None )
	else:
		viz.cam.setHandler( CameraControls() )

viz.callback(viz.UPDATE_EVENT,onUpdate)

	#SELECTION ALGORITHM

def SelectionLoop(elementArray):
	#What is the Mouse over?
	item = viz.pick(1)
	#Make a loop equal to the array size.  Iterate through every element in the array.
	for i in range(len(elementArray.myArray)):
		#Check to see if the mouse is over the object
		print 'elemArray'
		print elementArray.myArray[i].model
		print 'item.object'
		print item.object
		if item.object == elementArray.myArray[i].model:
			#highlight the object
			item.ambient(5,5,5)
		else:
			#unhighlight the object
			item.object.ambient(0.5,0.5,0.5)
I recently stuck SelectionLoop function into the main code, I originally had it as an outside function, but when I ran into some problems with this array business I tried throwing it into the main file just to see if it resolved any variable passing issues.

Here is the ElementArray class:
Code:
import viz
import vizinfo
import vizact
import math
import SelectionHandler
from Element import Element

#######################################################
#
# This is where you code everything you want your elements
# to be.  All of it is hand coded, this is where 'your' work
# goes if you are using my classes as a base.
#This goes for you guys down at Stennis Space Center!
#######################################################

class ElementArray:
	def __init__(self):
		#Create two arrays
		elem1coords = -5,-1,0
		elem2coords = 5,1,0
		elem1 = Element(elem1coords, viz.ON)
		elem2 = Element(elem2coords, viz.ON)
		self.myArray = [elem1,elem2]
and, the element class:

Code:
import viz
import vizinfo
import vizact
import math

from Visualizer import Visualizer

#What needs to happen here:
#
#When clicked on an element model, the element should encircle it in a sphere of appropriate size.
#  Then, several visualization spheres of some % of the sphere size should immerge.  The sphere
#  sizes should be scalable with the '+' '-' keys similar to how maya scales the transform handle.
#

class Element:
	def __init__(self, coords, visib):
		
		#----------------------------------------------
		#Models
		#----------------------------------------------
		
		#Base Model
		self.model = viz.add('testValve.OSG')
		self.model.setScale(0.25,0.25,0.25)
		#Plane Model
		self.plane = viz.add('plane.OSG')
		
		#----------------------------------------------
		#Input Parameter Setup
		#----------------------------------------------
		self.model.setPosition(coords)
		self.plane.setPosition(coords)
		self.model.visible(visib)
		self.plane.visible(visib)
		
		#----------------------------------------------
		#Instance Variables
		#----------------------------------------------
		self.mview = viz.MainView
		#Is the mouse over the base model?
		self.isMousedOver = 0
		#is the base model selected?
		self.isSelected = 0
		#Controls how far away the Visualizers are from the Element
		self.radius = 4
		#Spacing (in degrees) between visualizers
		self.spacing = 360
		#Create instances of Visualizer class
		self.viz1 = Visualizer()
		self.viz2 = Visualizer()
		self.viz3 = Visualizer()
		#Set the models of the Visualizer to children of the plane
		self.viz1.model.parent(self.plane)
		self.viz2.model.parent(self.plane)
		self.viz3.model.parent(self.plane)
		#Create the temporary standin for the Visualization class:



#		# If the mouse if moving over the instance and it is not selected
#		if self.item.object == self.model and self.isMousedOver == 0 and self.isSelected == 0:
#			self.isMousedOver = 1
#			self.model.runAction(self.MouseOverObject)
#		# If the mouse if not over the instance
#		else:
#			self.isMousedOver = 0
#		#If the mouse if over the object, it is not selected and it is left clicked
#		if self.item.object == self.model and self.isMousedOver == 1 and viz.mouse.getState() == viz.MOUSEBUTTON_LEFT and self.isSelected == 0:
#			self.visibleSpheres()
#			self.isSelected = 1
#			self.model.runAction(self.MouseSelectObject)
#		#If the mouse is not over the object, it is selected, and there is a left click
#		if self.item.object != self.model and self.isMousedOver == 0 and viz.mouse.getState() == viz.MOUSEBUTTON_LEFT and self.isSelected == 1:
#			self.isSelected = 0
#			self.model.runAction(self.noMouseSelectObject)
#		#If the object is not selected, the object is not selected, and there is a left click
#		if self.item.object != self.model and self.isMousedOver == 0 and viz.mouse.getState() == viz.MOUSEBUTTON_LEFT and self.isSelected == 0:
#			self.isSelected = 0
			
		#Updates	
		self.plane.lookat(self.mview.getPosition())
		
	def visibleSpheres(self):
		#Set the sphere locations
		for i in range (len(self.sphereArray)):
			self.sphereArray[i].model.visible(viz.ON)
			self.angleBetween = (360 / len(self.sphereArray))
			print 'loop #'
			print i
			self.sphereArray[i].model.setPosition(self.convert2Pol(self.radius,(self.angleBetween*(i+1)*(math.pi/180))))
				
		#Bring the Spheres into visibility
		
			
	def convert2Pol(self, r, theta):
		#This deals with converting the polar coordinates.
		self.x = r * math.cos(theta)
		self.y = r * math.sin(theta)
		self.z = 0
		print 'x:'
		print self.x
		print 'y:'
		print self.y
		self.newCoords = [self.x,self.y,self.z]
		return self.newCoords
Reply With Quote