Search Unity

Blender 2.57

Discussion in 'Asset Importing & Exporting' started by KnifeFightBob, Apr 14, 2011.

  1. w00dn

    w00dn

    Joined:
    Apr 28, 2010
    Posts:
    275
    @chs200123: Glad it worked out for you. About colliders and textures. Well, to be honest, I couldn't get a good workflow setup. We tested around with asset postprocessor scripts to generate a range of colliders and assign some scripts during import, but it got very unusable after a while. So now I just create reusable assets and place them in unity.

    And don't use "Generate Colliders". All it does is assigning the whole mesh as a mesh collider, which is madness in most cases.

    Still, this script might be interesting for you:

    Code (csharp):
    1.  
    2. // This script is used to automatically add components to an imported .blend file while
    3. // maintaining the prefab connection.
    4.  
    5. // Place this script in a folder called "Editor" in your Assets folder and place
    6. // your .blend files in the folder "Environment". You can change the assetPath on line 44.
    7.  
    8. // Name your objects in blender according to the following list. For example:
    9. // - A crate with a box collider and a rigidbody you'd name "crate_box_r_".
    10. // - A simple wall with a box collider you'd name "xyz_wall_box"
    11. // - A piece of floor with a box collider and the floor tag you'd name "xyz_floor_box"
    12. // - An invisible convex mesh collider you'd name "xyz_coll_meshc"
    13.  
    14. // mesh -> adds a meshcollider
    15. // meshc -> adds a meshcollider and makes it convex
    16. // box -> adds a boxcollider
    17.  
    18. // coll_ -> removes meshrenderer (for invisible colliders or simplified colliders)
    19. // _r_ -> adds rigidbody
    20. // _rk_ -> adds rigidbody set to kinematic
    21. //_ig_ -> ignores object or meshdata (useful for objecty you only need in blender)
    22.  
    23. // Make sure the tags you define here are present in your unity project.
    24. // floor_ -> tags object with "Floor"
    25. // wall_ > tags object with "Wall"
    26. // obst_ -> tags objects with "Obstacle"
    27.  
    28. // This can easily be extended to any other component you can add in unity,
    29. // like layers or your own scripts. The only problem so far is blenders
    30. // character limit on object names :-(
    31.  
    32.  
    33. class MyModelPostprocessor extends AssetPostprocessor {
    34.     var toDelete = new Array();
    35.     function OnPostprocessModel (g : GameObject) {
    36.         //Debug.Log("Start Postprocessing");
    37.         Apply(g.transform);
    38.         for(go in toDelete){
    39.             assetImporter.DestroyImmediate(go);
    40.         }
    41.     }
    42.    
    43.     function Apply (transform : Transform){
    44.         // only assets in the folder "Environment" get postprocessed by this script
    45.         if (assetPath.ToLower().Contains("environment")) {
    46.            
    47.             if(transform.name.ToLower().Contains("mesh")){
    48.                 transform.gameObject.AddComponent(MeshCollider);
    49.             }
    50.             if(transform.name.ToLower().Contains("meshc")){
    51.                 transform.gameObject.collider.convex = true;
    52.             }
    53.             if(transform.name.ToLower().Contains("box")){
    54.                 transform.gameObject.AddComponent(BoxCollider);
    55.             }
    56.            
    57.             if(transform.name.ToLower().Contains("_r_")){
    58.                 transform.gameObject.AddComponent(Rigidbody);
    59.             }
    60.            
    61.             if(transform.name.ToLower().Contains("_rk_")){
    62.                 transform.gameObject.AddComponent(Rigidbody).isKinematic = true;
    63.             }
    64.            
    65.             if(transform.name.ToLower().Contains("coll_")){
    66.                 var mf = transform.gameObject.GetComponents(MeshFilter);
    67.                 var mr = transform.gameObject.GetComponents(MeshRenderer);
    68.                 for(f in mf){
    69.                     assetImporter.DestroyImmediate(f);
    70.                 }
    71.                 for(r in mr){
    72.                     assetImporter.DestroyImmediate(r);
    73.                 }
    74.             }
    75.            
    76.             if(transform.name.ToLower().Contains("floor_")){
    77.                 transform.gameObject.tag = "Floor";
    78.             }
    79.            
    80.             if(transform.name.ToLower().Contains("obst_")){
    81.                 transform.gameObject.tag = "Obstacle";
    82.             }
    83.            
    84.             if(transform.name.ToLower().Contains("wall_")){
    85.                 transform.gameObject.tag = "Wall";
    86.             }
    87.            
    88.             if(transform.name.ToLower().Contains("_ig_")){
    89.                 toDelete.Add(transform.gameObject);
    90.                
    91.                 // also delete the sharedMesh with _ig_ in the name
    92.                 var igmf = transform.gameObject.GetComponents(MeshFilter);
    93.                 for(var igf in igmf){
    94.                     assetImporter.DestroyImmediate(igf.sharedMesh, true);
    95.                 }              
    96.             }
    97.         }
    98.    
    99.         // Recurse
    100.         for (var child in transform){
    101.             Apply(child);
    102.         }
    103.     }
    104. }
    105.  
     
  2. ron333

    ron333

    Joined:
    Jan 3, 2009
    Posts:
    47
    Hi,

    I downloaded Unity-BlenderToFBX.zip that you have above and installed it in Unity 3.30f4 (63135).
    In Blender 2.57 (April 13), I just saved the default file as cube.blend

    When I put cube.blend in the Assets folder and run Unity, I get:

    Blender could not convert the .blend file to FBX file.
    You need to use Blender 2.45 or higher for direct Blender import to work.

    Any help would be appreciated. Thank you.
     
  3. chs200123

    chs200123

    Joined:
    Oct 14, 2010
    Posts:
    7
    I think you need to look at Woodn's post above
     
  4. ron333

    ron333

    Joined:
    Jan 3, 2009
    Posts:
    47
    I did and tired to follow his directions exactly. It simply did not work.

    I ended up exporting a COLLADA (.dae) from Blender 2.57a. Then I used Autodesk's FBX Converter (2011) to convert the .dae into a .fbx

    This worked great.
     
  5. alewinn

    alewinn

    Joined:
    Nov 8, 2009
    Posts:
    185
    ron333 :
    Just for my information : what System do you use ? (Windows XP, Windows 7, Ios....) ?
     
  6. ron333

    ron333

    Joined:
    Jan 3, 2009
    Posts:
    47
    I'm using Blender 2.57a and Unity 3.3. I have finally realized that not doing some things in Blender were the problem (with respect to exporting a fbx file from Blender and importing it into Unity). Here is a little summary of how to work in Blender.

    After you have used Blender's UV/ImageEditor and got your image aligned with your unwrapped object, you need to create a material and texture for it in Blender.
    Click on the material icon in the Properties Window and then new material to get a slot. Now click the plus sign to create a new material. You may rename the material. Scroll down and click on the Face Textures checkbox (As Seraph noted above).
    Click the Assign Button. Click on the texture icon (its next to the material icon). Click new texture.
    Set the texture type to image/video. Scroll down and import your image file. Scroll down to Mapping and set the Coordinates to UV.

    Make sure you are in Object Mode and select the objects you want to export.
    From the File menu, chose Export and Autodesk FBX.
    On the left hand side, scroll down and chose the options you want.
    Export your .fbx file.

    In your Unity project but the .fbx file in the Assets folder.
    Create a folder named textures in the Assets folder and put in your .png (or whatever) files.

    Unity will automatically assign your textures to the materials.
     
  7. ron333

    ron333

    Joined:
    Jan 3, 2009
    Posts:
    47
    to: Alewinn
    I am using Mac OS 10.6.7 Thanks for any help.
     
  8. Bryan

    Bryan

    Joined:
    Feb 27, 2011
    Posts:
    36
    I've been using 2.49b even up until now. I shall try it with 2.57 next. However, I'm confused about something. It seems the only issue being raised about being able to use .blends directly is the convenience of exporting an FBX. My reasoning for using 2.49b over 2.5 and using FBX, was that I have lots of animations, and by importing a direct .blend, all my actions are divided up in Unity. Is there a way to make FBX Divide up my actions? or were most people not using animations?
     
  9. DrPygameNewb

    DrPygameNewb

    Joined:
    May 6, 2011
    Posts:
    136
    Had issues with 2.49 in the past on my computer, so 2.57 is what I stick with.
     
  10. luistorrao

    luistorrao

    Joined:
    Jun 19, 2011
    Posts:
    4
    Hi there, I am using this excelent .py and I get all into Unity. Except I have a small problem with some of my actions: despite working perfectly in Blender, when they are imported to Unity, I get strange deformations in some vertices. Does it have to do with the py, or is there something I can do to prevent it from happening?
     
  11. luistorrao

    luistorrao

    Joined:
    Jun 19, 2011
    Posts:
    4
    Nervermind my last question. I managed to solve it with some extra weight painting in Blender. But I still dont understand why it happened: i had my actions combined in one animation and rendered a video of it that came out just fine. Then when importing to Unity I had strange artifacts so I had to go back to blender and weight paint a bit to hide them...
     
  12. KnifeFightBob

    KnifeFightBob

    Joined:
    Jan 22, 2009
    Posts:
    196
    I may just be stupid now, but (I think) with 2.57b2 the custom FBX script got broken. Not sure. However, it does clearly not work with 2.58. Not that I was expecting anyone to have anything done by this point either, though. Any thoughts? I'd love to contribute more to this on-going development as Unity Tech most certainly will not keep up with the updates, but unfortunately I don't know the first thing about how the importer works.
     
  13. deekr

    deekr

    Joined:
    Jun 22, 2011
    Posts:
    4
    There wasn't that much to change. I replaced the last call to 'io_scene_fbx.export_fbx.save' with the following:

    Code (csharp):
    1. io_scene_fbx.export_fbx.save(
    2.                 FakeOp(),
    3.                 bpy.context,
    4.                 filepath=outfile,
    5.                 use_selection=False,
    6.                 global_matrix=mtx4_x90n,
    7.                 object_types={'ARMATURE', 'EMPTY', 'MESH'},
    8.                 ANIM_OPTIMIZE=False,
    9.                 ANIM_ACTION_ALL=True)
    For some more instructions:
    • Let's call the Unity installation folder $UNITY. (For me, $UNITY = C:\Program Files (x86)\Unity)
    • The script you want to update is $UNITY\Editor\Data\Tools\Unity-BlenderToFBX.py. (.py for Python)
    • Before editing the script, make a copy of it and put the copy in the same directory. (For safe keeping)
    • Replace the last call to the function io_scene_fbx.export_fbx.save with that above.

    Note, just in case you don't know where the function call ends, the line

    Code (csharp):
    1. # I don't think HQ normals are supported in the current exporter
    should occur just after the closing parenthesis of the function call in the original script.

    While I'm at it, for those interested, the module being referenced here is $BLENDER\2.58\scripts\addons\io_scene_fbx\export_fbx.py. (For me, $BLENDER = C:\Program Files\Blender Foundation\Blender)
     
    Last edited: Jun 22, 2011
  14. KnifeFightBob

    KnifeFightBob

    Joined:
    Jan 22, 2009
    Posts:
    196
    Thank you. It works! Also, thanks for reposting the instructions, but what I meant was not how to apply the script changes, but rather how one should go about fiddling with the actual script. When a new version of Blender comes out, for example, what changes should be made to the import script? What am I looking for? What exactly gets broken in the process between Unity and Blender?

    Anyway, thank you for posting the most recent changes!
     
  15. deekr

    deekr

    Joined:
    Jun 22, 2011
    Posts:
    4
    No problem KnifeFightBob. I was just posting the instructions with the code.

    Basically what is going on is that Blender 2.5x is still being developed. So, things like Blender's Python API are subject to change. In this case, a function signature in the API has changed, which broke the Unity Python script for Blender that was written for (I'm kinda guessing here) 2.49 and 2.56.

    When something like this happens, you need to look at the Unity script to see how it is trying to interact with Blender's Python API (what modules, functions, classes, etc.. the script is trying to use). Then, you would need to look through Blender's Python API and determine what stayed the same, what changed, and, if there is something broken in the script, how to interact with the new API to accomplish the original task.

    For this script, it looks like a lot of the changes are just around this one function 'io_scene_fbx.export_fbx.save'. From the previous post, the location of the code for this guy in Blender is in $BLENDER\2.58\scripts\addons\io_scene_fbx\export_f bx.py (note the version of Blender in the path). So, one would need to look at that python file and check the changes, correcting the Unity script as they go.
     
    Last edited: Jun 24, 2011
  16. jorsalsa

    jorsalsa

    Joined:
    Aug 25, 2010
    Posts:
    8
    Deekr,
    Thanks for the fix. I just installed 2.8 and changed the .py file in Unity as recommended by you, and it is working.
    Jorsalsa
     
  17. temp_account

    temp_account

    Joined:
    Apr 24, 2011
    Posts:
    6
    Thanks for the updated script - but unfortunately for the life of me, I can't manage to get 2.58 to work with Unity. I thought this would be a good time to upgrade my blender version from 2.57a, but looks like it wasn't..

    I have downloaded the script from Page 1 and updated it accordingly to Deekr's instructions. My Blender is installed to C:\Program Files (x86)\Blender. During the installation I just made sure the "open .blend files with Blender" was checked.

    What else can go wrong in the file import? Does Unity even know where to look for the Blender installation? Or if someone could upload a working .py file, in case I messed something up in there.

    Any additional help would be appreciated.

    Edit: My bad, I had accidentally inserted a newline after the function name and the parentheses, like this..:

    io_scene_fbx.export_fbx.save
    (

    instead of

    io_scene_fbx.export_fbx.save(

    which works perfectly. I guess coming from c# world I take these things for granted.. Again, thanks for the updated script, it's great the community keeps supporting Blender.
     
    Last edited: Jun 27, 2011
  18. deekr

    deekr

    Joined:
    Jun 22, 2011
    Posts:
    4
    I agree temp_account. I think Blender is a great tool and it's getting better and better all the time. Wow, that sounded incredibly generic, but there are times in life when such things are true, and I think that is the case here. I think the Unity team is pretty stand-up for supporting it and I hope the community benefits from this and does what it can to keep it rolling.
     
  19. g2mediagroup

    g2mediagroup

    Joined:
    Jun 25, 2011
    Posts:
    98
    :D

    I actually stumbled on this thread by searching for something else (isn't that the way it works anymore?), but HOLY SMOKES! You guys are God-sends! I do have a favor, where do we change this on our Linux installs? THIS WILL SAVE OODLES OF TIME! Thank you, thank you, thank you!

    I know it's early, but does anyone have any comments on pitfalls of installing blender 2.58?

    Thanks ALL again!
     
  20. g2mediagroup

    g2mediagroup

    Joined:
    Jun 25, 2011
    Posts:
    98
    Actually, when I posted, I was on a previous page without paying attention to more thread pages...

    @deekr

    I went to edit the original file with your changes for 2.58 to work and can't find your reference line:


    Code (csharp):
    1. # I don't think HQ normals are supported in the current exporter
    This is the file I'm editing:


    Code (csharp):
    1. blender249 = True
    2.  
    3. try: import Blender
    4. except:
    5.     blender249 = False
    6.     import bpy 
    7.  
    8. if blender249:
    9.     try: import export_fbx
    10.     except:
    11.         print('error: export_fbx not found.')
    12.         Blender.Quit()
    13. else:
    14.     try: import io_scene_fbx.export_fbx
    15.     except:
    16.         print('error: io_scene_fbx.export_fbx not found.')
    17.         # This might need to be bpy.Quit()
    18.         raise
    19.  
    20. # Find the Blender output file
    21. import os
    22. outfile = os.getenv("UNITY_BLENDER_EXPORTER_OUTPUT_FILE")
    23.  
    24. # Do the conversion
    25. print("Starting blender to FBX conversion " + outfile)
    26.  
    27. if blender249:
    28.     export_fbx.write(outfile,
    29.     EXP_OBS_SELECTED=False,
    30.     EXP_MESH=True,
    31.     EXP_MESH_APPLY_MOD=True,
    32.     EXP_MESH_HQ_NORMALS=True,
    33.     EXP_ARMATURE=True,
    34.     EXP_LAMP=True,
    35.     EXP_CAMERA=True,
    36.     EXP_EMPTY=True,
    37.     EXP_IMAGE_COPY=False,
    38.     ANIM_ENABLE=True,
    39.     ANIM_OPTIMIZE=False,
    40.     ANIM_ACTION_ALL=True,
    41.     GLOBAL_MATRIX = Blender.Mathutils.RotationMatrix( -90, 4, 'x' ),\
    42.     )
    43. else:
    44.     import math
    45.     from mathutils import Matrix
    46.     # -90 degrees
    47.     mtx4_x90n = Matrix.Rotation(-math.pi / 2.0, 4, 'X')
    48.    
    49.     class FakeOp:
    50.         def report(self, tp, msg):
    51.             print("%s: %s" % (tp, msg))
    52.    
    53. [COLOR="red"]   io_scene_fbx.export_fbx.save(
    54.     FakeOp(),
    55.     bpy.context,
    56.     filepath=outfile,
    57.     use_selection=False,
    58.     batch_mode='OFF',
    59.     BATCH_OWN_DIR=False,
    60.     GLOBAL_MATRIX=mtx4_x90n,
    61.     context_objects=None,
    62.     object_types={'EMPTY', 'ARMATURE', 'MESH'},
    63.     mesh_apply_modifiers=True,
    64.     mesh_smooth_type='FACE',
    65.     ANIM_ENABLE=True,
    66.     ANIM_OPTIMIZE=False,
    67.     ANIM_OPTIMIZE_PRECISSION=6,
    68.     ANIM_ACTION_ALL=True,
    69.     use_metadata=True,
    70.     path_mode='AUTO'   
    71.     )[/COLOR]
    72.  
    73. print("Finished blender to FBX conversion " + outfile)
    74.  

    Is the code snippet in RED what gets replaced? Just making sure I'm following along and up to current version...

    Thanks!
     
  21. g2mediagroup

    g2mediagroup

    Joined:
    Jun 25, 2011
    Posts:
    98
    @temp_account

    Would you mind sharing what and where you actually made the tweak spoken of here? I'm attempting to find any/all ways to optimize the Blender/Unity workflow before we get too deep into a project, then find the "ahhhhh, that would have saved oooodles of time..." tricks----is this in a .py script in Blender? Unity? Where....THANKS!
     
  22. d.toliaferro

    d.toliaferro

    Joined:
    Jan 24, 2011
    Posts:
    95
    deekr, you are the man! Amazing work!
     
  23. w00dn

    w00dn

    Joined:
    Apr 28, 2010
    Posts:
    275
    @blenderers: I'm currently testing a new .blend direct import workflow for simple models where I don't want any "blender-rotations" on my meshes and model objects in Unity. It sounds a little complicated to setup in blender, but once you have done it, your models will have no rotations on them in unity, so you don't have to have a parent object with a rotated sub object for every mesh you want to use somewhere.

    Check out the attached blend file. I set up a "copy rotation constraint" on the model, which copies the rotations of the empty object on layer 2. The resulting funky rotations of the whole object then need to be "nullified" by rotating the mesh (not the whole object) back so it's original rotations, so you can work on it in blender withouth thinking in a different coordinate system (r g s - x y z stortcuts ftw!). If you set up this rotation thing on the default object in your blender startup scene, you won't even know it's there when you start to model.

    Important: Make sure your object(!) doesn't have any rotations on it (ctrl-a > apply rotation)!
    Then save the file and put it in your Assets/Models folder.

    If you don't like Empty_ig_ object showing up in unity, put the attached ModelPostprocessor.js script in the Assets/Editor folder to get rid of it. The script processes every model in the folder "Assets/Models" or "Assets/models" and ignores every object and mesh with _ig_ in its blender name.

    Let me know what you think... Also, if someone has a better / simpler / more automated solution, I'm all ears :p
     

    Attached Files:

  24. NSdesignGames

    NSdesignGames

    Joined:
    Dec 29, 2010
    Posts:
    496
    g2mediagroup Here is the working code for Blender 2.58!

    Code (csharp):
    1. blender249 = True
    2.  
    3. try: import Blender
    4. except:
    5.     blender249 = False
    6.     import bpy 
    7.  
    8. if blender249:
    9.     try: import export_fbx
    10.     except:
    11.         print('error: export_fbx not found.')
    12.         Blender.Quit()
    13. else:
    14.     try: import io_scene_fbx.export_fbx
    15.     except:
    16.         print('error: io_scene_fbx.export_fbx not found.')
    17.         # This might need to be bpy.Quit()
    18.         raise
    19.  
    20. # Find the Blender output file
    21. import os
    22. outfile = os.getenv("UNITY_BLENDER_EXPORTER_OUTPUT_FILE")
    23.  
    24. # Do the conversion
    25. print("Starting blender to FBX conversion " + outfile)
    26.  
    27. if blender249:
    28.     export_fbx.write(outfile,
    29.     EXP_OBS_SELECTED=False,
    30.     EXP_MESH=True,
    31.     EXP_MESH_APPLY_MOD=True,
    32.     EXP_MESH_HQ_NORMALS=True,
    33.     EXP_ARMATURE=True,
    34.     EXP_LAMP=True,
    35.     EXP_CAMERA=True,
    36.     EXP_EMPTY=True,
    37.     EXP_IMAGE_COPY=False,
    38.     ANIM_ENABLE=True,
    39.     ANIM_OPTIMIZE=False,
    40.     ANIM_ACTION_ALL=True,
    41.     GLOBAL_MATRIX = Blender.Mathutils.RotationMatrix( -90, 4, 'x' ),\
    42.     )
    43. else:
    44.     import math
    45.     from mathutils import Matrix
    46.     # -90 degrees
    47.     mtx4_x90n = Matrix.Rotation(-math.pi / 2.0, 4, 'X')
    48.    
    49.     class FakeOp:
    50.         def report(self, tp, msg):
    51.             print("%s: %s" % (tp, msg))
    52.    
    53.     io_scene_fbx.export_fbx.save(
    54.                 FakeOp(),
    55.                 bpy.context,
    56.                 filepath=outfile,
    57.                 use_selection=False,
    58.                 global_matrix=mtx4_x90n,
    59.                 object_types={'ARMATURE', 'EMPTY', 'MESH'},
    60.                 ANIM_OPTIMIZE=False,
    61.                 ANIM_ACTION_ALL=True
    62.                 )
    63.  
    64. print("Finished blender to FBX conversion " + outfile)
     
  25. g2mediagroup

    g2mediagroup

    Joined:
    Jun 25, 2011
    Posts:
    98
    Thanks, NSdesignGames!
     
  26. deekr

    deekr

    Joined:
    Jun 22, 2011
    Posts:
    4
    @NSdesignGames Thanks.

    @g2mediagroup Sorry for not responding, didn't check the forums till now. It looks like everything is sorted out. Good Luck!
     
  27. theropodx

    theropodx

    Joined:
    May 1, 2011
    Posts:
    40
    YAY! Works for me too! This should be sticky!!! Good grief I hate losing time tracking down fixes like this...

    Thank you!!!