C#编写可导入Blender Python导出脚本的DLL遇类型错误求助
Hey there! Let's break down why you're seeing that CS0012 error and fix your DLL for Blender Python step by step.
The System.Windows.Media.Media3D.Vector3D class lives in PresentationCore.dll, which is a WPF-specific library tied directly to the full .NET Framework—not .NET Standard. When you try to use it in a .NET Standard class library, you're running into compatibility gaps: .NET Standard doesn't include all the framework-specific types (like the exact ValueType implementation WPF relies on) that Vector3D needs to work.
Worse, even if you managed to compile it, Blender's Python runtime (which uses clr to load .NET assemblies) won't have access to WPF libraries anyway—so you'd hit runtime errors even if the build succeeded.
Since you only need basic 3D vector operations (add, subtract, dot product, cross product), it's way better to create a lightweight, self-contained class instead of relying on WPF's heavyweight, framework-locked Vector3D. Here's a minimal implementation that works perfectly in .NET Standard and plays nicely with Blender Python:
using System; namespace BlenderVectorTools { public class Vector3 { public double X { get; set; } public double Y { get; set; } public double Z { get; set; } public Vector3(double x, double y, double z) { X = x; Y = y; Z = z; } // Addition operator public static Vector3 operator +(Vector3 a, Vector3 b) { return new Vector3(a.X + b.X, a.Y + b.Y, a.Z + b.Z); } // Subtraction operator public static Vector3 operator -(Vector3 a, Vector3 b) { return new Vector3(a.X - b.X, a.Y - b.Y, a.Z - b.Z); } // Dot product calculation public double Dot(Vector3 other) { return X * other.X + Y * other.Y + Z * other.Z; } // Cross product calculation public Vector3 Cross(Vector3 other) { return new Vector3( Y * other.Z - Z * other.Y, Z * other.X - X * other.Z, X * other.Y - Y * other.X ); } // Optional: Make debugging easier in Python public override string ToString() { return $"Vector3({X}, {Y}, {Z})"; } } }
- No external dependencies: This class uses only basic .NET Standard types, so it compiles cleanly and loads in Blender's Python runtime without missing assembly errors.
- Exactly what you need: It includes all the operations you requested, and it's easy to add more functionality later if you need it.
- Python-friendly: The public properties and methods will be directly accessible via Blender's
clrimport system.
Once you build your DLL, you can load and use it in Blender like this:
import clr clr.AddReference(r"C:\Path\To\Your\BlenderVectorTools.dll") from BlenderVectorTools import Vector3 # Create sample vectors v1 = Vector3(1.0, 2.0, 3.0) v2 = Vector3(4.0, 5.0, 6.0) # Test operations sum_vec = v1 + v2 diff_vec = v1 - v2 dot_product = v1.Dot(v2) cross_product = v1.Cross(v2) print(f"Sum: {sum_vec}") print(f"Difference: {diff_vec}") print(f"Dot Product: {dot_product}") print(f"Cross Product: {cross_product}")
That should run without any issues! The key takeaway here is that for interop with non-WPF environments like Blender Python, stick to lightweight, self-contained .NET Standard code instead of framework-specific libraries like PresentationCore.
内容的提问来源于stack exchange,提问作者Walrfi

