Hi, I have been stuck at trying to import Alembics via my plugin. I keep getting the Context is incorrect error. I have followed all of the methods that are available here.
I have tried the following solutions:
- Change Area.Type
- Override Context
- Low-level
- Specify the right context (I did that after trying to run the Poll() operation for each context)
But nothing is working and the error persists. According to documentation this is how alembics are imported via python:
bpy.ops.wm.alembic_import(filepath=meshPath)
The above line of code works fine in the command line and the script editor but it is not working in the plugin. Any help would be greatly appreciated!
[Update 1]
The bpy.context.area is None so I cannot really Override the context either because I was using the following way of overriding the context:
original_type = bpy.context.area.type
bpy.context.area.type = “VIEW_3D”
bpy.ops.wm.alembic_import(filepath=meshPath, as_background_job=False)
bpy.context.area.type = original_type
[Update 2] The following properties are all None.
bpy.context.area = None
bpy.context.gizmo_group = None
bpy.context.region = None
bpy.context.region_data = None
bpy.context.screen = None
bpy.context.space_data = None
bpy.context.window = None
bpy.context.workspace = None
[Update 3] Stripped down code of the plugin (Though I should mention importing directly in the operator throws some other errors but it does work in the operator.):
import bpy, os, time, socket, threading
bl_info = {
"name": "Import Helper",
"description": "Let's blender import goemetry",
"author": "Ajwad",
"version": (1, 0),
"blender": (2, 80, 0),
"location": "File > Import",
"warning": "", # used for warning icon and text in addons panel
"wiki_url": "https://docs.google.com/document/",
"tracker_url": "https://docs.google.com/document/",
"support": "COMMUNITY",
"category": "Import-Export"
}
class Import_Processor():
def __init__(self):
self.importMesh()
def importMesh(self):
try:
meshPath = "C:\\Users\\PC\\Desktop\\box.abc" # Path to the alembc file.
bpy.ops.wm.alembic_import(filepath=meshPath, as_background_job=False)
except Exception as e:
print( "Error: ", str(e) )
class ms_Init(threading.Thread):
def __init__(self, importer):
threading.Thread.__init__(self)
self.importer = importer
#Start the thread to start listing to the port.
def run(self):
try:
run_livelink = True
host, port = 'localhost', 28888
socket_ = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket_.bind((host, port))
while run_livelink:
socket_.listen(5)
client, addr = socket_.accept()
data = ""
buffer_size = 4096*2
data = client.recv(buffer_size)
if data != "":
self.TotalData = b""
self.TotalData += data
while run_livelink:
data = client.recv(4096*2)
if data : self.TotalData += data
else:
self.importer(self.TotalData)
break
except Exception as e:
print( "Error: ", str(e) )
class IMPORTHELPER_OT_import(bpy.types.Operator):
bl_idname = "importhelper.import"
bl_label = "Import Helper"
def execute(self, context):
try:
self.thread_ = threading.Thread(target = self.socketMonitor)
self.thread_.start()
return {'FINISHED'}
except Exception as e:
print( "Error: ", str(e) )
return {"FAILED"}
def socketMonitor(self):
try:
threadedServer = ms_Init(self.importer)
threadedServer.start()
except Exception as e:
print( "Error: ", str(e) )
return {"FAILED"}
def importer (self, recv_data):
try:
Import_Processor()
except Exception as e:
print( "Error: ", str(e) )
return {"FAILED"}
def menu_func_import (self, context):
self.layout.operator(IMPORTHELPER_OT_import.bl_idname, text="Import helper plugin")
def register():
bpy.utils.register_class(IMPORTHELPER_OT_import)
bpy.types.TOPBAR_MT_file_import.append(menu_func_import)
def unregister():
bpy.types.TOPBAR_MT_file_import.remove(menu_func_import)
if __name__ == "__main__":
The second class that sends dummy data over socket. You can install a VS code extension called “Code Runner” by “Jun Han” to run this script once the plugin is installed in blender and you started the plugin by going to: File > Import > Import helper plugin
import sys, socket
host, port = 'localhost', 28888
final_data = "dummydata"
# Create the socket & send the request
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.sendall(bytes(final_data, encoding='utf-8'))
s.close()
You can get the box alembic from here: https://drive.google.com/file/d/1K2vlb929YZihsx_294LRpE6BdHPyXHX_/
Thanks!