Expose tangent space for export script in Python

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

drakfluga
Posts: 4
Joined: Sat Nov 03, 2012 9:31 pm

Expose tangent space for export script in Python

Postby drakfluga » Sun Nov 04, 2012 7:18 pm

Hi,

This is my first post. I'm sure this has been asked before, I've searched on the forums but the threads I've found have been either short and non-specific or in other ways useless and/or long dead.

I'm with a small indie game development company. I'm currently working on an export script, getting our 3D assets from Python into our proprietary game engine. We're rolling our own binary format.

My question: Is it possible, somehow, to export tangent space coordinates from a Blender mesh via Python? I know the values are there because Blender correctly renders normal mapped surfaces, but as far as I can see they are not available through the Python API.

The problem is that normal mapping is implementation-dependant, so any attempt to recreate tangents/bitangents outside of Blender is futile. We need to access the values within Blender directly.

I've seen patches around which hack this functionality into Blender but being dependent on a patch which breaks with every update is not acceptable, nor is having to provide our external talent with hacked builds. And that's providing it even works, I haven't looked that closely at it yet.

Since the values are available to Blender internally, it should be a trivial matter to expose them to the Python API, no?

I'd be extremely grateful for any response on this issue. Normal mapping is an integral part of game graphics these days and not having the ability to extract this information is a serious impediment to Blender being a viable 3D graphics package for game development.

Cheers!

-- Andy

jkoops
Posts: 3
Joined: Fri Nov 02, 2012 4:10 pm
Location: Amsterdam

Postby jkoops » Mon Nov 05, 2012 9:57 am

Hi Andy,

Just wanted to say that this is something I would very much appreciate too.

On the other hand, are you sure that tangent space definition is implementation dependent? It seems to me that two of the axis can be calculated from the UV mapping, and the third axis is a simple cross-product. From what I understand, this is what Blender does, and since you have access to the UV-mapping, you can do this yourself too.

But as you say: apparently the functionality does exist within Blender, and it would be very nice to have Python access to it.

drakfluga
Posts: 4
Joined: Sat Nov 03, 2012 9:31 pm

Postby drakfluga » Mon Nov 05, 2012 3:47 pm

jkoops wrote:Hi Andy,

Just wanted to say that this is something I would very much appreciate too.

On the other hand, are you sure that tangent space definition is implementation dependent? It seems to me that two of the axis can be calculated from the UV mapping, and the third axis is a simple cross-product. From what I understand, this is what Blender does, and since you have access to the UV-mapping, you can do this yourself too.

But as you say: apparently the functionality does exist within Blender, and it would be very nice to have Python access to it.


There's a fairly detailed writeup on the Wiki:
http://wiki.blender.org/index.php/Dev:S ... ormal_Maps

What stumps me is that I've been looking at old (2.4 and before) scripts, and it seems that tangents have been possible to extract before, but that this feature somehow disappeared in 2.5+. And nobody has bothered to put it back in, in several years.

synkarius
Posts: 1
Joined: Wed Nov 07, 2012 9:17 pm

Postby synkarius » Wed Nov 07, 2012 9:50 pm

Drakfluga:

Apply this patch to build 46548:
https://projects.blender.org/tracker/in ... 9&atid=127

I am in your exact situation. Tangents are calculated differently in Blender and the engine I am using, Away3D. I can confirm that this patch works. Yes, it is a hack, but it will get the job done. Plus, 46584 is build 2.63a, so you're not missing that much. (Current build at time of writing this is 2.64a.)

There is also this patch to the Collada exporter, which I have not tried yet:
https://projects.blender.org/tracker/in ... 9&atid=127

drakfluga
Posts: 4
Joined: Sat Nov 03, 2012 9:31 pm

Postby drakfluga » Thu Nov 08, 2012 2:37 pm

synkarius wrote:Drakfluga:

Apply this patch to build 46548:
https://projects.blender.org/tracker/in ... 9&atid=127

I am in your exact situation. Tangents are calculated differently in Blender and the engine I am using, Away3D. I can confirm that this patch works. Yes, it is a hack, but it will get the job done. Plus, 46584 is build 2.63a, so you're not missing that much. (Current build at time of writing this is 2.64a.)

There is also this patch to the Collada exporter, which I have not tried yet:
https://projects.blender.org/tracker/in ... 9&atid=127


Hi!

I actually managed to get a hold of the original patch author just yesterday, and he's updated it to the current build! As of right now, the verts and faces members of tangent_space are empty in my meshes and I'm trying to track down the problem. But he built it on his end and has verified that it does indeed work.

I would be extremely grateful to see this patch merged into the Blender trunk, as that would mean we could start relying on it more permanently. If there is anyone with dev status who would like to take it upon them to look at this, I and a big part of the gamedev community will be in your debt. ;)

azy.4849
Posts: 3
Joined: Fri Jul 26, 2013 8:00 am

Postby azy.4849 » Fri Jul 26, 2013 8:23 am

drakfluga wrote:I actually managed to get a hold of the original patch author just yesterday, and he's updated it to the current build! As of right now, the verts and faces members of tangent_space are empty in my meshes and I'm trying to track down the problem. But he built it on his end and has verified that it does indeed work.

I would be extremely grateful to see this patch merged into the Blender trunk, as that would mean we could start relying on it more permanently. If there is anyone with dev status who would like to take it upon them to look at this, I and a big part of the gamedev community will be in your debt. ;)


I too have been having this problem.

If I may ask in my potential ignorance, who exactly did you contact? On top of that, did you ever get it working and if so, how?

minyor
Posts: 1
Joined: Thu Nov 03, 2011 1:38 pm
Location: Ukraine

Postby minyor » Sun Aug 18, 2013 10:25 pm

azy.4849 wrote:I too have been having this problem.

If I may ask in my potential ignorance, who exactly did you contact? On top of that, did you ever get it working and if so, how?


Hi,
One possible reason for empty tangents can be if your mesh doesn't have uv coordinates. Did you unwrapped your meshes before export?

drakfluga
Posts: 4
Joined: Sat Nov 03, 2012 9:31 pm

Postby drakfluga » Thu Sep 26, 2013 3:02 pm

azy.4849 wrote:I too have been having this problem.

If I may ask in my potential ignorance, who exactly did you contact? On top of that, did you ever get it working and if so, how?


I talked to the author of the patch, Eugene Minov. The patch page is here: https://projects.blender.org/tracker/in ... 9&atid=127

He recently updated the patch to work with 2.68a as well.

I talked to the guy assigned to the patch, Campbell Barton, today. He says the patch is too intrusive and it "can be done lighter weight". He also says that Blender has two devs working on game dev features now and they will likely look at it. He has asked one of the devs to look into it.

I did get it working, but the patch exposes the internal mesh which Blender uses to render to the screen, which is not the same as the mesh you can normally access with the Python API. It works, but it does feel like a hack, and having the functionality built into Blender properly would be a lot neater.


Return to “Python”

Who is online

Users browsing this forum: No registered users and 0 guests