# How to get the filepath to an imported mesh?

I wanted to add some additional functionality to this Texture Map Batch Bake Addon (it's not mine, no longer maintained, and I take no credit for it whatsoever).

By default it exports baked textures to a //Textures/ folder in the same folder as the .blend file:

path = StringProperty(
name='Bake Folder',
subtype='DIR_PATH',
default='//textures/',
description='Save baked images from this object in this folder',
)


In my case though I'll be using this addon on an imported fbx and it doesn't look like the addon keeps track of where it was imported from.

Do I have to somehow use the fbx_io operator to get access to the filepath of the fbx it imported?

I couldn't find any threads about how to get the filepath of an imported fbx post-export, and was hoping someone knows of a way to get it.

Here is a "hack" that will work in the UI.

The following defines a filebrowser panel, that will never poll, but the poll method looks at the active operator, and if it's fbx import, writes the import directory (could use filepath for filename) into the class property "hack"

import bpy
from bpy.props import StringProperty
class SimplePanel(bpy.types.Panel):
bl_label = "Panel"
bl_idname = "simple_panel"
bl_space_type = 'FILE_BROWSER'
bl_region_type = 'TOOLS'
hack = None
@classmethod
def poll(cls, context):
op = context.active_operator
if op and op.bl_idname == "IMPORT_SCENE_OT_fbx":
cls.hack = op.directory
return False
# needs a draw method
def draw(self, context):
pass
def register():
bpy.utils.register_class(SimplePanel)

def unregister():
bpy.utils.unregister_class(SimplePanel)

if __name__ == "__main__":
register()


Ok we've registered this dummy panel, let's test it in the console.

>>> bpy.types.simple_panel.hack is None
True
# imported an fbx
>>> bpy.ops.import_scene.fbx('INVOKE_DEFAULT')
{'RUNNING_MODAL'}
# check again
>>> bpy.types.simple_panel.hack is None
False
# there's the directory fbx was imported from
>>> bpy.types.simple_panel.hack
'/home/batfinger/'
# this one I cancelled
>>> bpy.ops.import_scene.fbx('INVOKE_DEFAULT')
{'RUNNING_MODAL'}
# what do we get this time
>>> bpy.types.simple_panel.hack
''
# ok can do something with that.


PS was hoping to use bpy.types.Macro but have never had any joy with getting operators defined to macro after import fbx to run.

Conversely, Could use something like this to import your fbx. Creates a new scene, imports fbx and sets the path of the objects for bbake if installed. Would need to edit in the settings you use for import. (and fix paths)

import bpy
context = bpy.context
screen = context.screen
scene = bpy.data.scenes.new("fbx_import")
screen.scene = scene

# import the fbx
# do this properly i suppose with os.path
directory = "/home/batfinger/"
filepath = "/home/batfinger/untitled.fbx"
bpy.ops.import_scene.fbx(filepath=filepath)
for ob in scene.objects:
if hasattr(ob, "bbake"):
ob.bbake.ob_settings.path = directory

• Is it possible to get this hack working with that addon? I just now tried to add it to the init of it. Got the register/unregister calls set up. I'm not quite sure where to stick that simple_panel.hack call though. Before the bake there's a place in batch_bake_operators where it checks to see whether there's a valid director or not. Edit: Thanks a lot for the hack! – kusogaki Oct 13 '17 at 2:12
• Gotta admit haven't tested this. Assumed you can set the path with obj.bbake.obj_settings.path = bpy.types.simple_panel.hack for each object in scene.objects after import and before you batch bake. (ie No need to alter the addon source) – batFINGER Oct 13 '17 at 4:05

I'm not an expert but the import addon gets the folder of the imported file (eg: "basedir = os.path.dirname(filepath)"), but its use is probably temporary to just read the file and other files (original textures?) in the same path, at import time. see 2.79's import_fbx.py "load" method, line 2273.

This original path, if I got it, could be saved in some imported object custom property, to allow later usage, like saving baked texture where the original file was when imported, but I guess it is not, now, so it is lost after the import operation.

So, unless you request (and get) this to be done by the importer maintainer, you could patch it yourself to read that path and save it into a custom property (unfortunately I can't help you with this, I'm not into this).

Another way could be to write a (custom) import log to a text file (maybe even inside blender text datablocks) so that you can later access it to know where/what you got when you imported the file...

Although, the original folder could (easily) not be "there" anymore, removed or renamed, or it could be readonly... so you can't take it for granted to be able to write there, unless you are following a well defined after-import workflow...

for sure you'll have to check if that (still) exist, is writable, and decide what to do if either things are false.

• Thanks a lot for the help! I'm really inexperienced with python (have more experience with VBA). "This original path...could be saved in some imported object custom property..." This sounds like the best option to me. Unfortunately, I wouldn't really no where to start to get it implemented in that addon I linked. The after-import workflow would be well defined and required. – kusogaki Oct 13 '17 at 2:05