Relative import when running blender from command line

Hello, I’m trying to create a batch file that will run an export script of mine when Blender launches.

The .bat itself seems to run correctly, and what I’ve done is have it launch blender with a .blend file selected, and a python script. Unfortunately, it doesn’t like when I say “from . import ExportModel”. It seems that it doesn’t know where “.” points to. The error I get is:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "<string>", line 2, in <module>
SystemError: Parent module '' not loaded, cannot perform relative import

My question is: how can I rephrase the “from . import x” statement to import an addon in the blender scripts directory?

Sorry, a bit of a python noob here.

Did you check this?

Thanks! I get a different error now, though.

I necro’d this guy’s thread because it’s the same error. http://www.blenderartists.org/forum/showthread.php?334117-AttributeError-_RestrictData-object-has-no-attribute-filepath

However I’m not able to fix it the same way he did.

See here:

http://wiki.blender.org/index.php/Extensions:2.6/Py/API_Changes#Restricted_Context

That is the proper way to go about it.

A hacky way is to register a one-shot app handler callback for scene updates to defer the execution, but it’s really a hack.

I’m sorry, I don’t understand. What I’m trying to do is run Blender from command line with some parameters so it opens a .blend file, runs an exporter add-on I’ve written, then closes. I guess I’m just wondering how to make use of the -P argument without running into this restricted access error.

So you basically want to load a .blend in background mode, ensure your exporter addon is activated, then execute a python script that calls an operator of your addon?

You should avoid any access to bpy.data and bpy.context in register() or functions you call from there in your addon. You should access them in the operator’s execute() method only, or in the python script you wanna execute after loading your .blend on the command line.

Would be helpful to see your actual code.

Precisely. I sent you a PM with the code.

I sent you a PM, have you had a chance to look at it yet? Sorry if this is a simple problem that I’m just not grasping.