Send data from C# to Maxscript (Property/variable data)


#1

Thanks for trying


#2

It’s the expected behavior.
System.Single (float) is a Value Type, thus it defaults to its default value 0.0. It can never be ‘null’ (undefined).
System.String is a Refernce Type, thus its value is ‘null’ (undefined) if not assigned.
That’s the behavior in C# and that’s what you get.
If you need value types with the possibily of being ‘null’, you have to use the ‘?’ modifier.


#3

Thanks for trying


#4

You don’t have to do anything. Conversion is automatic from the main value types as floats, doubles or integers.
From strings too, and from arrays of these types too.


#5

Yes, I understand that. However, the data is not coming across in a usable format. “Undefined” doesn’t equal “This is a string”. It’s getting, extracting or whatever the data in Max the same as in C#. I’m not sure what I’m missing unless the data is there and that the Print function isn’t showing it.


#6

Sorry but I can’t understand your problem… because I can’t see any problem.


#7

The problem is that the data being pulled in from C# is Undefined. It should be “This is a string”. If I assign the data to the Text property of a textbox will it still show “This is a string” or will it error out as being Undefined?


#8

For example, the data for a given property, e.g. .PositionX is -39.52099 in the C# app. However it is always 0.0 in 3dsMax. I’m trying to get the actual float value for use in Maxscript to position an object.


#9

This is not right.
If you call a method of your class that returns a double (or an integer, or a string…) you’ll get in Max a double (or an integer, or a string…). For example, in MaxScript:

classObj = dotNetObject "MyNameSpace.Out2Max"
aaa = classObj.myDoubleMethod() --> you'll get a double value

If you call a method of your class that returns a reference object, for example an “Out2Max” instance, you’ll get an instance of “Out2Max” as a dotnet object, and you can read its public properties or fields without any problem, or call its methods without any problem.

classObj = dotNetObject "MyNameSpace.Out2Max"
aaaRef = classObj.myRefMethod() --> you'll get a dotnet object holding the reference object returned by this method.
X = aaaRef.PositionX --> you'll get the float value
Test = aaaRef.Test --> you'll get the string

If you allways get 0 for value types or undefined for strings is because you are returning void values, not because of Max behavior.


#10

Thanks for trying


#11

Could you give me a sample method that holds float and string variables inside it that are accessible to the sensor app?


#12
(
local source = "using System;
namespace mxs
{
  public class myclass
  { 
	private string _testString;
	public string GetTestString { get { return _testString; } set { _testString = value; } }
  }
}"

		compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"
		compilerParams.ReferencedAssemblies.Add("System.dll");
		compilerParams.ReferencedAssemblies.Add("System.IO.dll");
		compilerParams.GenerateInMemory = on
		csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
		compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #( source as string )

		if (compilerResults.Errors.Count > 0 ) then
		(
			local errs = stringstream ""
			for i = 0 to (compilerResults.Errors.Count-1) do
			(
				local err = compilerResults.Errors.Item[i]
				format "Error:% Line:% Column:% %\n" err.ErrorNumber err.Line err.Column err.ErrorText to:errs
			)
			format "%\n" errs
			undefined
		)
		else
		(
			compilerResults.CompiledAssembly.CreateInstance "mxs"			
		)
)
(
	x = dotNetObject "mxs.myclass"
	format "should return undefined >> %\n" x.GetTestString
	x.GetTestString = "test"
	format "should return test >> %\n" x.GetTestString
)

#13

Thanks Sergey!! I’m getting mad with this.

David, the only thing you can do is show both of your codes, C# and MaxScript, and we may find your problem. But the problem isn’t where you think it is.


#14

Thanks for trying


#15

Yes, this is what I imagined… Computers can do many things but not magic (at least to date).

On the ‘on TestOut2Max open do’ event you create an object instance of your class, a new one. Why the hell should it be the one you are feeding with your sensor app?
You must get the instance you are updating from the sensor app or, directly, feed the one you create with Max with your app.

P.S. 1: By the way, the loading assembly should be at the start of your script or create a global for it and check in the ‘open’ event if it is already loaded. You can load a dll only once.
P.S. 2: Programming is entertaining and fun, but it’s a professional job too. I do not know why there is a general idea of what anyone can do it with a few clever copy/paste.


#16

Thanks for trying


#17

What do you think you are doing when you write:

classObj = dotNetObject "MyNameSpace.Out2Max"