Groups and single objects Q

Groups and single objects Q

Anonymous
Not applicable
778 Views
11 Replies
Message 1 of 12

Groups and single objects Q

Anonymous
Not applicable
Hi

How can i loop over my scene objects and print names with the following conditions:

1.If its a single object, just print the object name
2. If its a group, only print the the "top group" name (not groups within groups or the
member names)


When I run a loop **- for o in objects do print o.name -** I get single object as
well as groupmembers. And when I loop over the helpers where isGroupHead, I get
the groups (and the groups inside groups....)


Any help would be greatly appreciated 🙂



BR
Aplomo
0 Likes
779 Views
11 Replies
Replies (11)
Message 2 of 12

Anonymous
Not applicable

for obj in objects do
if not isGroupMember obj then
print obj.name


Works for me!

-Johan
0 Likes
Message 3 of 12

Anonymous
Not applicable
Guess it did 🙂 Thank you !

I'm trying to eliminate printing out Helpers, but Since groups are
part of the helper superclass I'm not sure how to write the expr.

This is what I've written so far. Maybe someone can help me shorten the
script or point out my mistakes.

Another thing; If I want to output the obj.names to a txtfile and then open the
createfiledialog. Do I have to write a fn to hold the result of the objectloop?


on exec_btn pressed do
(
for obj in objects where
superclassof obj != shape and -- Checks to see if the obj is of the superclass shape
superclassof obj != camera and -- Checks to see if the obj is of the superclass camera
superclassof obj != light and -- Checks to see if the obj is of the superclass light
classof obj != TargetObject and -- To avoid listing targetobjects

not isGroupMember obj do -- Makes sure the Groupmembers are not included
(

print (("\n")+"The Object named: "+(obj.name)+("is of superclass: ")+ (superclassof obj as string))

)
)



BR
Aplomo
0 Likes
Message 4 of 12

Anonymous
Not applicable
Tried to make a function to hold the loop, like this:


function do_default =
(
for obj in objects where
superclassof obj != shape and
superclassof obj != camera and
superclassof obj != light and
classof obj != TargetObject and
not isGroupMember obj do print (obj.name+("\n"))
)


Since I want to have checkboxes to include/exclude cameras/helpers/Shapes etc.

Is this an ok way to do it?

BR
0 Likes
Message 5 of 12

Anonymous
Not applicable
That's no problem, you could also make a struct and use this function in the struct. That keeps the code nice and clean.... you can then use the script in stdscripts and call your struct function everytime you need it. But you'll have to pass some parameters to the function to dis/enable the options you want included.


struct mystruct (
fn do_default options = (etc),
fn another_function option1 option2 = (etc2),
etc
)


call the function


mystruct.do_default options -- pass the options to the fn


You see that you can group functions... almost the same as a class/obj in c++ or whatever OOP language. Look it up in the reference lots of good stuff on that.

-Johan
0 Likes
Message 6 of 12

Anonymous
Not applicable
Thanks!

I feel my codebits are very inefficient and needlessly long.
Check out my script to see.

(Combos between the checkboxes (other that lights and cameras) and Output to txt isn't implemented yet)
0 Likes
Message 7 of 12

Anonymous
Not applicable
Yes I think it's not very efficient this way 😉
Make 1 function and pass the "filters" as array and check in the function against the set filters. Going to sleep right now... maybe I can put something up tomorow, but read up on passing variables to function in the mxs help! You can easily pass the states of the checkboxes directly to the function on exec_btn pressed, and sort it out in the function!

-Johan
0 Likes
Message 8 of 12

Anonymous
Not applicable
Passing variables to the function........puh!...
For a guy who started learning MAXScript 3 weeks ago,
that is quite a task 🙂

Is this more efficient?

global theClasses =#(superclassof,classof)
global theFilter =#(shape,camera,light,TargetObject)
global ArrItemclass = 0
global ArrItemFilter = 0

function userChose ArrItemclass ArrItemFilter =
(
for o in objects where theClasses o != theFilter
do print ((o.name as string)+("\n"))
)

on exec_btn pressed do
(
userChose 2 4
)


BR,
Aplomo
0 Likes
Message 9 of 12

Anonymous
Not applicable
I've tried several solutions.
I feel this one will be the easiest to modify:


global MyCams = for cams in objects where superClassof cams == camera collect cams.name
global MyLights = for ls in objects where superClassof ls == light collect ls.name
global MyShapes = for shps in objects where superClassof shps == shape collect shps.name
global MyDef = for obj in objects where superClassOf obj != light and superClassOf obj != camera and
superClassOf obj != shape and classOf != TargetObject collect obj.name
global Nums = 0

function userChose Nums =
(
if Nums == 1 do
(
print MyCams as string
)
)



I'm struggling with updating the objectprinting to include newly added objects. How can I have
my array update(live) and collect at runtime ?

Any help would certainly be welcome 🙂

BR

Aplomo
0 Likes
Message 10 of 12

Anonymous
Not applicable
Johan (Or anyone .. : )

Could you, if you find the time, explain how to pass variables from i.e. a checkbox state that affects the
outcome of a function, and how I can use it with the struct?

I'm having trouble finding good examples on how to do this 🙂



Thanks

Aplomo
0 Likes
Message 11 of 12

Anonymous
Not applicable
There's different ways of doing these things. Really depends on how you build your tool. You can use the struct to build the UI from it. In that case you define the struct as global variable and you can access it anywhere.


global myStruct
struct myStruct (
fn doMyCalculations var1 = (),
fn buildMyRollout = (
rollout "name" etc
(
button bn1 "name" etc
checkbutton ch1 "name"
)
on button pressed do mystruct.doMyCalculations ch1.state
),
)


That's only a principle description of how I do it, if you want to see an example of how I used it download: http://scripts.subd.nl/?f=JHN_userPropBuffer.ms or http://scripts.subd.nl/?f=JHN_FusionSplines.ms They both have the struct way of building a tool. Also interresting is to download some of Neil Blevins scripts on scriptspot and pick them apart.. that's how I got coding this way. Really the best way to learn is too pick other people scripts apart!

-Johan
0 Likes
Message 12 of 12

Anonymous
Not applicable
Thanks Johan!

I've written small 'exporter' script and tried to implement some global variables
to a checkbutton. (Save a maxfile and set the project folder before running the script.)

You must specify an object format : ie .obj in the textfield.

Any ideas on how to open the export dialog for the specified object ? (now , since i use the nopromt,
the latest settings will apply.)


-- Author: Thomas Pedersen--
-- webmaster@aplomo.com --
----------------------------

macroscript XpressExport category:"TT_Tools"
(

global updown = false
global executer


rollout XPX "XPress Exporter" width:160 height:168
(
button exec_btn "Export" pos: width:72 height:24 enabled:false
button cls "Close" pos: width:72 height:24
edittext navnet " Format: " pos: width:144 height:16
checkbutton chk_sel "Optional : Make Selection...." pos: width:144 height:24
button btn_opnexport "Open exportsettings" pos: width:144 height:24
groupBox grp_info "Info" pos: width:144 height:40
label lbl_info "Write your preferred format:" pos: width:128 height:16


fn executer =
(
if chk_sel.state == true then
(
for o in (selection as array) where classof o != TargetObject and
superclassof o != light and superclassof o != camera and
superclassof o != shape and not isGroupMember o do
(
select o
f = o.name+navnet.text
exportfile f #noprompt selectedonly:true
)
messagebox("Finished with the selected objects! Press OK to Close")
DestroyDialog XPX
)
else
(
for o in objects where classof o != TargetObject and
superclassof o != light and superclassof o != camera and
superclassof o != shape and not isGroupMember o do
(
select o
f = o.name+navnet.text
exportfile f #noprompt selectedonly:true
)
messagebox("Finished with all objects! Press OK to Close")
DestroyDialog XPX
)
)



on exec_btn pressed do
(
if updown == true and selection.count == 0 then messagebox("Select something!") else
executer()
)
on chk_sel changed state do
(
if state == on then
(
updown = true
chk_sel.text = "Select objects to export...."
)
else
(
updown = false
chk_sel.text = "Optional : Make Selection...."
)
)

on cls pressed do
(
DestroyDialog XPX
)

on navnet changed txt do
(
if navnet.text != ""then
(
exec_btn.enabled = true) else return exec_btn.enabled = false
)
)

createDialog XPX
)



Dont be too harsh 🙂

BR,
Aplomo
0 Likes