architecture-building-systems / revitpythonshell

An IronPython scripting environment for Autodesk Revit and Vasari
MIT License
494 stars 113 forks source link

wpf import #46

Closed gtalarico closed 7 years ago

gtalarico commented 7 years ago

<br />import clr
<br />clr.AddReference('IronPython.Wpf')
<br />import wpf
<br />
<br />```
<br />I can run the above code in an IronPython interpreter, and inside a pyRevit script, but when I try to run it in RPS, I get `ImportError: No module named wpf`
<br />
<br />Any ideas what might be the cause and if there is fix?
gtalarico commented 7 years ago

I am hoping @daren-thomas @eirannejad might now about this :) https://github.com/gtalarico/revitpythonwrapper/issues/4 @hdm-dt-fb

CyrilWaechter commented 7 years ago

Hello, I asked the same question more than 2 years ago ;-). This thread's answer should solve the issue : http://stackoverflow.com/questions/27580543/importerror-no-module-named-wpf-in-revit-environment-only-user-interface check this one too : http://stackoverflow.com/questions/32013933/ironpython-wpf-withe-revitpythonshell

PMoureu commented 7 years ago

Looks like there is a conflict between PyRevit and RPS, the first using wpf module wins ! And makes the other crash...

in RPS, i can import the wpf module directly in the file init.py (works with the 3 lines above), BUT PyRevit doesn't like it, at all : settings or extensions dialogs crash, maybe other components too.

Can someone confirm this lead ?

(I tried some fixes in RPS on a Revit 2015, compiling with the last IronPython DLLs, maybe that's why i get these results.)

eirannejad commented 7 years ago

pyRevit is importing Wpf in scriptutils.userinput with no problems.

RPS does not include the IronPython.Wpf.dll in its installation package. pyRevit does. But the IronPython versions are different between the two. RPS is running 2.7.3 and pyRevit is run under 2.7.6

Install IronPython 2.7.3 and grab IronPython.Wpf.dll from the installation folder (and while you're at it also grab IronPython.SQLite.dll) and copy to RPS installation folder.

Test the script again.

UPDATE: I tested this myself and didn't work

gtalarico commented 7 years ago

Thanks to all of you for trying to help out on this! :)

I did some more tests, and I just confirmed something I suspected: the error definitely caused by a conflict with pyRevit; perhaps the IP or wpf module version. Let me add some more context:

I have some code in rpw that uses wpf to create forms. rpw is imported and used almost exclusively within pyRevit scripts.

To ensure rpw doen't break during development, I wrote a bunch of unittests

In order to run the scripts independently of pyRevit, a while ago I added the test runners to RPS External Scripts (see toolbar image) image image

I have been using these tests for a while, but since the last update on pyRevit, I noticed the forms tests which used to run fine, kept breaking on the import wpf line I tried a few different import methods, but none of them would work in the RPS tests. To confirm my hunch, I removed pyRevit from the initialization, and voila, tests work again!

image

Now with pyRevit enabled again:

image

eirannejad commented 7 years ago

So this means since pyRevit loads a newer version of the IronPython.wpf at startup, it breaks the imports in RPS since it's IronPython engine is older right?

daren-thomas commented 7 years ago

I guess that means it is time to update the IronPython version of RPS! I'll work on it, but next month, as I a have a high work load right now.

fbe-work commented 7 years ago

Thank you @gtalarico !! Yes, with a disabled pyRevit addin I can use the forms successfully from RPS.

fbe-work commented 7 years ago

@daren-thomas it would be really awesome if scripts could be run equally on RPS and pyRevit. I use RPS a lot for prototyping and exploring, while pyRevit brings smooth deployment of these scripts to the teams.. @eirannejad seems also to me that this is what is happening: When I try to load IronPython.dll, IronPython.Modules.dll and IronPython.Wpf.dll explicitly from pyRevit loader addin path, inside a pyRevit-disabled Revit-RPS session, I can get to IronPython and Modules but not Wpf..

PMoureu commented 7 years ago

From the CLR context, i get these results:

From RPS, without Pyrevit : (note : i'm using the last ironpython DLL) clr_nopyrevit

From PyRevit (not updated) clr_withpyrevit

From RPS, if PyRevit is using wpf rpsclr_withpyrevit

(Special note: you cannot use WPF by importing the package Wpf explicitely, IronPython manages this for us, the only way is : import wpf
link : https://github.com/IronLanguages/main/issues/1273)

UPDATED :

I updated PyRevit... now it works from both sides. What i've done within RPS before:

eirannejad commented 7 years ago

@gtalarico I updated the RPS core dlls and AvalonEdit. This needs your review though: https://github.com/architecture-building-systems/revitpythonshell/pull/48

daren-thomas commented 7 years ago

I merged PR #48 to master and created an installer for RPS 2017. Does this solve the issue?

gtalarico commented 7 years ago

Sounds good to me. Thanks guys

eirannejad commented 7 years ago

@gtalarico @PMoureu @hdm-dt-fb Does the new update solve the wpf import issue?

fbe-work commented 7 years ago

@all thank you so much!! I am very happy about the IronPython update in RPS! I'd love to test it, but unfortunately I am currently tied here to Revit 2015 or 2016 and the installer is for 2017..

eirannejad commented 7 years ago

@hdm-dt-fb Install it using the 2017 installer and then copy the RevitPythonShell.addin file into your other Revit version. It works. The addin files are under: %appdata%\Autodesk\Revit\Addins

fbe-work commented 7 years ago

@eirannejad thank you!! I did not know they are interchangeable.. Since there is no 2017 rvt install on this machine, there was no 2017-addin to be found, but with your information I just redirected the path of the 2016 addin, and that works - awesome!! Now I'll check for the wpf and post again..

fbe-work commented 7 years ago

hmm.. RPS seems to find the newer IronPython.Wpf, which it retrieves from pyRevit path, but I still seem to be unable to do: "import wpf" as in posted repl session below. I thought that it would be a good sign it loads IronPython.Wpf.dll from pyRevit? [please note, that due to our setup our pyRevit version is slightly behind: 4.1. I hope this does not cause additional confusion.]

IronPython 2.7.7 (2.7.7.0) on .NET 4.0.30319.42000 (64-bit)
Type "help", "copyright", "credits" or "license" for more information.
>>> import clr
>>> clr.AddReference("IronPython")
>>> clr.AddReference("IronPython.Modules")
>>> clr.AddReference("IronPython.Wpf")
>>> import wpf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named wpf
>>> import rpw
[INFO] Running In Revit
>>> for r in clr.References:
...     print(r)
... 
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
RevitAPI, Version=16.0.0.0, Culture=neutral, PublicKeyToken=null
RevitAPIUI, Version=16.0.0.0, Culture=neutral, PublicKeyToken=null
RpsRuntime, Version=2017.0.0.0, Culture=neutral, PublicKeyToken=null
RevitLookup, Version=2016.0.0.11, Culture=neutral, PublicKeyToken=null
IronPython, Version=2.7.7.0, Culture=neutral, PublicKeyToken=7f709c5b713576e1
IronPython.Modules, Version=2.7.7.0, Culture=neutral, PublicKeyToken=7f709c5b713576e1
IronPython.Wpf, Version=2.7.6.3, Culture=neutral, PublicKeyToken=7f709c5b713576e1
PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
>>> for r in clr.References:
...     print(r.Location)
... 
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.dll
C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll
C:\Program Files\Autodesk\Revit Architecture 2016\RevitAPI.dll
C:\Program Files\Autodesk\Revit Architecture 2016\RevitAPIUI.dll
C:\Program Files (x86)\RevitPythonShell2017\RpsRuntime.dll
C:\ProgramData\Autodesk\Revit\Addins\2016\RevitLookup.dll
C:\Program Files (x86)\RevitPythonShell2017\IronPython.dll
C:\Program Files (x86)\RevitPythonShell2017\IronPython.Modules.dll
C:\HdM-DT\pyRevit4\pyrevitlib\pyrevit\loader\addin\IronPython.Wpf.dll
C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework\v4.0_4.0.0.0__31bf3856ad364e35\PresentationFramework.dll
>>> form = rpw.forms.SelectFromList("test", ["a", "b"])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\HdM-DT\pyRevit4\extensions\RevitPythonWrapper.lib\rpw\forms\forms.py", line 70, in __init__
AttributeError: 'NoneType' object has no attribute 'LoadComponent'
PMoureu commented 7 years ago

@eirannejad : :tada: :cake: Many thanks Sorry i have no time to setup and run some tests before next week, it should work great if the 2 engines are equals, as it works with my hack.

@hdm-dt-fb : issue may be related to PyRevit version, an update solved this on my machine, it's worth a try

eirannejad commented 7 years ago

Yes please update to pyRevit head (4.3) that uses the 2.7.7 IronPython

fbe-work commented 7 years ago

@PMoureu thank you! yes that works!! thank you all!! @eirannejad we definitively will, but we are lagging a bit behind, since we want to verify our scripts still run, before we update our local-proxy-git-thing..
If it was only up to me, I would have preferred the direct git update..

gtalarico commented 7 years ago

Just confirming, the update resolved my issue as well

PMoureu commented 7 years ago

Everything work for me too, i also tried a setup from scratch on Revit2017, without PyRevit, maybe we should include IronPython.Wpf.dll in the RPS directory to make it easier for newcomers.