Notifications
Clear all

[Closed] [c#] RegisterViewportDisplayCallback problem

Getting my feet wet with some c# but I’m stuck… I think I know what the problem is but have no clue on how to fix it.

edit, long story short:

How do I do a ‘myVC = new myViewportCallback();’ to solve the ‘Runtime error: dotNet runtime exception: Object reference not set to an instance of an object.’ that the code below generates:


	source="
	
	using System;
	using Autodesk.Max;
	using Autodesk.Max.Plugins;
	
	class myViewportCallback : Autodesk.Max.Plugins.RedrawViewsCallback
	{
	 
		public override void Proc(IInterface ip)
		{
			// do stuff here
		}
	}
	
"
  
csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"

compilerParams.ReferencedAssemblies.AddRange #("System.dll",@"C:\Program Files\Autodesk\3ds Max 2014\Autodesk.max.dll")

compilerParams.GenerateInMemory = on
compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
 
assembly = compilerResults.CompiledAssembly
assembly.CreateInstance "myViewportCallback"

5 Replies

Trying using the (dotnetclass “Activator”).CreateInstance… still no luck…


	 source="
	using System;
	using Autodesk.Max;
	using Autodesk.Max.Plugins;
	
	public class MyViewportCallback : Autodesk.Max.Plugins.RedrawViewsCallback
	{
		 
		public override void Proc(IInterface ip)
		{
			// do stuff here
		}
	}
	
	"
		
 
csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"

MethodInfo = dotnetobject "System.CodeDom.Compiler.CompilerParameters"

compilerParams.ReferencedAssemblies.AddRange #("System.dll",@"C:\Program Files\Autodesk\3ds Max 2015\Autodesk.max.dll")

compilerParams.GenerateInMemory = on
compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
 
assembly = compilerResults.CompiledAssembly

mType =   assembly.GetType("MyViewportCallback")

(dotnetclass "Activator").CreateInstance  mType
 lo1

Is it just the instantiation from maxscript that’s throwing the exception? You can check by adding a static method in the c# class that instantiates and returns the constructed object.
If that also throws, get a stack trace and see who’s to blame.

Thx, I got a bit further, it all goes well until this:

	test =  MyVC.get() 

It’s throws a “– Runtime error: dotNet runtime exception: Object reference not set to an instance of an object.”

The MyViewportCallback class itself works fine in C#, when I rewrite the GetMyViewportCallback to just return the GetMyViewportCallback’s ‘Foreground’ it return a true/false to MXS.

Any clues? And how can I get a stacktrack ?

source="
 
 using System;
 using Autodesk.Max;
 using Autodesk.Max.Plugins;
   
 public class MyViewportCallback : ViewportDisplayCallback 
 {
 	
 	public  MyViewportCallback()  
   {
 		
   }
 	 
 	public override void Display (int t, Autodesk.Max.IViewExp  vpt, int flag )
   {
    
   }
    
 	public override  void GetViewportRect(int t, Autodesk.Max.IViewExp  vpt,  Autodesk.Max.IBox2 rect)
   {
   
 	} 
    
 	  public override bool Foreground {
 		get {
 			return true;
 		}
 	}
 	 
 };
 
 public class GetMyViewportCallback  
 {
 	 
 	
 	public  GetMyViewportCallback() 
   {
 	 
   }
   
   
 	public  MyViewportCallback  Get() 
 	 {
 	 var MyCB=new MyViewportCallback() ;   
 		return MyCB  ;
   }
 	
 	 
 };
 
 
 	
 "
 		
  
 csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
 compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"
 compilerParams.CompilerOptions = " /unsafe " ;
 
 MethodInfo = dotnetobject "System.CodeDom.Compiler.CompilerParameters"
 compilerParams.ReferencedAssemblies.AddRange #("System.dll",@"C:\Program Files\Autodesk\3ds Max 2015\Autodesk.max.dll")
 compilerParams.GenerateInMemory = on
 compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
  
 if (compilerResults.Errors.Count > 0 ) then 
   ( 
 		  err = compilerResults.Errors.Item[0] 
 		  print err.ErrorText  
 ) else
 (
 
 	--create c# handler		
 	assembly = compilerResults.CompiledAssembly
 	dotNetArg=dotNet.ValueToDotNetObject #(1) (dotNetObject "System.Object")
 	mType =   assembly.GetType("GetMyViewportCallback")
 	activator = dotNetClass "System.Activator"		
  
 	
 	MyVC=assembly.CreateInstance  "GetMyViewportCallback"
 	
 	test=	MyVC.get() 
 	 
 	-- register handler	
 	--assembly = dotnet.loadAssembly "Autodesk.Max"  
 	--g = (dotnetClass "Autodesk.Max.GlobalInterface").Instance  
 	--inface = g.CoreInterface  
 	--inface.RegisterViewportDisplayCallback  (dotNetObject "System.Boolean" true)  MyVC
 		
 		
 )
 
1 Reply
 lo1
(@lo1)
Joined: 2 years ago

Posts: 0

This sounds like an internal maxscript issue then. To get a stack trace you need to catch the exception on the c# side. Since there is no exception on the c# side that doesn’t help you much.

in my mxs DLL manager I instantiate classes using System.Activator and passing Type as a dot net object.

what happens if you replace


MyVC=assembly.CreateInstance  "GetMyViewportCallback"

with


MyVC=activator.CreateInstance  mType

?