ScriptSpot

Forum topic · Scripts Wanted

select just one instance

By titane357 · 2013-10-18

Description

hello
I look for a script to select just one instance of an object :
- select all objects I want to work with
- run the script
- just one instance of each object is selected.
Thanks

Comments (17)

???

kredka · 2013-11-24

So regarding Your code, I'm not maxscript master, I making my first baby steps in maxscript so I would need more explanation regarding this script:

fn reassignModifier mode: modyClass: =
(
local tsArr = getClassInstances modyClass
local objs = case mode of
(
#all:
(
if tsArr.count == 0 then #() else
(
for i = 1 to tsArr.count where (nodes = refs.dependentNodes tsArr[i]).count != 0 collect nodes[1]
)
)
#selection:
(
if selection.count == 0 then #() else
(
local firstInstances = #()
for o in selection where o.modifiers.count != 0 do
(
InstanceMgr.GetInstances o &firstOnly
if firstOnly.count > 1 then append firstInstances firstOnly[firstOnly.count-1] else append firstInstances o
)
firstInstances
)
)
)
if objs.count != 0 and tsArr.count != 0 do
(
max create mode
with redraw off
(
for o in objs do
(
for m = o.modifiers.count to 1 by -1 where isKindOf o.modifiers[m] TurboSmooth do
(
bulb = o.modifiers[m].enabled
mody = copy o.modifiers[m] ; mody.enabled = bulb ; deleteModifier o m ; addModifier o mody before:(m-1)
)
)
)
)
)

I created some objects with active turbosmooth, fired it up and I noticed script do preety much nothing:) I eresed
"addModifier o mody before:(m-1)" line 37 so cript should only delete modyfiers(it's only way for me to check if script work, because modyfier look identical before and after execution) but it seems script dont erase modyfiers. I have also impression that max don't touched this modyfiers - last script make it blink - there was some reaction on max modyfi interface . I don't even know is it working at all or it's work and I use it in wrong way? Next thing is, you gave to line for action on selection and action for all scene elements but how to use it, exactly, how to create two different script for this using this command. Next thing is script copy and paste this same turbosmooth. When there is skin glitch cased by turbosmooth - turbosmooth must be deleted and added new because contain glitch so if I copy and paste this same turbosmoothit is useless.So settings from oryginal turbosmooth mus be taken, old turbosmooth must be removed, new added and settings from oryginal must be set also check if turbosmmoth is active or not and set it identical.

I know my understanding of maxscript leave a lot to be desired but I'm beginner:)
Can You explane this script, please, maybe I just don't get it.

Don't retired, people need help:)

barigazy · 2013-11-24

this solution works for any modifier class and both (selection and all scene)


fn reassignModifier mode: modyClass: = 
(
	local tsArr = getClassInstances modyClass
	local objs = case mode of
	(
		#all: 
		(
			if tsArr.count == 0 then #() else
			(
				for i = 1 to tsArr.count where (nodes = refs.dependentNodes tsArr[i]).count != 0 collect nodes[1]
			)
		)
		#selection:
		(
			if selection.count == 0 then #() else
			(
				local firstInstances = #()
				for o in selection where o.modifiers.count != 0 do 
				(
					InstanceMgr.GetInstances o &firstOnly
					if firstOnly.count > 1 then append firstInstances firstOnly[firstOnly.count-1] else append firstInstances o
				)
				firstInstances
			)
		)
	)
	if objs.count != 0 and tsArr.count != 0 do
	(
		max create mode
		with redraw off 
		(
			for o in objs do
			(
				for m = o.modifiers.count to 1 by -1 where isKindOf o.modifiers[m] TurboSmooth do
				(
					bulb = o.modifiers[m].enabled
					mody = copy o.modifiers[m] ; mody.enabled = bulb ; deleteModifier o m ; addModifier o mody before:(m-1)
				)	
			)
		)			
	)
)

You have two fn arguments: "mode" and "modyClass"
This will works only on selected objects


reassignModifier mode:#selection modyClass:TurboSmooth

and this will take to acount all scene modifiers of specified class


reassignModifier mode:#all modyClass:TurboSmooth

BTW... This is my 15000 point on this forum (see "users by points").
Maybe it's time to retire. Why do you think?

:)

.

miauu · 2013-11-24

Make your 15 001 post and post the code above in the right thread. :)

roamn · 2013-11-07

Or you can use instance trimmer from neil blevins script pack (which everyone should have installed already :-) )

barigazy · 2013-10-18

Try this

if selection.count != 0 do
(
local firstInstances = #()
for o in selection do
(
InstanceMgr.GetInstances o &firstOnly
if firstOnly.count > 1 do
append firstInstances firstOnly[firstOnly.count-1]
)
if firstInstances.count != 0 do select firstInstances
)

titane357 · 2013-10-19

Thank you very much ! :-)

Very Nice!

eyepiz · 2013-12-03

Could it me made to select all the objects that are not instances too?
so if I wanted to apply a modifier in all selected objects, that it would select the one instanced object like in the script and the objects that are not instances as well.

I could really use something like this.

Thanks!

Eric

barigazy · 2013-12-03

Yup. Script select all non-instances object and first instace

eyepiz · 2013-12-03

Cool...how do I add this to the script below?

(
if selection.count != 0 do
(
local firstInstances = #()
for o in selection do
(
InstanceMgr.GetInstances o &firstOnly
if firstOnly.count > 1 do
append firstInstances firstOnly[firstOnly.count-1]
)
if firstInstances.count != 0 do select firstInstances
)
)

barigazy · 2013-12-03

Which script below?

eyepiz · 2013-12-03

Thanks!

eyepiz · 2013-12-03

if selection.count != 0 do
(
local firstInstances = #()
for o in selection do
(
InstanceMgr.GetInstances o &firstOnly
if firstOnly.count > 1 do
append firstInstances firstOnly[firstOnly.count-1]
)
if firstInstances.count != 0 do select firstInstances
)

barigazy · 2013-12-03

Using this fn U can do what you want


	fn collectUniqueNodes objs = if objs.count != 0 do
	(
		local getNodeByHandle = maxOps.getNodeByHandle, getObjIns = InstanceMgr.GetInstances
		local uniqNodes = #(), allINodes = #()
		for o in objs do append allINodes o.inode.handle
		while allINodes.count != 0 do
		(
			obj = (getNodeByHandle allINodes[allINodes.count])
			if isValidNode obj do append uniqNodes obj
			if not (refhierarchy.IsRefTargetInstanced obj) then deleteItem allINodes allINodes.count else
			(
				getObjIns obj &firstOnly
				for o in firstOnly where (idx = findItem allINodes o.inode.handle) != 0 do deleteItem allINodes idx
			)
		)
		uniqNodes
	)

Now "objs" argument in fn represent array ei.collections of object.
If you want to consider only selected objects then run


collectUniqueNodes selection

to consider only geo-objects use


arr = for o in geometry where not isKindOf o TargetObject collect o
collectUniqueNodes arr

eyepiz · 2013-12-04

Still now working for me...
We are putting a script together to clean up imported CAD geometry. What we would like it to do is to selects all non instanced objects and a single instance of the instanced objects.

our goal is to select only the necessary objects prior to running the other scripts such as delete isolated verts, weld and smooth, flip inverted normal, quadrify ect...

I would appreciate any help.

Thank you!

barigazy · 2013-12-04

I simplified fn and now it works fine

fn collectUniqueNodes objs = if objs.count != 0 do
(
local getNodeByHandle = maxOps.getNodeByHandle, getObjIns = InstanceMgr.GetInstances
local uniqNodes = #(), allINodes = #()
for o in objs do append allINodes o.inode.handle
while allINodes.count != 0 do
(
obj = (getNodeByHandle allINodes[allINodes.count])
getObjIns obj &firstOnly ; append uniqNodes firstOnly[firstOnly.count]
for o in firstOnly where (idx = findItem allINodes o.inode.handle) != 0 do deleteItem allINodes idx
)
uniqNodes
)
select (collectUniqueNodes objects)

eyepiz · 2013-12-04

This works!...1000 Thanks!