1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

Discussion [Guide/Project] Exporting from Blender to GMS 2 3D

Discussion in 'GameMaker Studio 2 Community Tech Support' started by Pixelrobin, Nov 21, 2016.

  1. Pixelrobin

    Pixelrobin Guest

    NOTE: This is for the BETA version of GMS 2, and things might change over time. Also, the script is not at all finished yet, but usable under certain circumstances. I will be uploading new versions and editing this post to accommodate them accordingly.

    NOTE 2: I am considering making some stuff for the marketplace at a small price at one point... more deatils later I guess.

    Thanks to Michael's post, I was able to take Martin Crownover's script and edit it to make some progress on a script to export 3D models from Blender for use in Gamemaker Studio 2. I thought I would release it early so people can mess around with it and offer feedback as development continues.

    Currently, as 3D models or included files aren't supported, this exports a txt file as a script. You will need to open the file and copy/paste the contents in a new script within your project. Also, vertex colors and custom properties aren't supported yet either, it just defaults to white.

    Downloads
    Blender plugin script
    Example GMS2 project

    Installation and Exporting
    First and foremost, I highly recommend reading Michael's guide mentioned above. This assumes that you have complete knowledge of what was written there. This also assumes you're familiar with blender and it's interface. Then, follow the guide on Martin's blog mentioned above to install the plugin into blender and export to a Gamemaker script. Things from there are pretty much the same here. However, there are a few things to note:
    - I added the export option to flip z coords for use when the Camera's up vector is (0, 0, 1). If you are following Michael's guide, leave this on.
    - The "alternate script format" export option is currently broken, don't use it. I need to figure out how to edit a vertex buffer after creating it initially.

    Using this in your game
    I assume you know what the code is doing in these steps, if not, read Michael's guide.
    1. Remember not to use the alternate script format option yet. Uncheck it if it is checked.
    2. Create a script called 'model' and copy the contents of the exported file into it.
    3. If you have a texture and a model with uv mapping, create a sprite with it called 'spr_texture'
    3. Create an object called obj_model, use the following code in the create event:
    Code:
    // Create the format
    // MAKE SURE THIS IS IN THE SAME ORDER!!
    // I will add support for other orders and custom attributes later
    vertex_format_begin();
    vertex_format_add_position_3d();
    vertex_format_add_normal();
    vertex_format_add_color();
    vertex_format_add_textcoord();
    format = vertex_format_end();
    
    // Create the vertex buffer using the script 'model' and store it in the variable 'v_buffer'
    v_buffer = model( format );
    
    4. In the same object, put this code in the draw event, if you don't have a texture and didn't create a sprite texture, replace 'sprite_get_texture( spr_texture, 0 )' with '-1'
    Code:
     // Draw the buffer
    vertex_submit( v_buffer, pr_trianglelist, sprite_get_texture( spr_texture, 0 ) );
    
    5. Follow Michael's guide to setup the 3D scene and camera, (Step 1 in that post) and set up matrices to transform your models (Step 3).

    If you followed everything correctly, you should see your shiny 3D model in your game. If you don't, you might need to reverse the normals or scale it to a size more appropriate for the camera.

    Changelog
    11/20: Initial post

    I'm willing to help with questions below if you have any! I will make a better guide later too. I need to sleep for now.
     
    Last edited by a moderator: Nov 23, 2016
    Lerner, Kenjiro, Shut and 4 others like this.
  2. Manuel Di Iorio

    Manuel Di Iorio Member

    Joined:
    Nov 12, 2016
    Posts:
    17
  3. FredFredrickson

    FredFredrickson Member GMC Elder

    Joined:
    Jun 21, 2016
    Posts:
    120
    Cool, looking forward to trying this out. Thanks for giving me a heads up on Twitter about this too, appreciate it!
     
  4. Shut

    Shut Member

    Joined:
    Jun 21, 2016
    Posts:
    184
    Awesome job on converting that script, just tested it and it's working great. :)
     
  5. slojanko

    slojanko Member

    Joined:
    Jun 20, 2016
    Posts:
    113
    Do you plan on fixing the normals?
     

    Attached Files:

    Last edited: Aug 18, 2017
  6. Shut

    Shut Member

    Joined:
    Jun 21, 2016
    Posts:
    184
    YYC Windows won't compile the models, I think it used to work fine before the 2.1 update, but I'm not 100% sure. Any possible solutions?
     
  7. rwkay

    rwkay YoYo Games Staff YYG Staff

    Joined:
    Apr 12, 2016
    Posts:
    1,049
    If the models are pages of GML code then I suspect that the C++ compiler is taking a long time to compile (as there is a lot of code to compile).

    Personally I would not generate code for these things but generate binary data files that are the vertex buffers themselves and just load them at run time.

    Russell
     
    Shut likes this.
  8. Shut

    Shut Member

    Joined:
    Jun 21, 2016
    Posts:
    184
    @rwkay Thank you for the advice, though since I'm almost half way through the project and using the script/GML code method, is there any chance that this could be fixed in future update?

    I believe it wasn't an issue in the previous version and I could compile just fine with Windows YYC, the recent update (v.2.1) caused this problem.
     
    Last edited: Aug 28, 2017
  9. gnysek

    gnysek Member

    Joined:
    Jun 20, 2016
    Posts:
    1,313
    Compile time should depend on Visual Studio not on GMS2 in this case.
    And buffers are much much better, as they take less space (and can be loaded from external file).
     
    Last edited: Aug 28, 2017
    Shut likes this.
  10. rwkay

    rwkay YoYo Games Staff YYG Staff

    Joined:
    Apr 12, 2016
    Posts:
    1,049
    I am taking a look at this now... but the fundamental issue is the way in which it is being done....

    I would recommend a half way house - use the VM build to generate the vertex buffers and write the files out (copy them to buffers and write the buffers out to files) then in the YYC version load the files.

    Russell
     
    Shut likes this.
  11. rwkay

    rwkay YoYo Games Staff YYG Staff

    Joined:
    Apr 12, 2016
    Posts:
    1,049
    I have looked into this quite a bit today and even if I upgrade clang to v4.0 (which is the latest stable release) then this takes around 20mins to complete because the file is so big.

    Doing the models this way is just not viable and if you insist on doing it then use the VM, the only viable way to do this using YYC is to load binary data.

    Russell
     
    Shut likes this.
  12. Shut

    Shut Member

    Joined:
    Jun 21, 2016
    Posts:
    184
    @rwkay Thank you very much for looking into this!

    Just tried this and looks like the easiest solution for me. Everything works great now and YYC compiles in just 6 seconds which is amazing. :)

    I apologize for wasting your time.. I really thought that it had something to do with the update. Thanks again!
     
  13. rwkay

    rwkay YoYo Games Staff YYG Staff

    Joined:
    Apr 12, 2016
    Posts:
    1,049
    I am sure that the script / exporter for Blender could be modified to output a binary file in any case and cut out the VM middle man.

    Russell
     
    Shut likes this.
  14. MaddeMichael

    MaddeMichael Member

    Joined:
    Jun 20, 2016
    Posts:
    172
    Hey there!
    As convenient as it may seem, I recently made a script for Blender to export the model as a binary file, after having issues with large script models
    I've popped it on dropbox for now, here's the download: https://www.dropbox.com/s/84jf9bga4xpi0p5/gms2BufferExportv2FINAL.py?dl=1

    In order to load an exported model, I just use this script I made:
    Code:
    /// @func model_load(filepath, vformat)
    /// @desc Loads a model in via buffer.
    /// @arg file
    /// @arg vformat
    if(!file_exists(argument0))
    {
        show_debug_message("File not Found! " + argument0);
        return -1;
    }
    var buff = buffer_load(argument0);
    
    var vbuff = vertex_create_buffer_from_buffer(buff, argument1);
    
    buffer_delete(buff);
    return vbuff;
    
    It's basically a heavily modified version of @Pixelrobin's script, but I've reconstructed a heck of a lot of it - fixed a memory leak, and added vertex colour support too!
    You can also specify what vertex attributes are exported - just remember to adjust your vertex format as necessary - by default, it exports as a full-fat position3d-normal-colour-texture model. The types are always in that order, by the way, to help with format definition.

    There's a few other useful options too - "Export Selected", "Reverse cull order", and "Apply transforms". I've found them pretty handy.

    It hasn't had any errors for like 6 days, so hopefully it could work for you! If you have any issues with it, let me know, and I'll see what I can do.
    I'm probably going to upload it as a (free) marketplace asset shortly, I just want to set up a demo project first.

    EDIT: Hang on, I realised I left "sdm" in the load script - it's just a macro for show_debug_message - fixed now.
     
    Last edited: Aug 28, 2017
    Shut likes this.
  15. Shut

    Shut Member

    Joined:
    Jun 21, 2016
    Posts:
    184
    @MaddeMichael Sounds amazing! I'm going to test it now, thanks for sharing! :)

    I was just writing my own converter (current export format to buffer) but it's taking too long, about 10 mins to convert just 1 model. Your solution sounds much better.

    EDIT: Works great! Thank you so much! It's much easier this way. :)

    If anyone needs a converter for the original script (https://www.dropbox.com/s/c9mrw4zdavchyp1/3D Model Converter.zip?dl=0) I managed to decrease the time it takes to convert. Takes about 5 seconds to convert large models for me.
     
    Last edited: Aug 28, 2017

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice