WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 08-27-2007, 10:22 AM
shivanangel shivanangel is offline
Member
 
Join Date: Feb 2006
Location: New Jersey
Posts: 182
Pick porblem with arrays

Hello,

I am having a little bit of a problem with the Pick() function.
My code is below, but I will try to explain how the problem came up to begin with.

I was working on a selection class to highlight an object or expand to see different objects when it was clicked. I was using the Pick() function for highlighting the eligable objects, and if the left mouse button was also clicked, it would then set that object as selected and would then cause several spheres to come out and be visible (which would later be equivolent to different selection choices).

Anyways, I had it working, but then I went and used an array to hold each instance of a class called element, which was basically what I was checking for in the selection algorithm.

This is the new, very initial code just to make a sphere highlight when selected.

An array, composed of objects, is passed to the function:

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]


These objects in the array each have an associated model and I would access it by typing instanceofclass.model.

Before I would just check to see if a variable set equal to viz.pick(1) was equal to the instanceofclass.model and this worked just fine.

Now however, with the new array of objects I find that the addresses that are being referenced are not equal to one another. Basically, I just printed out the picked object and what I am looking for. I watched the output and when I moused over the object, the memory addresses were not the same.

Here is the selection code:

[I]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.model:
#highlight the object
item.ambient(5,5,5)
else:
#unhighlight the object
item.object.ambient(0.5,0.5,0.5)


Any help would be great, thank you!

George Lecakes
Rowan University
Reply With Quote
  #2  
Old 08-27-2007, 10:43 AM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
Could you post a full script that I can run? Also, when posting code on the forums, please use the [code][/code] tags, it will preserve the formatting and make it easier to read.
Reply With Quote
  #3  
Old 08-27-2007, 11: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
  #4  
Old 08-27-2007, 11:10 AM
shivanangel shivanangel is offline
Member
 
Join Date: Feb 2006
Location: New Jersey
Posts: 182
You might need this class as well, it isn't implemented, but I think the Element class has some calls to it, so better safe than sorry:

Code:
#######################################################
## Created By: George Lecakes
## Purpose: 
##		The Visualizer class is meant as a container
##		to hold the model information as well as what
##		to do when the user selected an object.
##
##	Requirements:
##		This class requires a timer to check
##		for onMouseOver events. 
##
## Date Created: August 13, 2007
##		Created the class.
##
#######################################################

import viz
import vizact
import vizinfo

class Visualizer:
	def __init__(self):
	#Initialize Variables
		#Sets the model for this instance.
		self.model = viz.add('testSphere.OSG')
		#Set the model coordinates
		self.coords = [0,0,0]
		#Set the model scale
		self.scale = [1,1,1]
		#Is the object Moused Over?
		self.isMousedOver = 0
		#Is this object selected or not?
		self.isSelected = 0
		#sets the base alpha state
		self.alpha = 0.5
		#sets the base gamma state
		self.ambient = [1,1,1]
		#Set the gamma when onMouseOver
		self.overambient = [2,2,2]
		#Set the color of the selected item
		self.selectedColor = [0,1,0]
		#Event when mouse is over an object
		self.MouseOverObject = vizact.fade([1,1,1],[0.5,0.5,0.5],0.25)
		#Event when mouse is selecting an object
		self.MouseSelectObject = vizact.fade([1,1,1],[0,1,0],0.25)
		#Event when mouse is not selecting object
		self.noMouseSelectObject = vizact.fade([0,1,0],[0.5,0.5,0.5],0.25)

	def overObject(self):
		self.item = viz.pick(1)
		
		# 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.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


	#def selectObject(self, num):
Thanks!

George Lecakes
Rowan University
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 07:55 PM.


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