Maxscript listener assistance

Maxscript listener assistance

Anonymous
Not applicable
4,694 Views
22 Replies
Message 1 of 23

Maxscript listener assistance

Anonymous
Not applicable

I am new at maxscript and always have wanted to use it but don't know how.  Now I have a redundant application that I would like to try to use maxscript listener to do it. or write a script. (if I knew how)

 

Here is what i would like to do.

 

1. select an object

2. quadify selection (listener does not pick this up)

3. uvw map, box, uncheck Real-World Map size

4. assign material map called "stainless"

5. end

 

Can someone help me with this?

thanks

 

 

0 Likes
4,695 Views
22 Replies
Replies (22)
Message 2 of 23

drew_avis
Autodesk
Autodesk

Hi there, that's great that you're learing MAXScript.  Not everything gets picked up by the Macrorecorder, so you have to start digging through the MAXScript help to figure out what's possible and how to do it.  For your script, see if this gets you started:

 

 

t = teapot()
convertto t editable_poly
select t -- select the node
q = Quadify_Mesh() -- create a Quadrify modifier
addModifier t q -- add the modifier to our node
u = UVWMap() -- create a UVWMap modifier
u.realWorldMapSize = false -- same as unchecking "Real World Map Size" checkbox
addmodifier t u -- add it to the node
mat = scenematerials["stainless"] -- find the scene material named "stainless"
-- note: this must exist on an item in the scene
t.material=mat -- apply the material to the node

 

My script creates a node and selects it, yours would start with something like:

myNode = $

Where $ gets the selected node.

 

Hope that helps,

Drew

 



Drew Avis
Content Experience Designer
Message 3 of 23

Anonymous
Not applicable

Thanks @drew_avis I tried to run your script just as a starting point but I get MAXScrip FileIn Exception error

--NO ""convertTo""function for unefined

I even commented the line # 2 "convertto t editable_poly" out and is still get the same error.

 

 

0 Likes
Message 4 of 23

Anonymous
Not applicable

delay my last I think I am just starting to talk to it lol

 

0 Likes
Message 5 of 23

Anonymous
Not applicable

myNode = $
--convertto $ editable_poly
select $ -- select the node
q = Quadify_Mesh() -- create a Quadrify modifier
addModifier t q -- add the modifier to our node
u = UVWMap() -- create a UVWMap modifier
u.realWorldMapSize = false -- same as unchecking "Real World Map Size" checkbox
addmodifier t u -- add it to the node
mat = scenematerials["stainless"] -- find the scene material named "stainless"
-- note: this must exist on an item in the scene
t.material=mat -- apply the material to the node

I am getting this...

undefined
-- Error occurred in anonymous codeblock; filename: S:\APEC COMPANY DOCUMENT SHARED FOLDER\Engineering\Mechanical Engineering\Assembly & Maintenance instruction\Standard Equipment\Coater\MC-70T\downloads\quad&assignmat.ms; position: 69; line: 3
-- No ""select"" function for undefined
-- MAXScript callstack:
-- thread data: threadID:11424
-- ------------------------------------------------------
-- [stack level: 0]
-- In top-level
Quadify_Mesh:Quadify Mesh
-- Error occurred in anonymous codeblock; filename: S:\APEC COMPANY DOCUMENT SHARED FOLDER\Engineering\Mechanical Engineering\Assembly & Maintenance instruction\Standard Equipment\Coater\MC-70T\downloads\quad&assignmat.ms; position: 168; line: 5
-- No ""addModifier"" function for undefined
-- MAXScript callstack:
-- thread data: threadID:11424
-- ------------------------------------------------------
-- [stack level: 0]
-- In top-level
Uvwmap:UVW Map
false
-- Error occurred in anonymous codeblock; filename: S:\APEC COMPANY DOCUMENT SHARED FOLDER\Engineering\Mechanical Engineering\Assembly & Maintenance instruction\Standard Equipment\Coater\MC-70T\downloads\quad&assignmat.ms; position: 330; line: 8
-- No ""addModifier"" function for undefined
-- MAXScript callstack:
-- thread data: threadID:11424
-- ------------------------------------------------------
-- [stack level: 0]
-- In top-level
stainless:Standard
-- Error occurred in anonymous codeblock; filename: S:\APEC COMPANY DOCUMENT SHARED FOLDER\Engineering\Mechanical Engineering\Assembly & Maintenance instruction\Standard Equipment\Coater\MC-70T\downloads\quad&assignmat.ms; position: 509; line: 11
-- Unknown property: "material" in undefined
-- MAXScript callstack:
-- thread data: threadID:11424
-- ------------------------------------------------------
-- [stack level: 0]
-- In top-level

0 Likes
Message 6 of 23

Anonymous
Not applicable

this is what i want to replace with a script.

0 Likes
Message 7 of 23

Anonymous
Not applicable

I hope you read the latest first. My previous post on this topic can be ignored.

Here is where i am at @drew_avis

Using your program other then the first couple lines. I have managed to get to this point. (please see attached)

I have a couple questions at this point;

1. What would be the syntax to prompt me for a selection then equal that select to "T"?

I have tried to put in your suggested myNode = $ and the next line saying t = $ but that don't work.

2. I noticed that it does not select "box" nor does it un-select "Real-world map size"  in the uvw map

 

 

Thanks this is really helping me!

 

0 Likes
Message 8 of 23

drew_avis
Autodesk
Autodesk

Hi there, so I think you're getting close.  I think you have 2 options for dealing with the selection:

 

1) Assume that an object is selected before you run the script.  You can verify this when you get the node, and exit with a message if you don't have something selected.  This would look something like:

 

if selection.count != 1 then (print "Please select one object and run again.")
else (
myNode = $
-- etc

)

2) Attach a callback that detects when nodes are selected and runs when only one is selected.  You are getting into more advanced territory here.  This would look something like:

fn doSomeStuff = 
(
	if selection.count == 1 then (
		myNode = $
		print myNode
		callbacks.removeScripts #selectionSetChanged id:#doStuff
                -- now do some stuff
	)
	else print "Select one object please"
)

callbacks.addScript #selectionSetChanged "doSomeStuff()" id:#doStuff

Let me have a look at why the UVW map options aren't being set.

 

Drew

 



Drew Avis
Content Experience Designer
0 Likes
Message 9 of 23

drew_avis
Autodesk
Autodesk

Huh, I can't reproduce what you're seeing with the UVW Map modifier.  The mapping isn't box because we don't set it in the script.  In this version I set it, and it looks ok.  Are you seeing any errors in your listener when you run this script?

 

t = box()
convertto t editable_poly
select t -- select the node
q = Quadify_Mesh() -- create a Quadrify modifier
addModifier t q -- add the modifier to our node
u = UVWMap() -- create a UVWMap modifier
u.realWorldMapSize = false -- same as unchecking "Real World Map Size" checkbox
u.maptype = 4 -- set to box
addmodifier t u -- add it to the node
mat = scenematerials["stainless"] -- find the scene material named "stainless"
-- note: this must exist on an item in the scene
t.material=mat -- apply the material to the node

Drew



Drew Avis
Content Experience Designer
0 Likes
Message 10 of 23

Anonymous
Not applicable

would it be to much trouble for you to tell me in English what each line is doing?

For instance I look at it and I wonder what the ! is for?

Next question myNode = $

Is the selection assigned the tag $ as its variable name? Can I then tell max that T = $

 

is two == a typo? if not how would you say that phrase in English?

 

 

 
if selection.count == 1 then

0 Likes
Message 11 of 23

Anonymous
Not applicable

 

no errors. it it checked the box this time. It did not un-check real-world map size however.

$Box:Box002 @ [0.000000,0.000000,0.000000]
$Editable_Poly:Box002 @ [0.000000,0.000000,0.000000]
OK
Quadify_Mesh:Quadify Mesh
OK
Uvwmap:UVW Map
false
4
OK
stainless:Standard
stainless:Standard

 

real-world.jpg

0 Likes
Message 12 of 23

drew_avis
Autodesk
Autodesk

So the != and == and = can be confusing when you're first getting into scripting / programming.  

 

! is not, so != is "not equal".  Similarly, == is "is equal".  Finally a single = is assignment, so x="hi" assigns the string "hi" to variable x.

 

In MAXScript, $ will hold the currently selected node or nodes.  You can check how many things are selected by looking at selection.count.  In our example, we only run if selection.count is 1, otherwise we print a message.

 

Once you pick up little things like this, the learning curve gets less steep.  BTW these concepts are not just for MAXScript, most scripting languages have similar syntax.

 

Hope this helps,

Drew



Drew Avis
Content Experience Designer
0 Likes
Message 13 of 23

Anonymous
Not applicable

awesome! this is great info.

Now let me ask you this. Is there a quick reference sheet somewhere that looks something like this on all of the commands? Or maybe someone may have made one?

This would be extremely helpful. If I go to the scriptscript help section of max and type "if" i get nothing.

 

    example
= equal x="on"
! is not  
!= in not equal x!="on"
== is equal  

 

Looking on Youtube if found this. Attached is an example of what I am looking for. Should this be a new idea suggestion to AutoDesk or has someone already suggested this?

 

0 Likes
Message 14 of 23

istan
Advisor
Advisor

> BTW these concepts are not just for MAXScript, most scripting languages have similar syntax.

 

not only scripting languages.. luckily C/C++ uses the same syntax.. only basic/vbs is totally different, which is driving me crazy sometimes..

 

but MXS implemented only half of C++ as it is not supporting && and || 

0 Likes
Message 15 of 23

Anonymous
Not applicable

Thanks @istan. I am starting to discover that. Too bad I don't already know some of the other languages. Smiley Wink

 

0 Likes
Message 16 of 23

Siger_
Enthusiast
Enthusiast

For logical expressions MXS uses AND OR NOT

Comparison expressions: ==  !=  >   <   >=  <=

 

Much less than C language group, but still something 🙂

0 Likes
Message 17 of 23

istan
Advisor
Advisor

Well, for a C++ programmer since decades, such language "anomalies" always waste my time.

 

Such an enhancement of MXS would be more than simple, but AD has absolutely no focus on this anymore. I am also waiting for a MXS debugger since decades. Even C++ development is smarter by using the "Edit/Continue" feature in a debug session.

0 Likes
Message 18 of 23

drew_avis
Autodesk
Autodesk

Hi, we don't really have a "quick reference" for MAXScript, but you might find interesting the "Learning MAXScript" section, which can be read straight through as an introductory tutorial, and will help you get started:

 

http://help.autodesk.com/view/3DSMAX/2018/ENU/?guid=__files_GUID_4C14F474_CD23_4001_93DF_0F0F9A6025C...

 

Drew



Drew Avis
Content Experience Designer
0 Likes
Message 19 of 23

Anonymous
Not applicable

Can someone please tell me what the script syntax would be for this? Nothing shows up in MaxScript listener. Also it gives me lower poly count then quadrify mesh will.

 

 

QUADRIFY ALL.jpg

0 Likes
Message 20 of 23

Anonymous
Not applicable

ok I have everything working except the quadrify all

 

macros.run "Modifier Stack" "Convert_to_Poly"
$.material = meditMaterials[4]
modPanel.addModToSelection (Uvwmap ()) ui:on
$.modifiers[#UVW_Map].maptype = 4
$.modifiers[#UVW_Map].utile = 1
$.modifiers[#UVW_Map].vtile = 1
$.modifiers[#UVW_Map].length = 1
$.modifiers[#UVW_Map].width = 1
$.modifiers[#UVW_Map].height = 1
$.modifiers[#UVW_Map].realWorldMapSize = off
$.modifiers[#UVW_Map].utile = 1
$.modifiers[#UVW_Map].vtile = 1
$.modifiers[#UVW_Map].wtile = 1
$.modifiers[#UVW_Map].length = 23.495
$.modifiers[#UVW_Map].width = 22.86
$.modifiers[#UVW_Map].height = 22.86

 

I found a script that i believe may work. The function is called quadify not quadrify but here it is. now i just need help on how plug it in with mine.

It kind of blows me away to think i need all of this..

 

 

fn QuadifyAll obj uv:cb_uv.checked mtl:cb_mtl.checked fID:cb_fid.checked oID:cb_oid.checked =
(
local idList = #(), facesList = #()
local tnt = Bomb strength:0 gravity:0 chaos:0 seed:0 spin:0 detonation:0f minFragmentSize:2 \
maxFragmentSize:2 fallOffEnable:off isHidden:on isSelected:off
bindSpaceWarp obj tnt ; sliderTime = 5f
local objTemp = snapShot obj
delete #(tnt, obj)
if fID do
(
for i in 1 to objTemp.Faces.count do appendIfUnique idList (gfm objTemp i) ; sort idList
if idList.count > 1 do (for i = 1 to idList.count do append facesList #())
if facesList.count != 0 do
(
for i = 1 to objTemp.Faces.count do
(
idx = finditem idList (gfm objTemp i)
append facesList[idx] i
)
)
)
meshop.autoEdge objTemp #{1..(objTemp.Edges.count)} 24 type:#SetClear
meshop.weldVertsByThreshold objTemp #{1..(objTemp.Verts.count)} 0.001
if idList.count != 0 do
(
for i = 1 to facesList.count do
(
for j = 1 to facesList[i].count do sfm objTemp facesList[i][j] idList[i]
)
free facesList ; free idList
)
if mtl then objTemp.material = objMat else objTemp.material = undefined
if oID do objTemp.gbufferchannel = objID
select objTemp ; objTemp
)
on pb_PickObj picked p_obj do
(
if p_obj != undefined do
(
CenterPivot p_obj ; ResetXForm p_obj
convertToMesh p_obj ; select p_obj
pb_PickObj.text = p_obj.name
objMat = p_obj.material
objID = p_obj.gbufferchannel
pb_doit.enabled = on
)
)
on pb_doit pressed do
(
if isValidNode (pickedObj = getNodeByName pb_PickObj.text) do
(
if getCommandPanelTaskMode() != #create do max create mode
undo off with redraw off
(
if cb_uv.checked then
(
addmodifier pickedObj (Unwrap_UVW ())
objTemp = QuadifyAll (snapShot pickedObj)
swap pickedObj.baseobject objTemp.baseobject ; delete objTemp
select pickedObj
)
else
(
objTemp = QuadifyAll pickedObj
objTemp.name = pb_PickObj.text
)
if rb_pm.state == 1 then convertToPoly selection[1] else convertToMesh selection[1]
)
CenterObject pickedObj
pb_PickObj.text = "Pick Object"
pb_doit.enabled = off
)
)
on quadAllRoll close do (gc())
)
createDialog quadAllRoll 150 146 style:#(#style_titlebar, #style_sysmenu, #style_toolwindow)

 

0 Likes