Blender Addon: Export GameMaker 3D Model Script

As I discussed in a recent blog post, after becoming fed up with the traditional process of importing 3D models into GameMaker, I decided to write an addon for Blender which would allow me to go directly from 3D design to GameMaker itself, with no need for middleware that doesn’t meet all of my needs, modelers that aren’t full-featured, or clunky import scripts. Rather than go into all of that again here, I’d simply like to present the script itself, and a short tutorial on how to use it.

First, the script! Get it here: io_export_gamemaker3d_1_4_6.py

Note: If you’re looking for a script which will output models directly to GameMaker’s internal model format, I’ve made one for that too, which you can find here.

Installation

Installation for this script works just like any other Blender addon – namely, you download it and save it somewhere, then open up Blender, go to File > User Preferences…, click the Addons tab at the top, then click the button at the bottom of the window that says Install from File…

Blender Addons window

Once the script is installed, click the Import-Export button on the left side of the window to help find it, and then tick the small box to the right to enable it. If you don’t want to have to do this every time you start Blender, click the Save User Settings button at the bottom left of the window. Close the User Preferences window, and you’re ready to start exporting!

Preparation

Once you’ve got a model built that you’re ready to export to GameMaker, there are a few final steps you’ll want to take to ensure that things turn out the way you want. Make sure you read over these before you export your object.

My shiny new 3D building in Blender

First, make sure the object is oriented around the origin exactly how you want it to be in the game. Consider the world origin in Blender (0, 0, 0) to be the pivot point for your model, and orient it accordingly.

Second, make sure the object is rotated and scaled properly. Again, make it exactly as you  want it to be in its default state in the game.

Once you’ve got everything positioned correctly, make sure you’re in Object mode and in a 3D view, click Object > Apply > Rotation & Scale. Then click Object > Apply > Location. These options are also available by hitting CTRL+A. This will move the object’s axis to the origin, and will reset the rotation and scaling to match how the object is currently oriented.

Another thing you might want to do is add an Edge Split modifier to your model. This will allow you to have absolute control over how the normals of your model translate into GameMaker, and I highly recommend it. With this modifier enabled, you can mark edges on your model that are to be sharp or not however you like. It’s way better than just hoping the edges turn out looking right based on their default smoothing angles.

Finally (and also optionally) get the object’s UV map set up how you like it, for texturing. The export script will not include UV coordinates if your object doesn’t have a UV map attached.

Usage

You can find the new GameMaker Model Script export in the File > Export menu. And though I’ve never had any major problems here, I recommend saving your work before exporting, just in case.

Exporting my building using the new script

Select your object and then launch the script. You’ll be met with a (hopefully) familiar file-picker dialog, with a few options on the lower left side. The default settings should be fine, but you can tweak them if necessary. Most should be pretty self-explanatory.

Make sure not to overwrite anything you’d like to keep, and hit Export to finalize. As long as nothing horribly wrong happened, you should now have a new text file with GML in it!

The build script for my exported building

Dropping the script into GameMaker Studio

Open up this text file and copy the contents into a new script in GameMaker. Obviously, you can make changes to it to suit your needs, but by default, when you call the script it will create the new model and then return the model’s resource ID. You can then use this to draw the model, or once you’re done with it, destroy it.

The new building model in-game!

Further Development

I have a few small ideas for options to add to the export process, and of course, if you need to add anything to the script yourself, it’s there for you to do so. If you make any major changes that you feel like I should know about (or if you find any bugs) please get in touch and let me know!

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.

33 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 - 358 days 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 - 357 days 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 - 357 days 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 - 357 days 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 - 357 days 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 - 356 days ago

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

    1. Martin - 354 days 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 - 302 days ago

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

    1. Martin - 301 days ago

      Glad it has helped, Dax! :)

  10. JDoe - 295 days ago

    I keep getting error location

  11. JDoe - 295 days ago

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

  12. Dax - 295 days 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 - 295 days 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 - 295 days ago

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

    1. Martin - 290 days 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 - 280 days 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 - 295 days ago

    in case it helps, my Blender is 2.77

    1. Martin - 290 days ago

      Replied to the wrong person here, sorry!

  16. Terrence - 258 days 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 - 257 days ago

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

    2. Martin - 257 days 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 - 156 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 - 44 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 - 43 days ago

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

Leave a Reply