Start a new topic

Integrating issues w/ suite22 API

Greetings,


Would like to run my old python scripts, started pre 3.0 but modified with print() so in theory they should work.  I'll try to drop my script here, but I think this is a configuration issue.  Suite 22 API contains python 2 and python 3.


Here's the error I get:

Traceback (most recent call last):

  File "C:\Presagis\Suite22\OpenFlight_API_22_0\bin_x64\release\mgapilib.py", line 14, in swig_import_helper

    return importlib.import_module(mname)

  File "C:\Python\Python38\lib\importlib\__init__.py", line 127, in import_module

    return _bootstrap._gcd_import(name[level:], package, level)

  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import

  File "<frozen importlib._bootstrap>", line 991, in _find_and_load

  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked

ModuleNotFoundError: No module named '_mgapilib'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "e:\utils\get_flt_comment.py", line 25, in <module>

    from mgapilib import *

  File "C:\Presagis\Suite22\OpenFlight_API_22_0\bin_x64\release\mgapilib.py", line 17, in <module>

    _mgapilib = swig_import_helper()

  File "C:\Presagis\Suite22\OpenFlight_API_22_0\bin_x64\release\mgapilib.py", line 16, in swig_import_helper

    return importlib.import_module('_mgapilib')

  File "C:\Python\Python38\lib\importlib\__init__.py", line 127, in import_module

    return _bootstrap._gcd_import(name[level:], package, level)

ModuleNotFoundError: No module named '_mgapilib'


Here's the path:

PYTHONHOME=C:\Python\Python38

PYTHONPATH=C:\Presagis\Suite22\OpenFlight_API_22_0\bin_x64\release


Here's the script?

 

##  Author: Shawn Allen 02.24.2017
##
##  get_flt_comment.py
##  portions of this code from egnew1.py
##
##  Script to inventory or revise header comments in OpenFlight(tm) files
##
##  Dependencies: mgapilib
##
##  API functions used:
##     mgInit(), mgExit(), mgSetNewOverwriteFlag(), mgGetLastError(),
##	  mgNewRec(), mgSetComment(), mgGetComment(),
##	  mgAttach(), mgInsert(), mgDeleteComment(),
##	  mgSetVtxCoord(), mgFree(),
##	  mgOpenDb(), mgWriteDb(), mgCloseDb().
##
##

target_string = 'EDGEDEF:'
edge_list = []

import sys

# import OpenFlight API module
from mgapilib import *

# disable generic OFAPI messages (otherwise they echo)
######################################################

mgSetMessagesEnabled (MMSG_STATUS, MG_FALSE)
mgSetMessagesEnabled (MMSG_WARNING, MG_FALSE)
mgSetMessagesEnabled (MMSG_ERROR, MG_FALSE)

def main ():

	ic0 =   0.0,   0.0, 0.0
	ic1 = 100.0,   0.0, 0.0
	ic2 = 100.0, 100.0, 0.0
	ic3 =   0.0, 100.0, 0.0

##	 note: throughout this sample, many function return values are ignored.
##		specifically: mgNewRec, mgAttach, mgInsert, mgSetVtxCoord, mgSetComment, 
##		mgDeleteComment, etc
##		normally, you should pay attention to these values and consider appropriate
##		action upon function failures.  
##		

   # check for proper arguments 
	if len(sys.argv) < 2:
		print ('\nUsage: %s <flt_filename>\n' % (sys.argv[0]))
		print (' Gets the header comment field; adding <src new .. src_x new_x>')
		print (' strings, this script  will change <src> to <new> in  comment.')
		print (' Separate each comment string by a space.\n')
		return
	
   # initialize the OpenFlight API
   # always call mgInit BEFORE any other OpenFlight API calls 
   #
	mgInit (None, None)

   # open the database 
#	print "\nOpening database: ", sys.argv[1], " \n"
	db = mgOpenDb (sys.argv[1])
	if db == None:
		msgbuf = mgGetLastError()
		print (msgbuf, "\n")
		mgExit()
		return

  # comment string from user input

	if len(sys.argv) <= 2:
            # no string args provided, so get comments instead
		comment_str = mgGetComment (db)

  # reformat to correct edit syntax

		print ('Header comment:\n', comment_str)

		ok = mgCloseDb (db)

		if ok == MG_FALSE:
			print ("Error closing database!\n")
	
			# always call mgExit() AFTER all OpenFlight API calls 
			mgExit ()
		exit()

  # check for sufficient arguments

	elif len(sys.argv) > 2:
		arg_list = sys.argv[2:]
	if len(arg_list) < 2:
		print ('Error: too few arguments!')
		exit()

	check_from = arg_list[0::2]
	change_to = arg_list[1::2]

  # get FLT header comment 

	comment_str = mgGetComment (db)

  # split edge data from TileSize
 
	c_tokens = comment_str.split('\n')
	c_header_str = c_tokens[0]
	c_tail_list = c_tokens[1:]

  # reformat edge data

	tmp_str = ''
	for m in c_tail_list:
		tmp_str = tmp_str + str(m)
	tmp_list = tmp_str.split('EDGEDEF: ')    

	del tmp_list[0]
	edge_counts, edge_labels = [],[]

  # split edges into edge_num, edge_label

	for m in tmp_list:
		temp_str = m
		temp_list = temp_str.split(' ')

		edge_counts.append(temp_list[0])
		edge_labels.append(temp_list[1])

  # set modify flag (default)
	changed_rec = False

  # label (string) replacement
  # for each argument <src> given, check edge_labels

	for i in xrange(0, len(edge_labels)):
		for x in xrange(0, len(check_from)):
			if edge_labels[i] == check_from[x]:
				edge_labels[i] = change_to[x]
				changed_rec = True

  # re-construct comment string

		if changed_rec == True:
#			print c_header_str, changed_rec
#			print ('labels:', edge_labels)
			out_str = ''
			for n in xrange(0, len(edge_counts)):
				out_str += 'EDGEDEF: ' + edge_counts[n] + ' ' + edge_labels[n] + '\n'
#			print ('output string:\n', out_str)
			out_comment = c_header_str + '\n' + out_str
			print ('Comment:\n', out_comment, type(out_comment))

  # write comment
			ok = mgSetComment(db, out_comment)
			print (ok)

		else:
			print ('Warning: search strings not found in file.')

  # write and close the database 
	ok = mgCloseDb (db)
#		print ('close ok:', ok)
	if ok == MG_FALSE:
		print ("Error closing database\n")

  # always call mgExit() AFTER all OpenFlight API calls 
		mgExit ()

main()

 

 

 


Looks like your script is not able to find _mgapilib.pyd


I suspect you missed the new addition to the docs on how to set up for standalone execution of scripts.


From the OpenFlight 22 docs:

Depending on which version of Python you want to use, you must copy the corresponding mgapilib.pyd from either the Python2 or Python3 subfolder into the parent bin_x64\release folder. All of the OpenFlight Script module files must be in the same folder before you can run OpenFlight Script in a stand alone program environment.


When we started supporting Python2 and Python3 simultaneously, we had to make two versions of _mgapilib.pyd. One for Python2 and one for Python3. If you notice, there are two folders in ../Suite22/OpenFlight_API_22_0/BIN_X64 named Python2 and Python3. each of these folders contains the _mgapilib.pyd for the corresponding version of python.


Just copy this file into the parent directory. (Note: don't cut or move the file because you will need to keep it around in case you switch back and forth from python2 to python3)

Hm.  Yeah, I missed that.


OK, file is copied from Suite22\OpenFlight_API_22_0\bin_x64\release\Python3.


Python is 3.8.0:

E:\Nads\ProjectData\Databases\miniSim\TMT\simple_rural\simple_rural2\flt>python --version

Python 3.8.0


Similar issues remain:


E:\Nads\ProjectData\Databases\miniSim\TMT\simple_rural\simple_rural2\flt>python e:\utils\get_flt_comment.py 2ln_3way_3x1_-1320x_0y_0r.flt

Traceback (most recent call last):

  File "C:\Presagis\Suite22\OpenFlight_API_22_0\bin_x64\release\mgapilib.py", line 14, in swig_import_helper

    return importlib.import_module(mname)

  File "C:\Python\Python38\lib\importlib\__init__.py", line 127, in import_module

    return _bootstrap._gcd_import(name[level:], package, level)

  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import

  File "<frozen importlib._bootstrap>", line 991, in _find_and_load

  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked

  File "<frozen importlib._bootstrap>", line 657, in _load_unlocked

  File "<frozen importlib._bootstrap>", line 556, in module_from_spec

  File "<frozen importlib._bootstrap_external>", line 1101, in create_module

  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed

ImportError: DLL load failed while importing _mgapilib: The specified module could not be found.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "e:\utils\get_flt_comment.py", line 25, in <module>

    from mgapilib import *

  File "C:\Presagis\Suite22\OpenFlight_API_22_0\bin_x64\release\mgapilib.py", line 17, in <module>

    _mgapilib = swig_import_helper()

  File "C:\Presagis\Suite22\OpenFlight_API_22_0\bin_x64\release\mgapilib.py", line 16, in swig_import_helper

    return importlib.import_module('_mgapilib')

  File "C:\Python\Python38\lib\importlib\__init__.py", line 127, in import_module

    return _bootstrap._gcd_import(name[level:], package, level)

ImportError: DLL load failed while importing _mgapilib: The specified module could not be found.

 

 

sorry.

Is your environment variable: PRESAGIS_OPENFLIGHT_SCRIPT set up?


Well - it's set, but I can't say it's right..


points to C:\Presagis\Suite22\OpenFlight_API_22_0\bin_x64\release

hm... that should have done it.  It looks like you have everything set up that I can think of.

  • I notice there is a pycache folder in your bin directory. I'm not sure if this could be causing problems. Try renaming it?
  • Another thought is that perhaps Environment variables were changed and the console was not restarted? This would cause the console to ignore the new Environment varaibles. 
  • The error you are getting is failure to load the dll. That could also happen if the dll depends on something that is missing. In this case it is mgapilib.dll, so perhaps the API has been corrupted. Can you try re-installing it?

Oh and double check that C:\Python\Python38 is the correct path to your python install.

Will do, and I'll try rebooting the PC as well. Thx

OK, removed _pycache_, rebooted PC.  Same error.


I just installed the API today, does it make sense to think it's been corrupted?  I did a complete install, but I chose Visual Studio 2017 for which files to use.

Gr.  Sorry, make that Visual Studio 2015 (VC14)

OpenFlight_API_Release_Notes - page 7:

If you are developing standalone scripts, you will also need to install Python 2.7.x.


Is this true?  I have 278 installed but it's not in the C:\Python folder, it lives in it's own folder.


I also do not have PRESAGIS_OPENFLIGHT_PLUGIN_DIR set (p11, Release Notes).


*I copied the 278 into c:\Python, no changes to environment variables, same error.


E:\Nads\ProjectData\Databases\_dev\flt>set PYTHON

PYTHONHOME=C:\Python\Python38

PYTHONPATH=C:\Presagis\Suite22\OpenFlight_API_22_0\bin_x64\release


Any ideas what I should try next?

The API is built using Python 3.7.6. I doubt it, but perhaps the Python 3.8 you have installed is not compatible with 3.7.6 causing an issue.

I'll do some tests on my side and see if I can figure anything else out.

thanks!

Login to post a comment