Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by pheu · Oct 23, 2013 at 03:52 PM · transformmeshscaleexportobj

How to export .OBJ from Editor with rescaled mesh?

I know there are scripts that can handle exporting .obj from editor here: http://wiki.unity3d.com/index.php?title=ObjExporter

The problem is that it takes static mesh attached to this model and it doesnt include my changes in Transform Component. Is there a way to export it with resized meshes (via transform)? Did you hear about some scripts that handle that?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
5

Answer by robertbu · Oct 23, 2013 at 04:04 PM

Here is an editor script that should do the job for you. It takes an object and makes a clone that has local scale of (1,1,1) and rotation (0,0,0). Place the script in the 'Assets/Editor' folder. It must be named 'RotateMesh'. To use select the object in the hierarchy, and select 'RotateMesh' from the 'Window' menu. It is only lightly tested at this point.

 using UnityEngine;
 using UnityEditor;
 using System.Collections.Generic;
 
 public class RotateMesh : EditorWindow {
     
     private string error = "";
     
     [MenuItem("Window/Rotate Mesh %#r")]
     public static void ShowWindow() {
         EditorWindow.GetWindow(typeof(RotateMesh));
     }
 
     void OnGUI() {
         Transform curr = Selection.activeTransform;
         GUILayout.Label ("Creates a clone of the game object with a rotated mesh\n" + 
             "so that the rotation will be (0,0,0) and the scale will\nbe (1,1,1).");
         GUILayout.Space(20);
 
         if (GUILayout.Button ("Rotate Mesh")) {
             error = "";
             RotateTheMesh();
         }
         
         GUILayout.Space(20);
         GUILayout.Label(error);
     }
     
     void RotateTheMesh() {
         List<Transform> children = new List<Transform>();
         Transform curr = Selection.activeTransform;
 
         MeshFilter mf;
         if (curr == null) {
             error = "No appropriate object selected.";
             Debug.Log (error);    
             return;
         }
         
         if (curr.localScale.x < 0.0 || curr.localScale.y < 0.0f || curr.localScale.z < 0.0f) {
             error = "Cannot process game objecrt with negative scale values.";
             Debug.Log (error);
             return;
         }
         
         mf = curr.GetComponent<MeshFilter>();
         if (mf == null || mf.sharedMesh == null) {
             error = "No mesh on the selected object";
             Debug.Log (error);
             return;
         }
         
         // Create the duplicate game object
         GameObject go = Instantiate (curr.gameObject) as GameObject;
         mf = go.GetComponent<MeshFilter>();
         mf.sharedMesh = Instantiate (mf.sharedMesh) as Mesh;
         curr = go.transform;
         
         // Disconnect any child objects and same them for later
         foreach (Transform child in curr) {
             if (child != curr) {
                 children.Add (child);
                 child.parent = null;
             }
         }
         
         // Rotate and scale the mesh
         Vector3[] vertices = mf.sharedMesh.vertices;
         for (int i = 0; i < vertices.Length; i++) {
             vertices[i] = curr.TransformPoint(vertices[i]) - curr.position;
         }
         mf.sharedMesh.vertices = vertices;
 
         
         // Fix the normals
         Vector3[] normals = mf.sharedMesh.normals;
         if (normals != null) {
             for (int i = 0; i < normals.Length; i++)
                 normals[i] = curr.rotation * normals[i];
         }
         mf.sharedMesh.normals = normals;
         mf.sharedMesh.RecalculateBounds();
         
         curr.transform.rotation = Quaternion.identity;
         curr.localScale = new Vector3(1,1,1);
     
         // Restore the children
         foreach (Transform child in children) {
             child.parent = curr;
         }
         
         // Set selection to new game object
         Selection.activeObject = curr.gameObject;
 
         //--- Do a rudamentary fixup of mesh, box, and sphere colliders----
         MeshCollider mc = curr.GetComponent<MeshCollider>();
         if (mc != null) {
             mc.sharedMesh = mf.sharedMesh;
         }
         
         BoxCollider bc = curr.GetComponent<BoxCollider>();
         if (bc != null) {
             DestroyImmediate(bc);
             curr.gameObject.AddComponent<BoxCollider>();
         }
         SphereCollider sc = curr.GetComponent<SphereCollider>();
         if (sc != null) {
             DestroyImmediate(sc);
             curr.gameObject.AddComponent<SphereCollider>();
         }
         
         if (curr.GetComponent<Collider>()) {
             error = "Be sure to verify size of collider.";
         }
         
         // Save a copy to disk
         string name = "Assets/Editor/"+go.name+Random.Range (0, int.MaxValue).ToString()+".asset";
         AssetDatabase.CreateAsset(mf.sharedMesh, name);
         AssetDatabase.SaveAssets();
     }
 }

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

15 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

Related Questions

Calculating mesh over GameObject scale 1 Answer

How to scale a mesh after it's been combined with another? 2 Answers

Scale arm to hand position 1 Answer

How do I permanently resize a model with children? 2 Answers

Scale collider independent of gameObject. 1 Answer


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