Notifications
Clear all

[Closed] my messy code

you can access all objects in the scene through the key word, ‘objects’

this here


  select objects
   myArray = #()
   ---- collect all object on scene
   for o in selection do 
   (
   append myArray o
   )
  

is equivalent to


  myArray = $selection as array
  

there are even more ways to store a group of objects as well. Say you want to collect all objects with the name ‘box’?


  myArray = for obj in objects where matchpattern obj.name pattern:("*box*") collect obj
  

that basically translates…
” look through all the objects in the scene, and give me all the ones that have box in their name.”

Now the * is a special character. since its before box, and after box, it means it will accept any character in front, and any character in back… this means, if an object is named ‘aboxa’ it will collect it. If you remove the *’s it will only find objects named ‘box’. Make sense?

You can use that similar command to collect objects that are just geometry, ones that have modifiers, etc. Might be a little advanced, but its a fun little macro.

Now in terms of your problem. DeleteItem is looking for an integer. So if you were trying to do that way, you would have to incorporate findItem.


  for obj in $selection do
  (
  local index = findItem myArray obj
  if index != 0 do 
  deleteItem myArray index
  )
  

we do the check if index != 0, because 0 will be returned from ‘findItem’ if the value isnt in the array.

In problem 2, you have to iterate through your ‘mycontrollerList’ and compare values to the objects controller


  select geometry
   mycontrollerlist=#(Position_XYZ,audioposition)
   
  for o in selection do 
  (
  local controller =  classof o.position.controller
  
  for item in mycontrollerlist do
  (
  if item == controller then
  (
  messagebox "I found my controllers"
  exit
  )
  )
  

the exit should stop you from running anymore loops. This is untested, but should work.

I’m not as skilled as Denis T, or some of the other guys on the forum, so they might have better ways of achieving this result, so keep that in mind.

-- make a sample scene
(
    -- delete all objects to make a clean scene
	delete objects 
	-- create 100 dummy objects just for test
	for k=1 to 100 do dummy name:(k as string)
	-- use garbage collection to free memory to make our performance measurement more accurate 
	gc()

	-- collect all available creatable float classes 
	cc = for c in FloatController.classes where c.creatable collect c 
	/*
	   [color=Yellow] getclassinstances - returns a list of all instances of a specified class in the scene 
		we are looking for Bezier_Float because it's default controller for any float controller
	
	    createinstance - creates an instance of a specified class if the class creatable 
		replaceinstances - replace an instance with another instance in all places where it was used   
	*/
[/color]    -- here we randomly populate all our float controllers with randomly picked class instance 
	for c in getclassinstances Bezier_Float do replaceinstances c (createinstance cc[random 1 cc.count])

	-- go through all float creatable classes and find its instances in the scene	
	for c in cc do
	(
		format ">> %
" c  
		nodes = #()
		t1 = timestamp()
		/*
			[color=Yellow]refs.dependentnodes with extra argument firstonly - returns the first node which depends on specified object... 
			in our case it's a controller.
			because the same node might be controlled by many other controllers we have put append a node to the list if only it's not exist.
			we use appendifunique for that
		*/
[/color]		for i in (ii = getclassinstances c) where (node = refs.dependentnodes i firstonly:on) != undefined do appendifunique nodes node
		format "	nodes:% controllers:% search time:%
" nodes.count ii.count ((timestamp()-t1)/1000.0)
	)
)

here is another explore function:

fn queryAllFloatControllersByClass class =
(
	for c in getclassinstances class astrackviewpick:on do
	(
		node = refs.dependentnodes c.anim firstonly:on
		format "controller: %
	anim:		%
	subname:	%
	client:		%
	node:		%

" c.name c.anim (getSubAnimName c.client c.subnum) c.client (try(node.name) catch())
	)
)
queryAllFloatControllersByClass Bezier_Float

Thank You, now it’s time to push it in to action.

I used to programme in AmosBasic on Amiga when I was teenager just for my self.From what I remember Amos was much simpler language but way more limited, of course larger code bricks = lower programming ability but it was so easy and intuitive. Here it have to be 7 line of code to do such a simple stuff which actually could be describe in two lines of code:

myobjectlist #(Box,Sphere,Dummy)
search for object on scene if object=myobjectlist then do …

However I cant use Amos to operate inside max:)
Without your help I would’t be able to do much:)

for me as novice this code was most transparent

select geometry
mycontrollerlist=#(Position_XYZ,audioposition)

for o in selection do
(
local controller = classof o.position.controller

for item in mycontrollerlist do
(
if item == controller then
(
messagebox “I found my controllers”
exit
)
)

I check it on geometry with Position_XYZ controllers
But don’t work.

(
	-- make a test scene
	delete objects
	which = #(Box, Sphere, Cone, Teapot)
	for k=1 to 100 do which[random 1 4] name:(formattedprint k format:"03d")

	-- do a check
	for n in objects do
	(
		i = finditem which (classof n)
		case i of
		(
			1: format "%:	-> Box
" n.name 
			2: format "%:	-> Sphere
" n.name 
			3: format "%:	-> Cone
" n.name 
	  default: format "%:	-> X3
" n.name 
		)
	)
)

ok, let say I have such a case: I have numbers in list and I what popup if I found on of object from my list. Code is:

objlist=#(2,4,5)
for i =1 to 10 do
(
print i
if i==objlist then messagebox “sss” –<— here is my action but don’t work
)

and second case:

I want loop in exception(exclude) of my objlist=#(2,4,5)

so it should be:
for i=(from 1 with exception of objlist)? to 10

I know it’s simple but I know I’m very close with first one however something is missing. Second case may be totally wrong.

1 Reply
(@gazybara)
Joined: 10 months ago

Posts: 0

First case

for i = 1 to 10 where findItem objlist i != 0 do print i 

And second case (opposite)

for i = 1 to 10 where findItem objlist i == 0 do print i

BTW read about “Array Values” topic in mxs help

Page 2 / 2