Friday 23 January 2015

Metasequoia 4 Plugins – part 4

This time we will create an Import plugin.

I will not use the class technique for creating the plugin.

I wrote a Metasequoia Python script [link] to import MilkShape 3D *.ms3d files and will write a plugin to do the same.

Plugins have access to more of Metasequoia’s inbuilt functions than scripts.

MilkShape 3D released a *.ms3d viewer with C++ source code [link].

We will use the *.ms3d file loading code from the viewer in our plugin to read the *.ms3d file so download msViewer2.zip and extract it somewhere.

See the previous posts for the details about the following steps.

Add a new project to the mqsdk solution.

Choose C++ Win32 project, give it a suitable name and click OK.

Click Application Settings, select DLL, Empty project and click Finish.

Open the msViewer\src folder in File Explorer and copy the following files into the project folder.
  • msModel.h
  • msModel.cpp
  • mathlib.h
  • mathlib.cpp

Copy the msViewer files into the project folder

In Visual Studio add the files (existing items) to the project.

Add MQInit.cpp (existing item) to the project.

Add a new blank *.cpp file to the project. We will write our plugin code in this file.

Set the following properties in the project’s Property Pages.
  • If a 64 bit *.dll is required use Configuration Manager to change the platform
  • For All Configurations set the Output Directory and Intermediate directory
  • For All Configurations set the Character Set to Multi-Byte Character Set
  • For All Configurations set the Additional Include Directories to “..”
  • For Debug Configuration set the Debugging Command to path to Metaseq.exe
  • For Debug Configuration set a Post-Build Event to copy *.dll to Import folder

If you don’t set the Post-Build Event you will have to copy the *.dll to the Metasequoia\Plugins\Import folder in File Explorer every time you build the project.

For reference open ImportDXF.cpp in the editor.

Copy and paste the define, includes and function declaration into our plugin’s blank source file.

Delete #include “Strings.h” since we do not need it, add #include “msModel.h” so we can use the functions from it and rename the function LoadMS3D.

MS3D lines 1 to 8

Next in ImportDXF is the DllMain function. We don’t need to initialise any variables when the *.dll is loaded so we don’t need to add this function to our plugin.

Add the required MQGetPlugInID function and change the Product and ID numbers to the ones you chose for yourself.

MS3D lines 9 to 14

Next add the required MQGetPlugInName function and change the text to what you want displayed in the Help>About Plug-ins dialog.

MS3D lines 15 to 19

Next add the required MQGetPlugInType function and make sure the type is MQPLUGIN_TYPE_IMPORT.

MS3D lines 20 to 24

Next add the MQEnumFileType function which is required for Import plugins and change the text to the file type you are importing.

MS3D lines 25 to 32

Next add the MQEnumFileExt function which is required by Import plugins and change the extension to your file extension.

MS3D lines 33 to 40

Next add the MQImportFile function which is specifically required for Import plugins.

Change the name of the function called in this function to the function name you declared at the top of the file.

MS3D lines 41 to 48

Now we need to write the LoadMS3D function to do the importing of the file.

We need to study the msViewer code to see how the msModel class is used.

We need to create a variable of the msModel type and then call the msModel::Load method.

To begin with we will only write code to load the model into an msModel object and see if the plugin builds and test successfully.

LoadMS3D function 1

Build the project and if successful place a breakpoint on line 52 inside the LoadMS3D function.

You place a breakpoint by clicking in the gutter area of the text editor’s left margin. A red dot indicates a breakpoint.

Breakpoints only work with the Debug configuration and only when you use the debugger.

Set a breakpoint

Run the debugger to start Metasequoia.

Metasequoia should report any problems loading our plugin on start up.

Failing to add MQInit.cpp to the project causes the error shown below.

Plugin failed to laod

If the plugin loads okay open a MilkShape 3D *.ms3d file.

The debugger should pause Metasequoia and return to Visual Studio at our breakpoint.

Execution paused at breakpoint

We have paused the plugin before the *.ms3d file is loaded so we can execute the lines one by one and see whether the *.ms3d data is saved in the variable, model.

Click DEBUG>Step Over or press F10 to execute one line of code.

Repeat until the yellow arrow is at line 55.

When line 54 was executed the *.ms3d file was read and model was populated with the data.

You can see the data in model by looking at the Locals window below the editor.

ms3d file loaded

We know the file loaded since the next line to be executed will return TRUE.

Stop the debugger and return to the editor.

You can remove the breakpoint by clicking on the red dot.

Now we have to write code to transfer the model data to a Metasequoia Object.

Rather than write the code directly in the LoadMS3D function I will write it in another function that will be called in LoadMS3D.

Because I am writing the new function below LoadMS3D in which it is called I have to declare it in the file somewhere above LoadMS3D.

Forward declaration of new function

I’ll post the completed function without explanation of the code.

I basically copied the ImportDXF code, used the SDK API reference and studied msModel.h.

As for the C++ I had to use trial and error a bit because my knowledge of classes and pointers is not good.

ConvertToMQ function

At this stage the plugin only imports the vertices and faces but since the msModel class stores materials and groups the plugin could be expanded to use those as well.

I expanded the plugin to use the UVs and materials from the *.ms3d and also to show a dialog for adjusting the scale. The code can be found at GitHub [link]. 


prev | next | first

No comments:

Post a Comment