WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   retrieve Object names (https://forum.worldviz.com/showthread.php?t=506)

Geoffrey 02-09-2006 09:03 AM

retrieve Object names
 
1 Attachment(s)
Hello,

I want to know wich object is under given screencoordinates.
(In this test-case these are the mousecoordinates..)
How can I access the name of the object ??

Code:

import viz

viz.go()

Cube1 = viz.add('theCube.ac')
Cube2 = viz.add('theCube.ac')
Cube3 = viz.add('theCube.ac')

Cube1.translate(-3,0,15)
Cube2.translate(0,0,15)
Cube3.translate(3,0,15)

def mijntimer(num):
        line = viz.screentoworld(viz.mousepos())
        begin = line[:3]
        end = line[3:]
        info = viz.intersect(begin,end)
        if info.intersected:
                print 'you are over object : ', info.object.id
                #mijntest = info.object
                #mijntest.Name() ...??
                #I want the name of the object.. Cube1, Cube2 or Cube3
               
viz.callback(viz.TIMER_EVENT,mijntimer)
viz.starttimer(0,0.5,viz.FOREVER)

for now the output is :

Code:

Loading File: theCube.ac
Loading File: theCube.ac
Loading File: theCube.ac
** Load Time: 0.38 seconds
you are over object :  0
you are over object :  1
you are over object :  2
you are over object :  2
you are over object :  1
you are over object :  2

I would like it to be :
you are over object : Cube1
you are over object : Cube2
you are over object : Cube3

can anyone help me ?
thanks,

geoffrey.

btw : I attached the file theCube.ac but changed the extention to txt (otherwise I could not attach it)

farshizzo 02-09-2006 09:27 AM

Hi,

There are many ways you can do this. You can create a dictionary that maps an object to its name:
Code:

nameDict = { Cube1:'Cube1',Cube2:'Cube2',Cube3:'Cube3' }
.
.
print 'you are over object : ', nameDict[info.object]

Or you could create a name attribute on the object:
Code:

Cube1.name = 'Cube1'
Cube2.name = 'Cube2'
Cube3.name = 'Cube3'
.
.
print 'you are over object : ', info.object.name

In each case you need to be careful when the mouse is over an object that you haven't named.

Geoffrey 02-09-2006 10:07 AM

another way ?
 
Hello,
I got your point,
I tested it, and off course it works.

but cant you just refer to the name you use when adding items to the world ?

Code:

Cube1 = viz.add('theCube.ac')
so here refer to Cube1, without extra need to say Cube1.name = Cube1

In that case the error could not be made when something is not named...

Another question is (expantion of this problem):

If you have a scene (myScene.ac), with lets say 3 objects :
a cylinder : myCylinder
a Cube : myCube
a Sphere : mySphere

I can get to these objects with :

Code:

myScene = viz.add('myScene.ac')
cylinder = myScene.getchild('myCylinder')

now what I would need to do is :

I load a scene,
and when I start, I need to know at each time which object is under the 2D coordinates.
This is the same program as the first one in the thread.
The only difference is that I want every object that is in the scene to be automatically named as in the scene file.
The information is there because I can call to my subchild... but I do not know how I can acces this info in the way I need it.

I tried with

Code:

myScene.get(viz.CHILDREN)
what should give me a list of all childs, but it does not work.
thanks for your help!

greetz
geoffrey.

farshizzo 02-09-2006 10:32 AM

Hi,

Python objects don't have names associated with them when they are created. You will have to explicitly name them yourself.

The command myScene.get(viz.CHILDREN) will return all Vizard nodes that are children of the object. When you add myScene it creates a single Vizard node, which contains miscellaneous internal nodes. When you call myScene.getchild('myCylinder') it will search for an internal node with the name myCylinder and convert it into a Vizard node. It seems like you need a command that will return the names of all the internal nodes underneath an object. Vizard doesn't have this feature, but it could be implemented through a plugin.

What exactly are you trying to accomplish by retrieving the names of the object? Maybe there is a better way to go about it.

Geoffrey 02-09-2006 11:34 AM

First of all,
thank you for replying that quickly,

what I try to do :

A user will navigate (with a joystick) in a virtual room.
I need to know what he is looking at, an when he is looking at it.

I will get (real-time) data from a pc with an eye tracker.
this data consists of 2D coordinates.

I want to check which object is under those 2D coordinates.
And I want to write this to a file.

I also want to know when, which items are visible for the user.

I dont know yet how the data will need to be saved, but one thing is sure :
I need to be able to know which objects are on the screen and
Which object is under the 2D coordinate at particular time.

this should be written in a file to do datamanipulation with later on.

thank you,
geoffrey.

farshizzo 02-09-2006 12:22 PM

Hi,

In this case you will need to create a Vizard node for each object that you want to be viewable by the user. If you have one model file with individually viewable objects, then you need to call getchild on each sub-object to ensure that a Vizard node is created for it.

You might want to create your own class that handles which objects are viewable and detects them for you. I've made a simple version here:
Code:

import viz
viz.go()

class NodeDetector:
        def __init__(self):
                self.__nodes = []
               
        def addNode(self,node,name):
                node.name = name
                self.__nodes.append(node)
               
        def detectNode(self,pos):
                line = viz.screentoworld(pos)
                info = viz.intersect(line[:3],line[3:])
                if info.intersected:
                        if info.object in self.__nodes:
                                return info.object
                return None

Cube1 = viz.add('theCube.ac')
Cube2 = viz.add('theCube.ac')
Cube3 = viz.add('theCube.ac')

Cube1.translate(-3,0,15)
Cube2.translate(0,0,15)
Cube3.translate(3,0,15)

nd = NodeDetector()
nd.addNode(Cube1,'Cube1')
nd.addNode(Cube2,'Cube2')
nd.addNode(Cube3,'Cube3')

def mijntimer(num):
        node = nd.detectNode(viz.mousepos())
        if node:
                print 'you are over object : ', node.name
               
viz.callback(viz.TIMER_EVENT,mijntimer)
viz.starttimer(0,0.5,viz.FOREVER)


Geoffrey 02-09-2006 12:53 PM

Owkay thank you,

and is there any way to automatically know which objects are available in a model file ?

I added another file with 3 cubes in it (Cube1_obj, Cube2_obj an Cube3_obj)
say I load this 'scene-file' in vizard, can I get a list with all the objects in this scene?

geoffrey

farshizzo 02-09-2006 12:56 PM

Hi,

As I already mentioned, this feature is not built-in to Vizard, but can be made available with a plugin. Would you like the plug-in to simply return a list of all the unique names of the sub-objects?

Geoffrey 02-09-2006 01:00 PM

hello,
well for the moment, this is al that I would need to solve the problem here I guess.

I do not know how to write a plugin..
geoffrey.

farshizzo 02-09-2006 01:23 PM

1 Attachment(s)
Hi,

I've attached the plugin you need with small sample script showing how to use it. Keep in mind that this will return ALL named nodes of an object. So you should probably come up with a naming convention to signify which nodes you want to actually retrieve. Let me know if you have any problems.

Geoffrey 02-09-2006 01:37 PM

thanks alot farshizzo !
Ill work a bit with this, and let you know if I have more troubles

thanks again,
geoffrey.

nige777 12-11-2009 04:26 AM

sorry for bump
 
Hi farshizzo

I found this post after an extensive search of the forum and thought that I had found the answer to one of my problems - creating a list of all the names of the child nodes in a model. However when I try to use it (with provided script) I get:

** ERROR: Failed to load plug-in: 'geoffrey.dlm':confused:

Is this because it was written for a previous release? Or is this functionality now available through python?

I would be very grateful for any help with this as its quite central to my current project

Thanks

Nige

edit: I'm sorry if this was posted in wrong forum as it's really a Vizard 3 question

Re-edit: Oddly it works as expected just calling 'getNodeNames', so I guess this was a bump for no reason - sorry


All times are GMT -7. The time now is 02:05 AM.

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