Now that you have the Visual Studio project setup and created the main class for your plug-in, you will need to make the class compatible with Vizard. The SDK provides a virtual base class called viz::Extension. Your main class must derive from viz::Extension and override certain methods to expose functionality to scripts.
Assuming you used the name MyExtension for your main class, the following code shows the bare minimum needed to derive from the viz::Extension class:
#include <viz/Extension>
class MyExtension : public viz::Extension
{
public:
MyExtension(void);
virtual const char* getName() const { return "My Extension"; }
protected:
virtual ~MyExtension(void);
};
The only method you are required to override is getName, which simply returns the name for your plug-in.
See the viz::Extension sub-topic for a list of all the available viz::Extension methods you can override.
In order to make your extension class accessible to Vizard, you need to create a DLL export function which Vizard can use retrieve a handle to your extension. Simply add the following code to your extension cpp file:
extern "C" __declspec(dllexport) viz::Extension* CreateVizardExtension(viz::Data &data)
{
return new MyExtension();
}
This CreateVizardExtension function will be called by Vizard when your DLL is loaded. All it needs to do is return an instance of your extension class.
You might have noticed that the export function above takes a data argument of the type viz::Data. Most methods of viz::Extension accept a viz::Data argument. This data object contains optional parameters that were used when calling the method from the script.
The viz::Data class is simply a container object that can hold any number and type of values. Each value is accessed by a string name. For example, if the data object contains an integer named "command", the following code would be used to gain access to the value:
int command = data.get<int>("command");
The viz::Data object has the following convenience methods for accessing common data types:
Convenience Methods |
bool getBool(const std::string &name, bool defaultValue = false) const |
int getInt(const std::string &name, int defaultValue = 0) const |
unsigned int getUInt(const std::string &name, unsigned int defaultValue = 0) const |
float getFloat(const std::string &name, float defaultValue = 0.0f ) const |
double getDouble(const std::string &name, double defaultValue = 0.0 ) const |
const char* getString(const std::string &name, const char* defaultValue = 0 ) const |
All the viz::Data objects passed to viz::Extension methods will contain the following values:
Name |
Type |
command |
int |
message |
const char* |
x |
float |
y |
float |
z |
float |
w |
float |
The following code shows how you could access these values within each method:
int command = data.getInt("command");
const char *message = data.getString("message");
float x = data.getFloat("x");
float y = data.getFloat("y");
float z = data.getFloat("z");
float w = data.getFloat("w");
Many of the methods that accept a viz::Data object also allow returning values back to the script. The data that is returned can by any Python object. To return data you must first include the <viz/python> header file:
#include <viz/python>
The return value must be added to the data object using the string macro PYTHON_RETURN_OBJECT. For example, if you wanted to return a Python string value from your method, you would use the following code:
data.set( PYTHON_RETURN_OBJECT , PYTHON_STRING("some string value") );
Note: The data object will steal a reference to the PyObject that is added to it.