Forum Groups
  All forums
    Help & Feedback
      Questions
      Work in progress
      Finished Art
      Non-Max related
    Community
      Offtopic
      News
    Hardware
    Photography


Featured Threads
  inspiration alert!!!
(36 replies)
  Indespensible MaxScripts, Plugins and 3rd Party Tools
(37 replies)
  The allmighty FREE Resources Thread !
(17 replies)
  spam alert!!!
(4886 replies)
  Maxforums member photo gallery index
(114 replies)
  Maxforums Member Tutorials
(89 replies)
  three cheers to maxforums...
(240 replies)
  101 Things you didnt know in Max...
(198 replies)
  A Face tutorial from MDB101 :D
(95 replies)
  Maxforums.org Members Gallery
(516 replies)
  SON OF POST YOURSELF
(637 replies)
  Dub's Maxscript Tutorial Index
(119 replies)

Maxunderground news unavailable

Maxscript, checking object exists
show user profile  reeves1984
Hi chaps, as says in the title, I just want to check if an object exists so I can create a new one or not!

I've only found object specific examples, (check if $box exists) where as I want to use a name associated with a variable, thats how I roll.


--
Simon Reeves


www.simonreeves.com - VFX Artist & Blog
twitter


www.analogstudio.co.uk <-- I work here

read 4142 times
11/3/2011 5:53:55 PM (last edit: 11/3/2011 5:53:55 PM)
show user profile  Dave
Hmm?

I got as far as selecting a box, and if no selection was made, then it made a box. But you want something moarz?

"I flew over Egypt once"

read 4131 times
11/3/2011 6:14:55 PM (last edit: 11/3/2011 6:14:55 PM)
show user profile  reeves1984
I don't want it related to any selection though, just if an object exists in the scene.


--
Simon Reeves


www.simonreeves.com - VFX Artist & Blog
twitter


www.analogstudio.co.uk <-- I work here

read 4123 times
11/3/2011 6:31:37 PM (last edit: 11/3/2011 6:31:37 PM)
show user profile  Garp
Is your variable holding the string that is the object's name?




read 4121 times
11/3/2011 6:38:02 PM (last edit: 11/3/2011 6:38:02 PM)
show user profile  Dave
if $box01 == undefined then
box()

??

I don't know, I am a giant n00b.

"I flew over Egypt once"

read 4118 times
11/3/2011 6:46:30 PM (last edit: 11/3/2011 6:46:30 PM)
show user profile  reeves1984
shhh Dave - no, really thats what I found, but its object specific, so its no good (and not good practice!)

yeah Garp thats what I mean - here is a made up example of what I want to do:


oObject = "socks"
oObjectExists = isValidObj oObject

if oObjectExists == false then
(
Dummy pos:[0,0,0] name:oPropName
)





--
Simon Reeves


www.simonreeves.com - VFX Artist & Blog
twitter


www.analogstudio.co.uk <-- I work here

read 4112 times
11/3/2011 6:51:17 PM (last edit: 11/3/2011 6:52:11 PM)
show user profile  Garp
This should work:

oObject = "socks"
execute ("if $" + oObject + "==undefined do dummy name:oObject")




read 4095 times
11/3/2011 7:29:52 PM (last edit: 11/3/2011 7:31:25 PM)
show user profile  reeves1984
Cheers garp!
Certainly seems messy though, having to use execute? I'll give it a go

--
Simon Reeves


www.simonreeves.com - VFX Artist & Blog
twitter


www.analogstudio.co.uk <-- I work here

read 4082 times
11/4/2011 11:05:14 AM (last edit: 11/4/2011 11:05:14 AM)
show user profile  Garp
The thing is you want to use a litteral name, but it's stored in a variable.
The alternative would be to read each object's name and compare it with the variable, which imo is even messier, and case sensitive. And less efficient.

You could minimize it with
execute("obj = $" + oObject)
then test obj.




read 4077 times
11/4/2011 2:47:16 PM (last edit: 11/4/2011 2:48:07 PM)
show user profile  reeves1984
Hmm yeah fair enough, case senstitive is okay

after checking how I've done it in python in xsi, I've just looped through the scene root's properties to see if it's in the list

(Im getting a property in xsi, in max Im making an object with custom properties)

--
Simon Reeves


www.simonreeves.com - VFX Artist & Blog
twitter


www.analogstudio.co.uk <-- I work here

read 4069 times
11/4/2011 3:09:24 PM (last edit: 11/4/2011 3:09:24 PM)
show user profile  reeves1984
See I prefer this complicated way, because it makes sense to me, where as the execute stuff is a mystery to me...

oPropName = "LemonSave"
oPropExists = False

for oObject in objects do
(
if oObject.name == oPropName do
(
oPropExists = True
)
)

if oPropExists == False do
(
oProp = Dummy pos:[0,0,0] name:oPropName
)

(how did you post code before btw? I saw you doing it before)

also thanks for your help garp :)

--
Simon Reeves


www.simonreeves.com - VFX Artist & Blog
twitter


www.analogstudio.co.uk <-- I work here

read 4064 times
11/4/2011 3:44:44 PM (last edit: 11/4/2011 3:45:24 PM)
show user profile  Garp
You're welcome :)

I find execute() pretty handy for this kind of thing. Its principle is simple. You just build the code as a string instead of typing it directly. So if you do
the_name = "MyBox"
execute("select $" + the_name)
MXS sees
select $MyBox

Also bear in mind that if you have many objects, your code would have to loop through all of them (or until it finds it). But if you prefer this way, I would rather make a function:


But this won't work with "LEMONSAVE", "lemonsave", "LeMoNsAvE", etc.

As for the code area, I've made a macro for it (just for MF). Makes it easy for people to copy/paste with the correct formatting.
You want it?

ps: [0,0,0] is the one position that you don't need to specify when creating an object, as it's the default ;)

edit: oops, there was a scope problem with oProp. It's corrected.



read 4061 times
11/4/2011 4:20:38 PM (last edit: 11/4/2011 4:44:56 PM)
show user profile  ccampbell
i may be miss understanding cause i'm kind of a noob but.. why not just

objVar = $objNameHere
if isvalidnode objVar then (expr) else ( newBox expr)

Good Luck!




$Entrepreneur = if((Designer + Engineer)*Programmer){Problem Solver};



read 4048 times
11/4/2011 8:25:43 PM (last edit: 11/4/2011 8:26:36 PM)
show user profile  Garp
Your code will look for an object whose name is "objNameHere", not the text string contained by the variable objNameHere.

Thanks for isValiNode() though. I forgot that one :)




read 4044 times
11/4/2011 8:41:00 PM (last edit: 11/4/2011 8:41:00 PM)
#Maxforums IRC
Open chat window


Support Maxforums.org