edyj / blender-to-unity3d-importer Goto Github PK
View Code? Open in Web Editor NEWAdvanced Blender to Unity 3D model importer
License: MIT License
Advanced Blender to Unity 3D model importer
License: MIT License
I've just tried importing some more complex models and found it's doing some unnecessary rotating.
Here's the model in question: https://drive.google.com/file/d/0B9DFPZBPKp6AdGU5czR4RDQteDQ/view?usp=sharing
When it's imported into unity with your script and the default settings, you'll notice the bike pedals incorrectly rotate 90 degrees.
If you have time could you please look into this?
I can't work it out.
Blender's default Front axis seems to be "-Y" instead of "+Y", as is actually taken by the importer. This is deducted from the view keys "Front" and "Back" (numpad-1, ctrl-numpad-1). When the object's Front is pointing +Y then the "Front" view shows its back.
Front should be considered to be -Y by the importer, and the actual "nozfix" flag should be reconfigured to be an optional flag ("zfix"?) that turns around the Z direction in the object.
Are you updating this importer,yet?
This importer is very useul.
This importer supports only Legacy Animation Clips,
but by this code, you can get Generic Animation Clips.
It works good for me.
// from https://forum.unity.com/threads/getting-animationclip-from-fbx-in-the-editor-mecanim.295229/
public IEnumerable<AnimationClip> GetAnimationClip(GameObject go)
{
AnimationClip[] clips = AnimationUtility.GetAnimationClips(go);
if (clips.Length == 0)
{
clips = Object.FindObjectsOfType<AnimationClip>();
}
HashSet<string> importClipName = new HashSet<string>();
ModelImporter modelImporter = assetImporter as ModelImporter;
for (int i = 0; i < modelImporter.defaultClipAnimations.Length; ++i)
{
importClipName.Add(modelImporter.defaultClipAnimations[i].name);
}
for (int i = 0; i < clips.Length; ++i)
{
AnimationClip clip = clips[i];
if (clip != null &&
importClipName.Contains(clip.name) &&
!EditorUtility.IsPersistent(clip))
{
yield return clip;
}
}
}
Hi!
On your website you have a blog entry that helped me a lot to get the textures of blender models into Unity3D. So thanks for that.
I use this importer, too, so I asked myself if it would be possible somehow to add a feature to this importer that automatically extracts the textures it finds and puts it into a textures subdirectory (from the path in the Assets/xyz-folder, where the "blenderfile[importer].blend" resides.
Thanks a lot for the work you put into this so far!
Cheers,
orrence.
If I use meshes without UVs, I've got this error:
IndexOutOfRangeException: Array index is out of range.
IndexOutOfRangeException: Array index is out of range.
EdysBlenderImporter.CalculateMeshTangents (UnityEngine.Mesh mesh) (at Assets/Editor/EdysBlenderImporter.cs:630)
I don't know, how to work on github, so I just insert my fix-code here for the next method :
public static void CalculateMeshTangents (Mesh mesh)
{
//speed up math by copying the mesh arrays
int[] triangles = mesh.triangles;
Vector3[] vertices = mesh.vertices;
Vector2[] uv = mesh.uv;
Vector3[] normals = mesh.normals;
//variable definitions
int triangleCount = triangles.Length;
int vertexCount = vertices.Length;
Vector3[] tan1 = new Vector3[vertexCount];
Vector3[] tan2 = new Vector3[vertexCount];
Vector4[] tangents = new Vector4[vertexCount];
Vector2 w1 = Vector2.zero;
Vector2 w2 = Vector2.zero;
Vector2 w3 = Vector2.zero;
for (long a = 0; a < triangleCount; a += 3)
{
long i1 = triangles[a + 0];
long i2 = triangles[a + 1];
long i3 = triangles[a + 2];
// Skip IndexOutOfRangeException: Array index is out of range.
if (uv.Length > 0)
{
// Then we have UVs for this object
w1 = uv[i1];
w2 = uv[i2];
w3 = uv[i3];
}
else
{
// all UVs stays 0
}
Vector3 v1 = vertices[i1];
Vector3 v2 = vertices[i2];
Vector3 v3 = vertices[i3];
float x1 = v2.x - v1.x;
float x2 = v3.x - v1.x;
float y1 = v2.y - v1.y;
float y2 = v3.y - v1.y;
float z1 = v2.z - v1.z;
float z2 = v3.z - v1.z;
float s1 = w2.x - w1.x;
float s2 = w3.x - w1.x;
float t1 = w2.y - w1.y;
float t2 = w3.y - w1.y;
float r = 1.0f / (s1 * t2 - s2 * t1);
Vector3 sdir = new Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
Vector3 tdir = new Vector3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);
tan1[i1] += sdir;
tan1[i2] += sdir;
tan1[i3] += sdir;
tan2[i1] += tdir;
tan2[i2] += tdir;
tan2[i3] += tdir;
}
for (long a = 0; a < vertexCount; ++a)
{
Vector3 n = normals[a];
Vector3 t = tan1[a];
Vector3.OrthoNormalize(ref n, ref t);
tangents[a].x = t.x;
tangents[a].y = t.y;
tangents[a].z = t.z;
tangents[a].w = (Vector3.Dot(Vector3.Cross(n, t), tan2[a]) < 0.0f) ? -1.0f : 1.0f;
}
mesh.tangents = tangents;
}
When trying to use this importer in my current version of unity I get an error. Is the importer broken for current versions of Unity?
---(Error)---
IndexOutOfRangeException: Array index is out of range.
EdysBlenderImporter.CalculateMeshTangents (UnityEngine.Mesh mesh) (at Assets/Editor/EdysBlenderImporter.cs:625)
EdysBlenderImporter.RotateMesh (UnityEngine.GameObject go, Quaternion rot) (at Assets/Editor/EdysBlenderImporter.cs:381)
EdysBlenderImporter.ProcessFirstLevel (UnityEngine.GameObject go) (at Assets/Editor/EdysBlenderImporter.cs:195)
EdysBlenderImporter.ProcessBlenderObject (UnityEngine.GameObject go) (at Assets/Editor/EdysBlenderImporter.cs:176)
EdysBlenderImporter.OnPostprocessModel (UnityEngine.GameObject go) (at Assets/Editor/EdysBlenderImporter.cs:95)
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115)
UnityEditor.AttributeHelper.InvokeMemberIfAvailable (System.Object target, System.String methodName, System.Object[] args) (at /Users/builduser/buildslave/unity/build/Editor/Mono/AttributeHelper.cs:168)
UnityEditor.AssetPostprocessingInternal.PostprocessMesh (UnityEngine.GameObject gameObject) (at /Users/builduser/buildslave/unity/build/Editor/Mono/AssetPostprocessor.cs:246)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.