Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by Benproductions1 · Nov 02, 2013 at 01:04 AM · javascriptbugimportblenderassetpostprocessor

AssetPostprocessor - Can't change Mesh Data

Hello again,

For the past couple hours I've been trying to solve the old: "Blender Axis is different to Unity's" problem by writing an AssetPostprocessor.

In the Unity Documentation for AssetPostprocessor.OnPostprocessModel it states: "This lets you modify the imported Game Object, Meshes, AnimationClips referenced by it", however after multiple attempts and a ton of google searches, I have not been able to change any mesh data and have it saved to disk:

 import System.IO;
 
 //Fix the ignorance of the standard Blender Importer!!!
 class BlenderAssetProcessor extends AssetPostprocessor {
     public function OnPostprocessModel(object:GameObject) {
         var importer : ModelImporter = assetImporter as ModelImporter;
         
         //Only rotate with blender files
         if (Path.GetExtension(importer.assetPath) == ".blend") {
             RotateObject(object);
         }
     }
     
     private function RotateObject(object:GameObject) {
         object.transform.rotation *= Quaternion.Euler(90, 0, 0); //This works and is saved
         
         var meshFilter:MeshFilter = object.GetComponent(MeshFilter);
         if (meshFilter) {
              //Use proven methods to manipulate the mesh data
              ChangeMesh(meshFilter.sharedMesh); //This changes the mesh, but nothing is saved???
         }

         //--some code that calls this recursively for children I left out
     }
  }

I get no errors and everything works absolutely fine, except for the fact that no mesh data is saved.

Has anyone been able to achieve something similar?
Is this even possible, because it definitely should be?!?

Thanks for your help,
Benproductions1

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tanoshimi · Nov 21, 2013 at 08:58 AM 0
Share

I'm afraid I don't know, but +1 for a well-written, relevant question. Let's hope for the sake of the community here that someone can write an equally eloquent answer :)

avatar image benhumphreys · Dec 11, 2013 at 07:31 AM 0
Share

This looks like it could be a life-saver. Could you post your Change$$anonymous$$esh() function?

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Benproductions1 · Nov 21, 2013 at 09:27 AM

Hello again again,

It turns out that mesh data is not passed by reference but via copy.
For instance:

 Vector3[] verts = mesh.vertices;
 verts[0] = Vector3.zero;

will not set the first vertex to Vector3.zero
What you need to do is set the vertex array of the mesh

 Vector3[] verts = mesh.vertices;
 verts[0] = Vector3.zero;
 mesh.vertices = verts;

I find this to be counter-intuitive, but what can you do?

Hope this helps anyone else,
Benproductions1

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image demented_hedgehog · May 14, 2015 at 03:38 AM 0
Share

This may reflect the underlying opengl implementation where the vertices are written to the graphics card memory. This way you are forced to batch your vertex writes (just a theory).

avatar image demented_hedgehog · May 14, 2015 at 03:52 AM 0
Share

I've read a bit more and this does seem to be the case. http://answers.unity3d.com/questions/334473/can-i-move-just-some-of-the-verts-on-a-mesh.html

http://answers.unity3d.com/questions/352162/optimized-vertex-manipulation.html

It's bad api design if you ask me.. having the .vertices return a copy when the syntax implies you're getting a reference is not good design.

avatar image
0

Answer by AubreyH · Aug 04, 2020 at 07:37 PM

Hope this helps. Tested in v 2020.2.

Make a script file anywhere sub an "Editor" directory in your Project folder. Copy the below. Save it. Reimport your blender folder full of .blend files.

Note, this is for just the meshes. I have not had a stab at skinning data or animation. Only use this for non animated meshes.

 using UnityEngine;
 using UnityEditor;
  
 public class BlenderModelImporter : AssetPostprocessor 
 {
  
     void OnPostprocessModel (GameObject g) 
     {
         // Only operate on blender files
         if (assetPath.IndexOf(".blend") == -1) 
         {
             return;
         }
 
         MeshFilter[] meshf = g.GetComponentsInChildren<MeshFilter>();
 
         foreach (MeshFilter meshFilter in meshf)
         {
             FixBlenderRotation(meshFilter);
         }
         
         AssetDatabase.Refresh();
     }
 
     private void FixBlenderRotation(MeshFilter meshFilter)
     {
         Mesh mesh = meshFilter.sharedMesh;
 
         Matrix4x4 matRot = Matrix4x4.Rotate( Quaternion.Euler(-90,0,0));
         
         Vector3[] rotatedVers = new Vector3[mesh.vertexCount];
         Vector3[] rotatedNormies = new Vector3[mesh.vertexCount];
         for (int i = 0; i < mesh.vertexCount; i++)
         {
             rotatedVers[i] = matRot * mesh.vertices[i];
             rotatedNormies[i] = matRot * mesh.normals[i];
         }
 
         mesh.vertices = rotatedVers;
         mesh.normals = rotatedNormies;
         mesh.RecalculateBounds();//This makes sure that bounds culling has updated data so that the camera doesn't stop drawing based on the pre-rotation bounds
     
         //Also bring back the transform rotation to identity so that there's no weird counter orientation in the game object itself.
         meshFilter.transform.localRotation = Quaternion.identity;
 
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

This sounds like a bug. 3 Answers

Animation not playing with animation.Play 1 Answer

Make sure levels assets are loaded before switching? 1 Answer

I import a model from blender to unity,but it is always showed in mirror 4 Answers

how do i import an animation from blender 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges