Blender Addon: Export GameMaker 3D Model Script

After becoming fed up with the traditional process of importing 3D models into GameMaker: Studio, I decided to write an addon for Blender which would allow direct export of 3D data to GameMaker – eliminating the need for weird middleware or slow/clunky import scripts. You can download this addon below, find instructions for how to use it, and this is where I will post all future updates to this project.

Here is the most up-to-date version of the addon (last updated July 16, 2017): Blender addon script 1.5.9 

Installation

Download the script, then open up Blender and go to File > User Preferences, click the Addons tab at the top, then click Install from File.

Once the script is installed, find it in your list of addons (type “gamemaker” in the search box, or click the Import-Export button tab to the left), and tick the small box on the right to enable it. Click Save User Settings and close the window.

Now you’re ready to start exporting models to GameMaker: Studio!

Export

When you’ve got your 3D model ready to export, select it, then click File > Export > GameMaker 3D (.txt).

Next you’ll see the standard Blender save dialog, but it’ll have some new options specific to the GameMaker export addon in the panel on the left.

There are a number of formats and model types to choose from now – here is a brief explanation of what each of the settings does:

Format

The desired output format of the model. Basically, how you want your output data formatted.

  • GML Script – The model will be output as a self-contained GML script, which can be added to your game as a script that you call to load your model. The script will return the new model’s id.
  • GameMaker Model – The model will be output in GameMaker’s built-in model format, and can be loaded to your game using d3d_model_load().
  • Raw Vertex Data – The model will be output as a list of vertex data, which follows this format: x, y, z, x normal, y normal, z normal, uv x, uv y. Each line is a vertex, and as with the other models, the number and order of vertices will depend on the model type you select in the next dropdown.

Model Type

The type/style of the exported model.

  • Triangle List – The model is written as a list of its triangles.
  • Line List – The model is written as a list of vertex pairs, one pair for each of its edges.
  • Point List – The vertex data is written as a list of points, one for each vertex.

Apply modifiers

Whether or not to apply any modifiers that might be on the object before exporting it. Generally, it’s a good idea to keep this on, because otherwise the exported model could differ from what you’re seeing in Blender.

Rotate X by 90

If you happened to create your model with the wrong orientation, or you imported it from a 3D app that uses Y as up (as opposed to Z) then this is a quick way to fix that in the exported model. It will rotate the model 90 degrees on the X axis.

Mirror on Y Axis

In Blender, the 3D space is oriented Z-up – but if you look at how X and Y are oriented, you’ll notice that Y runs positive in the opposite direction of how it would in a GameMaker game that is also Z-up. Therefore, if your model isn’t symmetrical, it will get mirrored going from Blender to GameMaker. This option flips the exported model by mirroring it on the Y axis.

Flip UV Vertically

Similar to the issue mentioned above, Blender’s UV coordinates also run opposite to GameMaker’s on the Y axis, so you either need to flip the UV coordinates (which is what this option does) or flip your texture map.

Scale

If your game uses a 3D scale that is different from Blender, you can use this option to scale the exported models, without having to work on models that are uncomfortably large or small, and without having to depend on scaling objects in GameMaker as you draw them, which often seems to cause the object’s normals to appear incorrect.

Make Coordinates Relative

This option will only appear if you are exporting the model as a GML script. If enabled, it will add some extra variables to the script that lets you transform/translate the model when you call the model script in your game. This can be useful if you want to add multiple copies of the object to your game, but in different places, and don’t want to perform the transform/translation at runtime.

Once you’ve chosen an appropriate path, filename, and the settings you want, click Export for GameMaker. The script will output a text file and your export will be ready to bring into GameMaker!

Import

Depending on which model format you chose, there will be different ways to get your exported model into GameMaker at this point. If you chose GameMaker Model or GML Script, you can follow the instructions below to get your model data into GameMaler. If you exported the model as raw vertex data, it will be up to you to consider how you want to import (and utilize) the data.

Importing a GameMaker Model

This is the easiest way to get your model into a GameMaker project.

Just call the d3d_model_load() function and point it to your model. Optionally, you may want to add the model file to your Included Files in the resource tree, so it always gets packaged with your game. You can also change the filename and/or extension to something else – GameMaker: Studio doesn’t care what extension the file has, as long as the data inside the file remains intact.

Importing a GML Script

If you’d rather have your models reside in your project as scripts (or, perhaps, you want to do some further manual tweaking of your model in GameMaker’s script editor), you can use the GML Script option.

Open the text file the addon created and select everything there, then create a new script in your GameMaker project, name it something appropriate, and paste the model data into the script.

Now just call the script when you’d like to load up your model, and store the returned model id to use later for drawing, etc. Your script will have a few more arguments to it if you chose the Make Coordinates Relative option when you exported your model.

Tips

While this Blender addon tries to help alleviate a lot of the problems with importing 3D objects to GameMaker from other 3D applications, there are still a number of things to be aware of to ensure that the best results. Here are a few tips that might help you:

  • Make sure your object is oriented around the world origin (0, 0, 0) in Blender exactly how you want it to be in the game – this will be your object’s default rotational axis. Additionally, make sure your object is rotated and scaled properly to your game’s setup.
  • It’s a good idea to apply your object’s location, rotation, and scale before exporting. You can do this by selecting your object, then pressing CTRL+A, then applying each of these properties. This will move the object’s origin to 0, 0, 0, and also normalize the object’s scale and rotation.
  • The Edge Split modifier can give you more control over how your object’s normals appear in GameMaker. You can read more about using it here.
  • Don’t forget to set up your object’s UV’s, if you’re planning on drawing it with a texture applied. Blender has many tools to help you do this, which you can read about here.
  • Although this addon is built in a way that it should never perform any permanent, destructive operations on your models, always be sure to save your work in Blender before exporting, just in case

Further Development

Although this addon has reached a pretty good place in terms of features, there’s always more to add. Currently, I’d like to add support for GameMaker Studio 2/vertex buffers, but that’s all that’s on my list. If you have any feature requests or bug reports, please get in touch and let me know!

Additionally, you’re welcome to add your own functionality, which should be relatively easy thanks to Blender’s Python scripting system. If you make any big improvements or additions that you think I might like to see, please drop me a note.

This script was built from an open source script originally written by Jeff LaMarche. I have contacted Jeff and he has indicated that he requires no credit or compensation for utilizing his script in this way. You can view the original script’s license here.

Changelog

1.5.9

  • Added proper changelog!
  • Fixed a scripting error with UV coordinates when exporting in GameMaker Model format

1.5.8

  • Re-wrote many parts of the script, removed lots of old code and outdated comments
  • Added the ability to export to GameMaker Model and Raw Vertex Data formats
  • Added the ability to export linelists and pointlists
  • Fixed an error that prevented models from being triangulated after modifiers were applied
  • Renamed some of the functions to (hopefully) be a bit more obvious about what they do
  • Changed the name of the script in the Export menu
  • Changed the button label in the export dialog

1.4.6

  • Small change to the name of the script in the Export menu
  • Updated tracker URL

1.4.5

  • Moved and re-wrote some functions to happen at better places in the order of operations
  • Added an option for exporting the object with relative coordinates
  • Further cleanup of various parts of the script

1.3.0

  • Removed some redundant code
  • Reformatted some indentation

1.1.0

  • Initial release

36 comments:

  1. Update to Blender addon for exporting 3D models to GameMaker | Martin Crownover - Over a year ago

    […] can download the updated version of my Blender addon at the original page, which I’ve changed to include the newly updated addon. That will always be the place to go […]

  2. epicguy474 - Over a year ago

    when i try to do this the export menu’s thing is gray and i cant use it. Anyone got a fix?

    1. Martin - Over a year ago

      Hi epicguy – make sure that the model you’re trying to export is selected, and that it is a mesh. You may need to convert it to a mesh prior to exporting it if it contains live text, curves, nurbs, etc. Just make sure to keep a copy of it somewhere else, as converting it to a mesh may destroy some editing capabilities.

  3. AX - Over a year ago

    Well,
    first thing I don´t find the updated version of the plugin and
    second if i drop the code into the draw event of a new object and put the object into the room, I don´t see it in the room (other models which I have done by myself are shown.)

    PLS Help!

    1. Martin - Over a year ago

      I’d have to see some of the project to tell you what’s wrong, unfortunately.

  4. Lynn - Over a year ago

    I’m confused; how do I download it because when I click on it, it opens a new window and has the scripting in it? Do I save ‘as link’ or something? Nonetheless, thank you for creating this…now I just need to figure out how to download it.

    1. P. - Over a year ago

      Changing the name of the file from “.txt” to “.py” worked for me

  5. Lynn - Over a year ago

    Is there a way you can make a video of this because as I’m trying to ‘export’ my object, I’m not able to select the ‘Game Maker 3D Model Script(.txt)’ option and I don’t know what I did wrong.

    1. Martin - Over a year ago

      Hello Lynn – sure, I could do that. You’ll have to give me a day or two though. Got a full plate right now!

  6. Dax - Over a year ago

    Hi Martin. Firstly, thank you for the script. I’m a complete newbie to 3D with GameMaker. I’m using MagicVoxel to export an OBJ into Blender, which includes colour materials. Can you tell me, if I use your script, will the information in the .txt file it creates include this colour information?

    1. Martin - Over a year ago

      Hi Dax! After investigating MagicaVoxel, I’d say yes, you should be able to do this – just use the PNG that MagicaVoxel exports with your model as the texture in GameMaker.

      You’ll need to resize it to be a square 256×256, but luckily you can do this in GameMaker’s image editor relatively easily. Just load up the image into GameMaker and open it in the built-in editor. Then go to Transform > Stretch… and uncheck Keep Aspect Ratio, change the height to 256 pixels, and set the quality to Poor.

  7. Dax - Over a year ago

    Thanks for the reply Martin. So, just to clarify, MagicaVoxel outputs the following files:

    model.obj
    model.mtl
    model.png

    Blender will use your script and product a .txt file.
    I can copy and paste this .txt file into a script in GameMaker and display the model (without colour)

    To get colour on my 3D model you’re saying resize the model.png to 256×256 and import it into GameMaker
    Can you explain how to link model.png to my 3D model in Gamemaker? Do I need some sort of extra file like a UV Map?

    Sorry for the newbie questions, but I’ve never done it before ;-)

    1. Dax - Over a year ago

      ignore my silly comments – it worked!!! Resized to 256×256!

      Note: I used Qubicle Voxel Editor > export OBJ

      Qubicle’s looks more like a texture map than MagicaVoxel? MagicaVoxel’s png looks more like a palette?

    2. Martin - Over a year ago

      Awesome!

      MagicaVoxel’s texture is actually a normal texture – it’s just set up to be 1 pixel high, with each color slot being assigned UV coordinates in the center of each appropriate pixel. Because of the way UV’s work, you can just stretch this out to be compatible with programs like GameMaker, which require textures to be square, and the UV’s will still land in the middle of each spot.

      Anyway, glad you got it working! :)

  8. Dax - Over a year ago

    Ah, that’s good to know. I actually love MagicVoxel – very easy to use and recommended

    1. Martin - Over a year ago

      Nice – I’ve only used it a few times, but I’ve been impressed. I will definitely use it if I ever have any voxel modeling projects pop up!

  9. Dax - Over a year ago

    Can’t thank you enough for this script Martin – saves me sooooo much time!!

    1. Martin - Over a year ago

      Glad it has helped, Dax! :)

  10. JDoe - Over a year ago

    I keep getting error location

  11. JDoe - Over a year ago

    error location unknown when i try to export in both d3d and txt

  12. Dax - Over a year ago

    Have you tried specifying exactly where you want to save the exported files? For example I had to tell it to save to my documents folder

  13. Dax - Over a year ago

    In addition to selecting say My Documents as a location to save to, fill in the box below with your own filename

  14. JDoe - Over a year ago

    yes i did but still won’t save i;m using blender v2.70 if that helps

    1. Martin - Over a year ago

      In reply to Dax.
      Hmm… I’ve never had that happen. Have you tried a different location, just to check?

      I just downloaded the script and freshly installed it, and it works just fine saving to my desktop. Does your user name on your computer have any odd characters in it that might be throwing off Blender’s file system handling? Try saving to just c:/ (or some other root location) where there can’t be any folder name issues.

      Also, make sure that when you save, at the top of the save dialog: the upper input box has the location and the bottom box has the file name.

      Let me know if this helps!

    2. Martin - Over a year ago

      One more update to this issue – someone else had a similar problem, and the solution was to create a new Blend file and append the file with your models, then try exporting again. Not sure what the problem is yet, but this helped another person, so I figured it’d be worth mentioning here. Good luck!

  15. Dax - Over a year ago

    in case it helps, my Blender is 2.77

    1. Martin - Over a year ago

      Replied to the wrong person here, sorry!

  16. Terrence - Over a year ago

    Hi, Martin!
    I’m finding a bit of trouble in the rendering portion of the 3D object, which my subsequently label my problem as a Game Maker problem that can easily be remedied, with help. My problem is, when attempting to draw the 3D model in-game, it doesn’t show up! Here are the steps I’d taken before meeting this problem:

    1) I went through the process of opening a .OBJ model in Blender, using the addon to export the model as a .txt file, and copying the script included in the .txt file to a script in my game.
    2) (This may very well be where my limited knowledge of 3D in GM:Studio got the best of me) I called the script in a code box in the Create event of the object that I chose to render the model. To call the script I simply typed out the name of the script (in this case it’s “model_build_tree();”) and that was all I kept in the object’s Create event.
    3) I went to the object’s Draw event, placed a code box, and typed out d3d_model_draw(temp,x,y,0,global.texture_tree) (I have a solid green texture saved as “global.texture_tree”, this for sure isn’t the problem) and left the draw event at that. I already have d3d started and everything for the room of my game, as I am able to see 3d blocks I’d previously made. So at this point, I placed the object, with only two lines of code (one in the Create event, and one in the Draw event) into my room.
    4) I ran the game, and in my room I could only see the 3D blocks I had placed previously, no 3D model of a tree anywhere to be seen.

    There is a very good chance that I missed a step setting up the script to actually render the 3D model in game, so I’d like you to enlighten my on whatever else I need to add to the coding of my game to get the model to show up, if you don’t mind, that is! Thank you in advance!

    1. Terrence - Over a year ago

      Actually, you can delete this question; I’ve got it figured out!

    2. Martin - Over a year ago

      Hi Terrence – glad you got it figured out! Did it have to do with how you were loading/drawing the info from the script?

  17. Azurexis - 270 days ago

    Thank you so fucking much man!!! This helped me a lot! <3 <3 <3
    This is so much more easy than converting .obj to something and in the end it's still shit!
    This is easy, fast, clean and very effective! THANK YOU!

  18. Mike - 159 days ago

    Shocker. Fresh installed Blender 2.78. Fresh installed this add-on. Imported an obj. Tried to export and what do you know: about four errors in the traceback. Shame…

    1. Martin - 157 days ago

      Hi Mike – works fine for me. What sort of errors did you get?

  19. Jorge Enrique Hawkins López - 110 days ago

    Some of the triangles of my model disappears when exporting to GameMaker format…

    1. Jorge Enrique Hawkins López - 110 days ago

      Nevermind, forgot to recalculate normals. Great job with the plugin!

    2. Martin - 104 days ago

      Hi Jorge, glad you figured it out! Let me know if you find any issues – I’ve got a new version almost ready for release too!

Leave a Reply