[New Feature Preview] Python 3 Support Issue Thread

Most welcome @Cezmi - it’s hot off the press this morning :smiley:

1 Like

Hello
some remarks with Dynamo 2.8 (but which are rather related to PythonNet)

  • Problem with auto-completion (native Python list has no “Add” method and PythonNet does not dynamically convert to NetList )


  • refs and out Parameter does not work (missing in PythonNet, I don’t know if you have the possibility to integrate it, or if there is a possibility of workaround)



  • Ditto for COM interfaces (missing in PythonNet, force to use reflection)




solution with Reflection without a wrapper

Note: clr.AddReferenceByName method does not exist under Pythonnet

I imagine that resolving Net compatibilities with de Pythonnet (especially coming from IronPython) should not be easy

8 Likes

Hi @c.poupin. You posted quite a few things there, let me go one by one.

Autocompletion
As you noticed, autocompletion for the CPython3 engine currently relies on the same mechanism used for the IronPython2 engine. Having an autocompletion engine specific for the new engine is currently on the works, but it didn’t make it to Dynamo 2.8.

Ref and Out
These two actually work with PythonNET but the way they do is not like in Iron Python and unexpected in my opinion. The ref/out parameters need to be provided but they are considered as inputs only. The output is actually obtained from the result of the function call, which is surprisingly a Python tuple. Here are a couple of examples showing this:

Out parameter in Dictionary.TryGetValue

Ref parameter in Interlocked.Exchange

COM interop
For this one I don’t have a better workaround than yours, so I will have to cheat :slight_smile:
Here is an example using Dynamo’s own .NET classes to help interact with Excel. Unfortunately
some of these functions are not public, but the idea can be used with a custom library that is part of a package for instance. That helps overcome the limitations regarding COM interop in PythonNET.

Thank you so much for the feedback @c.poupin. We will update our documentation with the issues you provided so that people can use it as reference in case they find themselves against them.

6 Likes

@martin.misol thank you for this feedback, especially for the explanation of the ref/out parameters.

2 Likes

You are welcome @c.poupin :slight_smile:

Just wanted to add an alternative for COM interop. This can be achieved also using Python --> COM integration, without going through .NET. There is a module called pywin32 that serves this purpose. Here is an example equivalent to the one I showed before but using this pywin32 instead of relying on a custom .NET library.

2 Likes

Hi @ustncz. I tried and got the same error you did. I think it’s because we are running Python from .NET, so the threading mode is already initialized.
I was actually able to use the library if I commented out the offending line, where CoInitializeEx is called.

Not sure if that causes other issues down the road, so please beware.

3 Likes

Oh my GOD! I should have come to you much earlier!! You saved me!!

Thank you very much!!

1 Like

Unsure if this is an actual error, or if it’s Error 45 (the error is 45 cm from the screen…)
I’ve followed the guidelines here to the point: Customizing Dynamo's Python 3 installation · DynamoDS/Dynamo Wiki · GitHub
and have been able to succesfully install ie pandas and numpy through pip.
Trying to use any of the modules gives me error messages like this one:


Are there any limitations or mishaps in my sparse code that I’m unaware of? Or is it something else completely?

I am not an expert here and haven’t tried myself, but perhaps try changing line 13 to plt.pyplot.plot(lst,lst) and see where you get (would be a 45 degree line on a line chart if I am not mistaken).

haha, you’re totally right, Jacob! :expressionless: Alas, the error was indeed off screen…
Here is a piece of the Mandelbrot fractal to cheer my self up:

8 Likes

Nah - this is more of a ‘wait this didn’t work, is it the code or the implementation?’ thing which the community is here for. :slight_smile:

For reference, any issues I have seen have warnings like now ‘library of that name found,’ and will throw the error early when you do your imports (assuming you do them together and early), but your mileage may vary. :smiley:

1 Like

Will we eventually be required to convert all of our old IronPython to Python 3, or will IronPython be supported in Dynamo for the foreseeable future.

Hi, the current version of IronPython will be supported as a Dynamo extension which is wrapped as a Dynamo package so user should continue be able to use IronPython and leverage nodes written in IronPython. In a future version, CPython will be the default Python engine of Dynamo and IronPython will not be available OOTB, but you can always get it from package manager. So in short, no requirements yet.

3 Likes

What is the best way to get names from .net enumerators? I see that values are returned instead of names.

This worked for me.

1 Like

I’m still struggling with collections and enumerators with CPython3. Here is an example using Dynamo for C3D.
If I want to get a particular alignment style with IronPython, I simply use:
civdoc.Styles.AlignmentStyleCollection["MyStyleName"]

This does not work with CPython3. Can someone shed some light on this?

Try importing the inspect module and using OUT = inspect.getmembers(civdoc.Styles) and see what you get.

That said, AFAIK Python 3 is unsupported in Dynamo for Civil 3D at this time, so you may be missing something in that integration as well.

That did not work for me. I get an error that says “Cannot convert object to target type”.
I am using the latest Civil 3D beta.

Paste your python code or DM me the DYN and I’ll take a loose stab at it. :slight_smile:

Thank you for looking at it. Here is the working code to get the object Id of an alignment style using IronPython. I’m trying to figure out what the CP3 equivalent is.

import clr

# Add Assemblies for Civil 3D APIs
clr.AddReference('AeccDbMgd')

# Import references for Civil 3D
from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices.Styles import *

civdoc = CivilApplication.ActiveDocument

myStyleId = civdoc.Styles.AlignmentStyles["Basic"]

OUT = myStyleId

Applicable API documentation:
Document Style Collection
Alignment Styles from Styles Root
AlignmentStyleCollection