[Closed] Can MAXScript take object attribute and add them together mathematically?
Sorry for the long title… Bit hard to describe in a few words.
I’ve been on a quest to work out if I can cost objects through 3D Max. It’s lead me to scripts.
My question: If I assign a custom attribute to two objects, is it possible to make a script to grab those attributes separately and add them together to get a total?
What I’m trying to achieve is building a scene with components and hopefully, when finished, run a script that gives me a cost estimation of the scene.
For example. I make a playground. Let’s say it includes the following;
8x posts
1x swing
2x decks
1x monkey bars
So I’m thinking that the object “post” would have an attribute of say, “1”. The number 1=$200.
So when I run the script, it sees that I have 8 number 1’s in the scene and it adds the value together to total 8. 8=$1600
So a swing could have a value of 2, decks a value of 3, etc etc.
Possibly another way would be to assign an object the actual cost price. So maybe post would have an attribute of 200. There’s 8 posts, 8×200=1600.
I’m not sure which was is easier or if it could be done. I would LOVE to know either way. It’s been a very frustrating experience trying to find the answers.
Thanks in advance,
Tim.
give ur object`s name a prefix .
in ur max script , set price for each prefix string . maybe a struct items with : prefix , price , comment … , these struct can be collect in a array .
do a loop , search for names corresponding the prefix_price , next is + and * …
Thanks for the reply hblan!
Well you’ve made me happy because it sounds like it can be done.
The only part I’m not happy about is now I have to learn how to code to make this script, haha! Doh!
I have hundreds of components/objects that will need to have a prefix attached to them then. Will be a big job I think but I need to do it. Will help out the company I work for and hopefully in turn, gives me a pay rise!
With this script, can you have it calculate the costings by selection only? So If I have a scene of 100 objects, but only select 20 to cost, can that be done as well? Seems like it would be useful to make a little UI pop out window for this. God, I’m just a lowly designer, not a programmer, lol!
so , u want a totally solution ? yes , I can make it . u want the job be free or pay me ? or let me join ur company
Im glad to be a freelance worker for ur comany . lol .
I have been coded for some guys who ask scripts with many thanks . everything can be simple or complex , u know , it is a little script at first ,then it will be bigger and bigger as somebody ask u again and again . that is Totally free , but my free time is limit .
as for now , I help others by show some ways that helpful for questions .
maybe u think it is valuable for u to do it , but how ? joke ?
i haven’t got your point. do you not know how to solve the task or 66 cents have to be paid your in advance?
the thing is not valuable for me. that’s true. it’s 5 lines of code and 1 minute of coding. there is nothing to challenge me, there is nothing that might help any other mxs developers. but i don’t see any reason for you to not practicing. even if it’s free…
but anyway…
fn whatdoesitcost =
(
local things = #("posts", "swing", "deck", "monkey bar")
local values = #(200, 250, 300, 400)
local cost = 0
for k=1 to things.count do cost += (execute ("$'" + things[k] + '*'")).count * values[k]
cost
)
sorry for late to know danis reply . the codes below mabe usful for Tim . As I think the process when u deal with ur problem .
u say u have many objects to select and rename , so , I give u a simple Tool, select a object , then press the top button to search for objects with same mesh face count , then u can filter by u self .
after selection , u can type a prefix in the edit space , then press add prefix or remove prefix , my rule : name do not contain a blank space , and prefix seperated with a “_” .
next is the caculate button .
below is a show area , let u check the price setting in the maxscript file .
normally , the price data should be in a Excel or xml files , or maybe in a Database , this way I should add codes with help of C# in maxscript . sorry for I dont have much spare time to do it .
and others I can thinkabout , a report , a email to client … others ,
————————-copy all codes into a txt file and save it as a name “name.ms” , then edit the price list array at the top of the script . Drag the ms file into ur max scene , let it works . all caculate by the prefix of the object name , simply , mainly on geometry .
rollout cgtalk_question "cgtalk_question"
(
-- variable define .
-------------------------------------------------------------------
-- mostly , ur data should be in a Excel or Xml file or Database .
-- we can load them when the script open with C# codes or update it manually .
-- but here , for simply , u can set the object and its Price ..etc here .
-------------------------------------------------------------------
struct ss ( iName , iPrice ) -- or u need : color , size , weight , comment etc .
local simpleData = #( \
( ss iName:"box" iPrice:10.0 ) , \
( ss iName:"box_01" iPrice:20.0 ) , \
( ss iName:"cyl" iPrice:20.0 ) , \
( ss iName:"sphere" iPrice:20.0 ) , \
( ss iName:"sphere_01" iPrice:20.0 ) \ -- note the last item has not a dot , !!
)
-------------------------------------------------------------------
local btWidth = 150
-- interface define .
button btSearchSameFaceNum "search objects with same face number " width:( 2 * btWidth )
edittext editPrefix "the prefix"
button btRemovePrefix " << remove prefix " width:btWidth pos:[0,editPrefix.pos.y + editPrefix.height]
button btAddPrefix " add prefix >> " width:btWidth pos:( btRemovePrefix.pos + [btWidth,0] )
label sep "----- total price of selections ------"
label labTotalPrice ""
button btCaculateTotalPrice "caculate total price" width:( 2 * btWidth )
button outputReport " output a detail report " width:( 2 * btWidth ) enabled:false
button otherFuctions "otherFuctions etc" width:( 2 * btWidth ) enabled:false
label labPriceInfo "" align:#left height:( ( simpleData.count + 3 ) * 12 )
-- function define .
function compItem a b = -- only for struct ss .
(/*{{{*/
local wLen = a.iName.count - b.iName.count
case of
(
( wLen < 0 ): 1
( wLen > 0 ): -1
default: 0
)
)/*}}}*/
function fPrefix iObjCollection iPrefix iAdd:true iFilterType:GeometryClass =
(
local foods = for i in iObjCollection where superclassof i == iFilterType collect i
if foods.count > 0 and iPrefix.count > 0 then
(
for i in 1 to foods.count do
(
if iAdd == true then
(
-- add prefix .
foods[i].name = iPrefix + "_" + foods[i].name
)else
(
-- remove prefix .
local hasPrefix = findString foods[i].name ( iPrefix + "_" )
if hasPrefix == 1 then
(
foods[i].name = replace foods[i].name 1 ( iPrefix.count + 1 ) ""
)
)
)
)
)
function fGetFaceNum geo =
(
addmodifier geo ( edit_mesh ())
local theR = meshop.getNumFaces geo
deleteModifier geo 1
return theR
)
-- action define .
on editPrefix changed val do editPrefix.text = trimLeft ( trimRight val )
on btSearchSameFaceNum pressed do
(
-- this is only a simple one . without enough filters .
if ( selection as array ).count == 1 do
(
if superclassof $ == GeometryClass do
try(
-- count mesh face number of select object .
local faceNum = fGetFaceNum $
-- init
max create mode
undo off
gc()
-- loop geometry for same face number .
for i in geometry do
(
if faceNum == ( fGetFaceNum i ) then selectmore i
)
)catch ()
)
)
on btAddPrefix pressed do fPrefix ( selection as array ) editPrefix.text
on btRemovePrefix pressed do fPrefix ( selection as array ) editPrefix.text iAdd:false
on btCaculateTotalPrice pressed do
(
local totalPrice = 0.0
local theSel = selection as array
if theSel.count < 1 then theSel = geometry as array -- maybe u have lights for caculate . :)
if theSel.count > 0 then
(
for i in theSel do
(
for j in simpleData do
(
local strSearch = findString i.name j.iName
if strSearch == 1 then
(
-- found one .
totalPrice += j.iPrice
exit -- break the simpleData loop .
)
) -- end for j in simpleData do .
) -- end for i in theSel
) -- end if theSel.count > 0
labTotalPrice.text = totalPrice as string
) -- end on btCaculateTotalPrice pressed .
-- sort the simpleData based on the length of the iName parameter .
-- let the longer be the prior one to caculate the price .
-- coz mostly , we use the type name as : box_type1_subtype1_1_1_3 .....
on cgtalk_question open do
(
-- show the price list .
local strShow = "the reference :
"
for i in simpleData do
(
strShow += "Name: " + i.iName + " Price : " + ( i.iPrice as string ) + "
"
)
labPriceInfo.text = strShow
-- sort the simpleData .
qsort simpleData compItem
)
)
createdialog cgtalk_question 300 500
Sorry for the late reply. For some reason, this website loads VERY slow from my work computer.
Thanks very much for the replies guys. This gives me something to work with and now I can at least try and make head or tail from it all.
Cheers.