Community
3ds Max Programming
Welcome to Autodesk’s 3ds Max Forums. Share your knowledge, ask questions, and explore popular 3ds Max SDK, Maxscript and Python topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Passing variables into a tool that's in a function in another script...?

4 REPLIES 4
Reply
Message 1 of 5
redregon
443 Views, 4 Replies

Passing variables into a tool that's in a function in another script...?

I know that this might seem like a very elementary thing I'm asking about but I'm trying to put together a collection of tools for use with a bunch of scripts and I'm trying to get a "point chain" tool creator.

I've done tests and know where to take the script I need to use but getting the number of iterations for the tool into the tool itself is making me realize that I have much to learn.

Here is the script I am working on. What I've posted is a work in progress but should give you an idea what I mean. Also, what you see here is the script I'm interested in passing the values to. I know this is total noob territory but I've been having a tricky time finding the right articles in the help file that would cover something this... esoteric?

 

struct customTools
(
	fn ctPointChain pointTotal =
	(
		print pointTotal
		tool makePoints numPoints:3
		(
			local pt1, pt2, pt3
			local ptActive
			local viewPorts=#
			(
					#view_persp_user,
					#view_iso_user,
					#view_camera,
					#view_spot
			)
			on mousePoint clickNum do
			(
				case clickNum of
				(
					1:
					(
						pt1=point()
						pt1.pos = worldPoint
						pt2=point()
						ptActive = pt2
						lookAtCon=lookAt_Constraint()
						pt1.rotation.controller=lookAtCon
						lookAtCon.appendTarget pt2 100
						lookAtCon.lookAt_vector_length=100
						lookAtCon.viewline_length_abs = false
						
					)
					2:
					(
						pt3 = point()
						ptActive = pt3
						lookAtCon=lookAt_Constraint()
						pt2.rotation.controller=lookAtCon
						lookAtCon.appendTarget pt3 100
						lookAtCon.lookAt_vector_length=100
						lookAtCon.viewline_length_abs = false
					)
				)
			)
			on mouseMove moveIt do
			(
				ptActive.pos = worldPoint
			)
			on mouseAbort del do
			(
				
				delete pt1
				delete pt2
				
				--throws an error if the third point has yet to be defined.
				delete pt3
				
			)
		)
		startTool makePoints
	
	), --End Point Chain

 So, as you can see this is a somewhat functional script but as you can also see it's hard-wired for a certain number of clicks. I know how to push this in the direction to make it do a loop for the case (though I haven't incorporated it into this script... but the actual script itself is not why I'm asking.)

Basically... This script is called from a rollout by calling the function and I need to pass it an integer variable so that it knows how many links in the chain to make. The reason why this is in a separate script is because I suspect that this tool will get a good deal of use with other tools so having it stashed in a struct would save me having to type/copy-paste it every time and then tooling it to that particular use. At least this way I can keep it all in one place and call it as I need it...

I understand that this is probably an incredibly elementary thing however I'm trying to re-learn maxscripting (the saying IS true... "Use it or lose it." And for a long time I didn't use the information I initially learned.)

4 REPLIES 4
Message 2 of 5
redregon
in reply to: redregon

Allright, just to be sure that I'm describing this properly... I'm going to post some more code to help illustrate what I mean because I'm beginning to suspect that I may be going about this backwards or asking something that it might not want to do...

So, here's the code that defines the macroscript that accesses the struct that the tool that's posted above is in.

macroScript ctPointChain
	category:"Custom Tools"
(
	on execute do
	(
		ct=customTools()
		ct.ctPointChain()
	)	
)

 As you can see, it's pretty simple (and I did cobble this together from scripts I found around the web so this isn't my own unique way of making this.)

Also, as you can also see, there isn't anything in there as to how to declare the variable that I'd like to pass on to the ctPointChain() funciton.

 

The reason why I went about it like this is mainly because it is cobbled together from other scripts... However, for this, I've been wondering if given the utility of this function I want to create I'm likely going to need it for other things even outside of the autorig script I'm working on for it to use. (Also because I do like to challenge myself but I'm starting to wonder if I've bitten off more than I can realistically chew.)

So, the way that I hope to use it will be with something like this.

rollout RiggingTools "Rigging Tools"
(
	group "Master Control"
	(
		editText rigName "Name:" text:"" 
		spinner spnNumberLinks "# of Links:" range:[1,20,4] type:#integer 
		button btnCreateDummy "Begin creating Dummy"

	)
	on btnCreateDummy pressed do
	(
		linkNum = spnNumberLinks.value
		macros.run "Custom Tools" "ctPointChain"
	)
	
)
createDialog RiggingTools

 Is accessing them this way destined for disaster or is there a way to do it like this? I can see about testing while putting the tool definitions themselves in the autorigging panel script as is but that would mean that that function may be limited to use by that one only afaik... So, any thoughts? I'd surely appreciate any ideas and thoughts on this one.

Message 3 of 5
redregon
in reply to: redregon

Okay, so some more research is telling me that the way I had it set up initially may have been a bit too complicated for what I need...

So, I've dropped the separate macroscript integration and left the struct with the tool definitions on it's own and used it that way. This means that this tool may not be easily added to a button on the toolbar but that's acceptable (besides, with a toolbar button, unless I wanted to add in a dialog to ask for the number of iterations, it wouldn't know how to pass the data over anyway. so, I've cut out the extra step.)

That and I've learned that if I'm using subrollouts (which the final tool will use) the "on pressed do" code seems to need to be inside the definition of the subrollout's group itself. Makes sense in a way because even though the final tool that I'm going to make will have subrollouts, I did set up some "enable/disable" conditions for that one (that one is way too beefy to add here though and it's most definitely a work in progress... right now it's about 200-300 lines of code and that might be a bit much just for this little example.)

I'll post if I make progress on this and how I resolved it in case anyone else is facing something like this as well. (that and the "nevermind, got it" posts... well, I am sure I'm not the only one that gets a tad frustrated whenever I see them 😉 )

Message 4 of 5
redregon
in reply to: redregon

Okay, so after some finagling of the code I've been able to get it to go through somewhat okay... There's still a point where I'm a little worried that there will be a problem and that's about trying to feed a variable into the "numPoints" field in the Tool itself.

Here's the code for the tool... it expects an integer value to function and will present that number of points linked up.

struct customTools
(
	fn ctPointChain pointTotal =
	(
                pts=#()
		
		tool makePoints numPoints:7
		(
			local ptActive
			local total = pointTotal
			local viewPorts=#
			(
				#view_persp_user,
				#view_iso_user,
				#view_camera,
				#view_spot
			)
			on mousePoint clickNum do
			(
				if clickNum == 1 do
				(
					p = point size:2.0 centermarker:true axistripod:true cross:false box:true constantscreensize:false drawontop:false wirecolor:green
					append pts p
					pts[1].pos = worldPoint
					ptActive = pts[1]
					print clickNum
				)
				if clickNum >= 2 and clickNum <= total do 
				(
					p = point size:2.0 centermarker:true axistripod:true cross:false box:true constantscreensize:false drawontop:false wirecolor:green
					append pts p
					ptActive = pts[clickNum]
					lookAtCon=lookAt_Constraint()
					pts[clickNum-1].rotation.controller=lookAtCon
					lookAtCon.appendTarget pts[clickNum] 100
					lookAtCon.lookAt_vector_length=100
					lookAtCon.viewline_length_abs = false
					print clickNum
				)
				if clickNum > total do
				(
					print "Point Chain complete"
					#stop
				)
			)
			on mouseMove moveIt do
			(
				ptActive.pos = worldPoint
			)
			on mouseAbort del do
			(
					
			)
		)
		startTool makePoints
	
	), --End Point Chain
)

 To get around the inability (either because I am doing it wrong or because it doesn't like variables instead of the value) to limit the tool clicks to the value I'd want to use, I put in a higher than usual number and just told it to automatically stop... I suspect that that's going to mean that I may have a tricky time getting it to collect the array and format it (name, heirarchy, linking, etc) to send it back to the tool but at the least it's kinda funcitonal. That said, if you do know how to go about telling the tool to use a variable for the numPoints then I'd love to hear from you because that would mean I can be a little less "on edge" for when I go to the next stage of this tool's coding.

 

I also want (for cleanliness's sake) to make the points instanced but that's going to be part of the next stage.

Message 5 of 5
redregon
in reply to: redregon

Found a quick way to returning the point chain to the other function... Turning "pts=#()" into "global pts=#()" Now I just toss them in and send it back.

It does mean that a global variable being declared means I'll have to do some garbage-collection and cleanup but that shouldn't be too tricky afaik. Reason being is that if I'm using this variable declaration on the tool itself, if I create a longer chain and then a shorter chain, the array will still remember what was in there previously given that I used the "append" command. All in all that shouldn't be too hard to keep tabs on and clean up because a simple loop or function that will check that array variable for data in it and go through it to clear it out that's called after each individual button that's set up to use this tool should tidy it up.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report