WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   Call objects created within definitions (https://forum.worldviz.com/showthread.php?t=2146)

lilio 07-12-2009 06:22 AM

Call objects created within definitions
 
Hi,

I want to create buttons within a definition. Something like that:

Code:

def createButton(self, position, pictureOff, pictureOn, name, szene):
                self.Position = position
                self.PictureOff = pictureOff
                self.PictureOn = pictureOn
                self.Name = name
                self.Szene = szene
               
                name = viz.add(viz.BUTTON, scene=szene)
                name.translate(position[0],position[1])
                name.uppicture(pictureOff)
                name.downpicture(pictureOn)
                name.setScale(12,3.6)

And afterthat I call the def like this way:
Code:

createButton([0.25,0.8],'button01Off.jpg', 'button01On.jpg', 'button1', 2)
So I want to set the button-name manually. This works fine.

But now I want to define onbuttondown events, but this does not work!

If I write:

Code:

def onButton(obj,state):
 if obj == viz.VizButtonLabel(5):
  viz.scene( 2 )

then it works. But this isn't a very good solution. Because if I create more and more objects, I had to change the number of the buttonlabel.

So I want to write:

Code:

def onButton(obj,state):
 if obj == button1:
  viz.scene( 2 )


But this does not work! I get the error message: global name 'button1' is not defined.


I don't know how to solve this problem.

I only want to create a lot of buttons and want to set the name of the button over a parameter. And afterthat I want to check the onbuttondown event via the buttonname.

Thanks for all your answers...

farshizzo 07-13-2009 09:52 AM

Your createButton function should return the button object and you should save the object in a global variable with the name you want. For example:
Code:

def createButton(self, position, pictureOff, pictureOn, name, szene):
        self.Position = position
        self.PictureOff = pictureOff
        self.PictureOn = pictureOn
        self.Name = name
        self.Szene = szene
               
        name = viz.add(viz.BUTTON, scene=szene)
        name.translate(position[0],position[1])
        name.uppicture(pictureOff)
        name.downpicture(pictureOn)
        name.setScale(12,3.6)
        return name

button1 = createButton([0.25,0.8],'button01Off.jpg', 'button01On.jpg', 'button1', 2)


lilio 07-13-2009 10:00 AM

Hi,

thanks for your answer. But this does not work. :mad:

For example:

I've got this class:

Code:

import viz

class Button(object):
       
        def createButton(self, position, name):
                name = viz.add(viz.BUTTON)
                name.translate(position[0], position[1])
                return name


and this start.py

Code:

import viz
import button

viz.go()


button = button.Button()

button.createButton([0.2,0.5], 'testbutton')

def onButton(obj,state):
        if obj == button.testbutton:
                print 'test'

viz.callback(viz.BUTTON_EVENT,onButton)

So I want to create a button with the name testbutton. And afterthat I want to check if the testbutton is clicked.

In my example, the button got the name 'testbutton'.

But then, I've got the error message: 'Button' object has no attribute 'testbutton'


What is wrong??

:confused::eek:

farshizzo 07-13-2009 10:26 AM

You need to assign the return value of the createButton function to a variable:
Code:

import viz
import button

viz.go()


btn = button.Button()

testbutton = btn.createButton([0.2,0.5], 'testbutton')

def onButton(obj,state):
        if obj == testbutton:
                print 'test'

viz.callback(viz.BUTTON_EVENT,onButton)


lilio 07-13-2009 11:13 AM

Yes it works!

Thank you very much....


All times are GMT -7. The time now is 11:40 PM.

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