Vizard 8 » Reference » Graphical user interfaces (GUIs) » vizdragdrop
8.1

vizdragdrop

The vizdragdrop module allows you to drag and drop files on the Vizard graphics window. It supports dropping one or more files at the same time. When a file is dropped an event is generated that provides the pathname of the file and the mouse position to the registered callback function. This can used as a way to add models and other types of resources to the scene at runtime.

 

To see how this works, run the following script and drop the vcc_male.cfg file (found in the resources folder of the Vizard installation) on the graphics window:

import viz
import vizdragdrop

viz.go()

court = viz.addChild('court.ive')

model_path = viz.res.getFullPath('vcc_male.cfg')

vizdragdrop.enable()

def onDropModelFile(e):
   
    if e.filenames[0] == model_path:
        male = viz.addAvatar('vcc_male.cfg',pos = [0,0,5],euler = [180,0,0])
        male.state(1)
       
vizdragdrop.onDropFile('*.cfg',onDropModelFile)

vizdragdrop commands

The following commands enable and disable file drag/drop:

Command

Description

vizdragdrop.enable()

Enables file drag/drop on the Vizard window.

vizdragdrop.disable()

Disables file drag/drop on the Vizard window.

When drap/drop is enabled and a file is dragged on the graphics window a drag/drop marker with "+" sign will appear that follows the mouse cursor. Once the file is dropped a vizdragdrop.DROP_FILE_EVENT is generated. Use the following command for registering a callback function to handle this event:

Command

Description

vizdragdrop.onDropFile(pattern,

                               func,

                              *args,

                              **kw)

Registers the function that gets called when a filename matching the specified pattern is dropped. If pattern is None, then all dropped files will be passed to the function.

 

func: The function being registered. An event object e, described in the table below, will be passed to this callback function.

 

*args: Optional arguments to pass to the registered function.

 

**kw: Optional keyword arguments to pass to the registered function.

The drop event provides a single event object e that gets passed to the callback function. The table below lists the object's attributes:

Attribute

Description

e.filenames

A list that holds the pathname(s) of the file(s) dropped

e.pos_normalized

Mouse position in normalized window coordinates

e.pos_pixels

Mouse position in pixel window coordinates

e.pos

Same as e.pos_normalized

Example

The following example uses the mouse position at drop time to determine where an avatar should be placed. Run the script and individually drop the vcc_male.cfg and vcc_female.cfg files on the graphics window (These files can be found in the resources folder of the Vizard installation). To change the position of an avatar drop it's .cfg file again in a new location:

import viz
import vizdragdrop

viz.go()

court = viz.addChild('court.ive')

male = viz.addAvatar('vcc_male.cfg',euler = [180,0,0])
male.state(5)
male.visible(viz.OFF)

female = viz.addAvatar('vcc_female.cfg',euler = [180,0,0])
female.state(5)
female.visible(viz.OFF)

#Get the full paths to the avatar cfg files to check
#against in the onDropModelFile function
male_path = viz.res.getFullPath('vcc_male.cfg')
female_path = viz.res.getFullPath('vcc_female.cfg')

#Turn off mouse navigation
viz.mouse(viz.OFF)

#Enable file drag/drop
vizdragdrop.enable()

def onDropModelFile(e):
   
    #Determine if the file dropped is either vcc_male.cfg or vcc_female.cfg
    if e.filenames[0] == male_path:
        avatar = male
    elif e.filenames[0] == female_path:
        avatar = female
    else:
        return
   
    #Get the mouse position at drop time and use that to
    #calculate the avatar position
    mouse_pos = e.pos_normalized
    x_pos = 5 * mouse_pos[0] - 2.5
   
    #make the avatar visible and set it's position
    avatar.visible()
    avatar.setPosition([x_pos,0,5])
       
vizdragdrop.onDropFile('*.cfg',onDropModelFile)