XPlane2Blender icon indicating copy to clipboard operation
XPlane2Blender copied to clipboard

280: Ability to toggle between world and local coordinates to make building OBJs and AGP-like scenes easier

Open muchimi opened this issue 4 years ago • 6 comments

Hi Ted,

I have a usage question as I'm getting unexpected coordinates in my OBJ export.

I was expecting xplane2blender to output in the OBJ Blender world coordinates. It appears that it exports local Blender mesh coordinates for the object exported. I reviewed the source code in XPlaneMesh() (in xplane_mesh.py) and as far as I can tell it uses the vertices member on the object data block, and that is local mesh vertex data. I'm not seeing anywhere in the code where mesh data is converted from local to world. Perhaps this is a Blender 2.8x thing.

To get world coordinates, that blender vertex data needs to be multiplied by the object's world matrix, as in:

obj = bpy.ops.context.active_object verts_local = [v.co for v in obj.data.vertices.values()] verts_world = [obj.matrix_world @ v_local for v_local in verts_local]

I verified this through some scripting by dumping my object's coordinates using the code above and comparing it to the .OBJ output. The OBJ matches the local coordinate system.

Why is this important to me? Well, at least when using WED (I'm doing scenery work primarily), the positioning of the object is usually based around the world origin (0,0,0) as the center of all things.

When assembling a group file (AGP) of multiple objects, the positioning of each object becomes important and must be referenced from the same coordinate. The easiest is to express relative position of each OBJ in the AGP using a common reference system, and that is not the local coordinate system of each individual object, and why I use the world coordinates. It also helps when building complex scenery objects in the same Blender scene like (reusable) lego blocks.

So my current workaround is to ensure my object's origin is set to (0,0,0) in Blender prior to the export so the local coordinate system is the same as the world coordinate.

Is there a setting I can set in the exporter that tells xplane2blender to do this transform prior to the export?

This comes into play when there are complex relationships and transformations happening.

Thanks for any input and ideas.

M

muchimi avatar May 02 '20 14:05 muchimi

Firstly, hi, thank you for writing this.

Co-ordinate Systems of the OBJ

Actually, the OBJ content is in X-Plane co-ordinates.

From xplane_mesh.py

                        vertex = xplane_helpers.vec_b_to_x(mesh.vertices[index].co)
                        normal = xplane_helpers.vec_b_to_x(tmp_face.split_normals[i] if tmp_face.original_face.use_smooth else tmp_face.normal)

From xplane_helpers.py

def vec_b_to_x(v)->mathutils.Vector:
    return mathutils.Vector((v[0], v[2], -v[1]))

def vec_x_to_b(v)->mathutils.Vector:
    return mathutils.Vector((v[0], -v[2], v[1]))

X-Plane uses this co-ordinate system because it was easier math for drawing cockpits and how air flows over the wings (I think).

AGP file techniques

Let me ask some people how they accomplish this. We have new tools like Collections so maybe people have found new ways to do this. I'll get back to you.

tngreene avatar May 04 '20 14:05 tngreene

How are you making .agp files? The old 2.49 scripts?

tngreene avatar May 04 '20 16:05 tngreene

Hi,

To elaborate:

This is scenery only - not looking at cockpits or aircraft geometry at all.

When I refer to coordinate systems, I refer to the blender coordinate system which is either local or world.

I'm building the AGP manually, although I am working on a scripted add-on for Blender that does this for me along other things to help common tasks in scenery design. This is a WIP and I'm not ready to share anything at this point.

I use AGP and WED to construct scenery a bit like lego blocks. Using this concept, it's important that exported objects share a common point of reference, and that reference is the world origin in the blender file. It becomes easy to assemble objects that share the same origin in the AGP file as they are all going to be at the center of the tile. It also becomes easy to place related objects in WED because they will all be at the same lon/lat.

Standalone objects manually placed in a scene can use the local coordinate system - wherever the pivot is the center - that works great because the composition of the scene doesn't require highly precise values - example - ground props like a bench, a trash bin. When it comes to compound objects made up of multiple separate .OBJ files - a complex structure with multiple textures, I like my objects to be referenced from the world center - using world coordinates, not local. This works better for the lego approach.

I'm still getting familiarized with the x-plane exporter as I previously used an exporter for 3DS Max that worked a bit differently and that definitely used world coordinates before exporting.

Perhaps I'm missing something but can you confirm that the x-plane exporter converts mesh vertices to world coordinates before exporting? It looks to me it's only exporting in local coordinates.

If it does - something strange is afoot.

This said, I wouldn't dwell of this much as I have a way to accomplish what I'm looking to do - either export objects using the local coordinate system, or the world coordinate system.

I do this by manipulating the object's matrix and pivot (origin) before the export.

I suppose my ask is "add a checkbox in the exporter that lets me select local or world coordinates" for the export.

muchimi avatar May 04 '20 18:05 muchimi

Yes, the exporter always uses local co-ordinates. Its not strange when you think about the OBJ file spec, it's geometry is not aware of the world - WED is going to place it. If you want all your geometry shifted over by some amount, you'll have to find a way to shift it over by some amount.

I'm still trying to think of an easy way to do this right now without a new feature.

For your checkbox idea, I've thought about this too - how to make several OBJs in the same Blender file and be able to arrange them next to each other. Sadly I'm still working heavily on lights so this workflow idea is not going to be a prioty any time soon.

It sounds like you're a programmer, so, if you'd like to try something the key files to look at are

xplane_mesh.py, xplane_object.py, and xplane_primitive.py. These are mainly where Blender data gets turned into VT and ANIM_ commands.

I'm very checkbox averse because I have to assume there will never be enough documentation, enough time for documentation, most people wouldn't read it, and finally if people don't know about a feature they can't ask for help using. "[ ] Use World Coordinates" doesn't sound very obvious to me. Things need to "just work" intuitively or it won't get used.

tngreene avatar May 04 '20 19:05 tngreene

I'm forgetting if linking is supported yet, but if it was maybe you could make OBJs in one .blend file all at the origin point, then in another .blend file bring them all together positioned as you like. 2.79 supported this I believe.

tngreene avatar May 04 '20 19:05 tngreene

Thank you!

muchimi avatar May 04 '20 19:05 muchimi