Search Unity

Bezier Curve from Blender as Path in Unity

Discussion in 'Asset Importing & Exporting' started by commmon, Jul 22, 2020.

  1. commmon

    commmon

    Joined:
    Apr 17, 2020
    Posts:
    2
    Hello - I just started digging into Unity seriously, and so far have had great luck finding answers in these forums. However, I seem to have come across a question I can't find an answer for, and am hoping someone may be able to help.

    I am working on a fairly simplistic ball-rolling game, where players control a ball down a semi-tubular track. I created the initial shape of the track using a bezier curve in Blender, then did a series of extrusions, cuts, and manual manipulations to get the actual mesh I'm using as a game object in Unity.The mesh works great, and my player and obstacle objects interact with it as expected.

    The problem I am having, is that the track is fairly twisty, and my follow camera keeps ending up interacting badly with the walls of the track.

    The solutions I have tried so far include
    1. Adding a bit to my camerafollow script, so that the camera moves closer to the player to avoid clipping from other objects
    2. Using cinemachine to have my camera follow the velocity heading of the player, instead of simply offsetting from it's transform
    These both improve the situation, but still encounter problems at different points, depending on gameplay.

    It occurs to me that in Blender, I still have access to the initial bezier curve, which would give me a perfect path for the camera to follow, exactly down the center of the the track at all points.

    I have tried exporting the bezier curve with no geometry attached, and while it imports with no errors, it also imports with literally nothing. If I export the curve with a little bit of geometry attached, then all that I can see or access in Unity is the mesh.

    Does anyone happen to know if there's a magic way that I'm missing to be able to export/import/use my initial curve as a camera path in Unity, or will I need to manually redraw a similar curve to use as a Dolly Path for my camera?

    First post on this forum, so please let me know if a different section would be better suited, or if there's any further information you may need - thanks in advance for any help!
     
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    Unity does not support import of splines via FBX.
    You could export the spline data from blender via XML / JSON / CSV with a pytong script.
    Then have a script in unity read thous data file and recreate the curve inside of unity.
    The question is, is it worth the time to create thous scripts or is it a one time thing that can be done by hand faster.

    Maybe you can bake the spline points into a mesh and read the mesh vertecy positions in unity to create the spline.
     
  3. nicholascarrow

    nicholascarrow

    Joined:
    Feb 20, 2018
    Posts:
    2
    Hi Commmon,

    I figured out a pretty consistent way to import splines into unity from blender, but it's not free. It's really convoluted, but ultimately pretty easy once you get it set up. I'm going to provide a detailed explanation assuming no knowledge of anything at all in case someone like me stumbles across this (other than assuming you already have a bezier curve in blender to work with).

    https://assetstore.unity.com/packages/tools/utilities/dreamteck-splines-61926#description

    First, you need dreamteck splines. It's on sale right now for about $18, usually its $35. Dreamteck can import splines into unity.

    Once you buy this plugin, go to the project manager window in unity, import it, and then import the examples (by clicking on Examples in the Dreamteck/Splines folder). I needed a rollercoaster example so I used that one to figure out how to convert my coaster into a working spline. I know Dreamteck has built in tube functionality as well, so it might be worth checking out if you're still on the same project. Here is the documentation:

    https://dreamteck.io/page/dreamteck_splines/user_manual.pdf

    The problem is that dreamteck splines only imports SVG and CSV files. Blender doesn't natively export to either of those formats. This is where the convoluted part comes in.

    https://blender.stackexchange.com/questions/51706/exporting-coordinates-of-vertices-to-csv

    This thread can get you started with exporting to CSV from blender. I had to make some changes to the script they used, so here is my version (formatting might be a little weird on this forum).

    Code (JavaScript):
    1. import bpy
    2.  
    3. outputFile = 'C:/Users/Work/Desktop/YourFolder/spline.csv'
    4.  
    5. verts = [ bpy.context.object.matrix_world @ v.co for v in bpy.context.object.data.vertices ]
    6.  
    7. csvLines = [ ";".join([ str(v) for v in co ]) + "\n" for co in verts ]
    8.  
    9. f = open( outputFile, 'w' )
    10. f.writelines( csvLines )
    11. f.close()
    You have to change the "*" sign that they originally used in line 3 to an "@" sign for newer versions of blender. You also have to make sure that the slashes are forward slashes when copying your folder address. Make sure it's exporting to a folder you actually want!

    In order to use this, make a copy of your spline with shift + d, then use F3-> Convert To -> Convert To Mesh From Bezier Curve.

    Next, go to the scripting tab in blender. Copy the code into the Text Editor in the middle, and do Text->Save As somewhere useful. Make sure your curve mesh is selected. Then, click Run Script.

    This is where it really gets convoluted. This script places all the coordinate data into one column in the CSV file, but Dreamteck needs each coordinate data point (x,y,z) to have it's own column. I don't know enough about python to fix this, but there is an easy enough workaround.

    You can open the CSV file in Microsoft Excel, then click on the "A" column to select everything, go to Data-> From Table, and just hit enter. When the new window pops up, click on Split Column -> By Delimiter -> set the Delimiter to semicolon or type ";". Then, hit close and load. This should split the data into 3 columns.

    Next, scroll down to the bottom, and make sure all the data points are visible. Drag the "..." thing down if they aren't.

    Finally, save the file. It will give you a warning, just hit yes. Close the file, and reopen the file. Right click on Row 1 (with the titles) and delete. Save the file again. Close the file.

    Finally, go back to unity. Import the CVS file into a unity folder by just dragging it in. You can change the name to something useful here.

    In unity, click on Window -> Dreamteck -> Splines -> Tool. Go to import/export, and import your spline. You have to change the import options to only use position (this is why we converted the data specifically to three columns). You may need to adjust the axis (and change the scale to -1). If you also have a corresponding (visible) mesh, you can place that in the scene as well, and make your spline a child of the mesh, then set the spline space to local in the Spline Computer component. You probably also want to set the type to B Spline, and fix the normals (if someone knows how to export normal data/ coordinate data straight from a Bezier curve into a CVS file please comment). You should now be able to move your spline around and reposition it.

    To get it working and test it, just replace the spline in the example with you new spline.

    Hope this is useful if anyone stumbles across this!

    Nick
     
    Last edited: Dec 3, 2020