Maxscript: String and integer mixdown to string?

Maxscript: String and integer mixdown to string?

Isaac_Zuniga
Advocate Advocate
3,188 Views
4 Replies
Message 1 of 5

Maxscript: String and integer mixdown to string?

Isaac_Zuniga
Advocate
Advocate

Hey all, I've taken a C++ programming class and loved it, so I thought about taking a shot at Maxscript, and wow is it hard.

 

My problem is this:

 

I simply want to mix a string and an integer and somehow turn them both into a string, so I can use that output string as an object's name. (Which is also broken as well.) Here's the code:

 

 

try (closeRolloutFloater RolloutFloaterTemplate) catch()

rollout RolloutTemplate "Test" -- Rollout title label
(
	edittext base_name
	spinner objnum "Number of objects" range:[10,100,10] type:#integer -- spinner stuff
	button button1 "Create" -- button stuff
		on button1 pressed do -- when button's pressed
	
	for i=1 to i=objnum.value do -- do this as many times as the spinner specifies
	(
		
	if base_name.text != "" do -- if the input text box isn't empty... do the bottom
		
	(	position = i*10 -- variable position tracks the loop number
	box pos:[position,0,0]-- every newly spawned box is moved over slightly
	base_name i as string = name -- *broken* input text and string become new object's name
	print name	-- prints the new name in the listener
		
	) -- ends loop for checking if the text box isn't empty
	
  )
  
)
	RolloutFloaterTemplate = newrolloutfloater "First window" 250 125 -- window dimensions
	addrollout RolloutTemplate RolloutFloaterTemplate -- rollout stuff

 

Can anyone tell me why it refuses to allow me to turn the integer into a string, letalone, allow the object to be renamed to what I'm trying to do?

 

By the way, I went around looking for the answer, and either I am totally sucking at this Maxscript language, or I overlooked something... 😕




Need help? Need to contact Autodesk? Click on the link below!

https://knowledge.autodesk.com/contact-support/technical-support
0 Likes
Accepted solutions (1)
3,189 Views
4 Replies
Replies (4)
Message 2 of 5

Swordslayer
Advisor
Advisor

First, what you're doing would be achieved much easier using prefix: and that way, you'd also avoid having duplicate object names:

 

try (closeRolloutFloater rolloutFloaterTemplate) catch()

rollout RolloutTemplate "Test" -- Rollout title label
(
	editText baseName
	spinner objNum "Number of objects" range:[1, 1e9, 10] type:#integer
	button btnCreate "Create" -- button stuff

	on btnCreate pressed do
		if baseName.text != "" do
			for i = 1 to objNum.value do
				Box prefix:baseName.text pos:[i * 10, 0, 0]
  
)
rolloutFloaterTemplate = newRolloutFloater "First window" 250 125
addRollout rolloutTemplate rolloutFloaterTemplate

 

Second, for loop syntax is not for i=1 to i=N... but for i=1 to N or for i in collection (plus optional while and where).

Third, the line base_name i as string = name is your biggest problem (and nope, C++ doesn't work that way either), just as most C-like languages, variable assignment takes the form variable = value, not vice versa. In your case, that would be obj.name = baseName.text + i as string (provided that you previously saved the created object in the obj variable).

Other than that, there are some minor details that are not mistakes per se but might lead to errors or confusion further on. And by that I mean the whole on button1 pressed indentation (plus while you can omit brackets like this, given that you are beginner, I wouldn't do that). Also, stick to one naming convention, doesn't matter if you choose to use under_scores or camelCase, just pick one and get in the habit of naming stuff as you write code - so no more button1's.

Oh, and even though it doesn't really make a difference performance-wise here, I've hoisted the if-test outside the for-loop - once again a good habit, when the condition you're checking for doesn't change.

Good luck and have fun Smiley Happy

0 Likes
Message 3 of 5

Isaac_Zuniga
Advocate
Advocate

Thank you for helping me with that, I'd only like to ask a few more things from you:

 

1. Can you direct me to a page of all the syntaxes and how they work? I tried the help documentation but that doesn't help me at all, and here's why:

 

I followed an example to see how it'd work out, and it doesn't work at all, here's the code:

 

 macroScript RenameThem category: "HowTo"
 (
	 rollout rename_rollout "Enter New Base Name"
	 (
		 edittext base_name ""
		 button rename_them "RENAME SELECTED OBJECTS..."
		 on rename_them pressed do
		 (
			 if base_name.text != "" do
			 for i in selection do i.name = uniquename base_name.text
		 )
	 )
	 createDialog rename_rollout 250 50
 )

I tried may things to get it to work, but it's as if Maxscript is completely ignoring the code, because I only get a print in the listener, and the number is "27598". I have no idea what that means, but it doesn't do what the tutorial or the script is supposed to do.

 

2. I'm currently trying to create an object outside of the loop, a basic rectangle (I do not need 10 rectangles), but regardless of where I place it's creation code, it refuses to work as a whole and throws me an error, here's the code for that:

 

try (closeRolloutFloater rolloutFloaterTemplate) catch()

rollout RolloutTemplate "Controls" -- Rollout title label
(
	editText rootName "Root name"
	spinner objNum "Number of sliders" range:[1, 100, 10] type:#integer width: 70
	button btnCreate "Create" -- button stuff

	on btnCreate pressed do
		
		rectangle()	
				
		if rootName.text != "" do
		for i = 1 to objNum.value do
			Box prefix:rootName.text pos:[i * 10, 0, 0] width: 2 height: 4
		
)
rolloutFloaterTemplate = newRolloutFloater "First window" 250 125
addRollout rolloutTemplate rolloutFloaterTemplate

I was trying to get it to work without coming to you for more help, but it doesn't seem Maxscript likes me, given reason #1 and this reason itself.

 

3. Is it possible I could have done something to 3ds Max that caused it to not understand Maxscript correctly? I mean, reason #1 shows that came from the help file itself, and 3ds Max <B>did</B> run it, but it only did it a few times and I've never gotten the script to execute ever again. Maybe I pressed a keystroke or I corrupted a system file for 3ds Max by doing something?




Need help? Need to contact Autodesk? Click on the link below!

https://knowledge.autodesk.com/contact-support/technical-support
0 Likes
Message 4 of 5

Swordslayer
Advisor
Advisor
Accepted solution

1. the maxscript reference, it's structured like a book, read all the beginners, syntax and howto chapters. Then you will know what the macroScripts are used for (hint: it's about making stuff accessible from the main UI without running scripts every time). Take your time to work through it systematically - and I mean it, take a week to do that starting at the very start and not in some random places. There's also a MAXScript 101 series from the MAXScript creator John Wainwright himself, very much recommended.

2. if you had said you took python class, I could imagine you'd expect this to work, but C++? Parentheses are used for a reason and I've told you before that being a beginner, you should use them even for a one-line codeblocks - and multi-line codeblocks simply won't work as you expect when you omit them.

3. see 1., you have wrong expectations, it works as designed

0 Likes
Message 5 of 5

Isaac_Zuniga
Advocate
Advocate

Sorry for the delay in my response:

 

Thank you for giving me the link to those videos, they helped me a lot/that was exactly a tutorial that I was looking for.




Need help? Need to contact Autodesk? Click on the link below!

https://knowledge.autodesk.com/contact-support/technical-support
0 Likes