spyder-ide / spyder

Official repository for Spyder - The Scientific Python Development Environment
https://www.spyder-ide.org
MIT License
8.25k stars 1.6k forks source link

ExtAPI,ViewOrientationType,Ansys,GraphicsResolutionType,GraphicsBackgroundType ***Undefined name #21850

Closed sk286102 closed 2 months ago

sk286102 commented 7 months ago
import os

from PIL import Image
import ansys.mechanical.core as mech
from ansys.mechanical.core.examples import delete_downloads, download_file
from matplotlib import image as mpimg
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation

# %%
# Embed mechanical and set global variables

app = mech.App(version=241)
globals().update(mech.global_variables(app, True))
print(app)

cwd = os.path.join(os.getcwd(), "out")

def display_image(image_name):
    plt.figure(figsize=(16, 9))
    plt.imshow(mpimg.imread(os.path.join(cwd, image_name)))
    plt.xticks([])
    plt.yticks([])
    plt.axis("off")
    plt.show()

# %%
# Configure graphics for image export
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ExtAPI.Graphics.Camera.SetSpecificViewOrientation(ViewOrientationType.Iso)
ExtAPI.Graphics.Camera.SetFit()
image_export_format = GraphicsImageExportFormat.PNG
settings_720p = Ansys.Mechanical.Graphics.GraphicsImageExportSettings()
settings_720p.Resolution = GraphicsResolutionType.EnhancedResolution
settings_720p.Background = GraphicsBackgroundType.White
settings_720p.Width = 1280
settings_720p.Height = 720
settings_720p.CurrentGraphicsDisplay = False

I am running the above program for the first time in spyder 3.10. This program gives the correct output but in the workspace on the left hand side it shows red cross error and stating that Undefined name for the line which contains ExtAPI,ViewOrientationType,Ansys,GraphicsResolutionType,GraphicsBackgroundType.

Is there any module that I need to import in my code?

dalthviz commented 6 months ago

Hi @sk286102 when you say red cross error you mean these errors?:

image

If that is the case, those are just messages from the code analysis package that Spyder uses (which does a static code analysis). Since seems like you are able to run your code without issue my guess is those errors/warnings are false positives and you can ignore them (probably running one of the early imports you have in your code makes the mentioned definitions available somehow at runtime).

If the above makes sense, to make Spyder ignore specific lines and prevent specific errors/warning messages to appear in that section of the editor you can write a # noqa comment at the end of the specific line you want to be ignored. With that in mind, that last part of your code could end up looking something like:

# %%
# Configure graphics for image export
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ExtAPI.Graphics.Camera.SetSpecificViewOrientation(ViewOrientationType.Iso)  # noqa
ExtAPI.Graphics.Camera.SetFit() # noqa
image_export_format = GraphicsImageExportFormat.PNG # noqa
settings_720p = Ansys.Mechanical.Graphics.GraphicsImageExportSettings() # noqa
settings_720p.Resolution = GraphicsResolutionType.EnhancedResolution # noqa
settings_720p.Background = GraphicsBackgroundType.White # noqa
settings_720p.Width = 1280
settings_720p.Height = 720
settings_720p.CurrentGraphicsDisplay = False

And with that no errors should appear:

image

For more info about the static code analysis tools that Spyder uses you can check the Spyder docs page: https://docs.spyder-ide.org/current/panes/editor.html?#linting-and-code-style

Let us know if the info above helps!

dalthviz commented 2 months ago

Closing due to lack of response