Single script to select different things
 1-20  21-31

Next
 From:  Flowgun
11127.1 
Hello everyone,

It's been a while that I haven't used MoI. I tried to refresh myself a bit today, but I found that I forgot all of my selection hotkeys.
I got the idea to create a single "smart" script that would make different selections depending on the situation.
Basically, what I want to achieve is this:

if nothing is selected {
// select all curves
var curves = moi.geometryDatabase.getObjects().getCurves(); for ( var i = 0; i < curves.length; ++i ) if ( !curves.item(i).isClosed ) curves.item(i).selected = true;
}
else if an object is selected
highlight its naked edges (only of the selected object)
else if a face is selected,
selectFaceEdges()
else if edges are selected // loop selection
moi.geometryDatabase.selectLoop()

Since my last login, I learned lots of programming, but I still can't understand MoI scripting due to the lack of API documentation.
is such a script achievable?
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
11127.2 In reply to 11127.1 
Hi Flowgun, try pasting this in to the "Command" part of a shortcut key:

code:
script: var selected_objs = moi.geometryDatabase.getSelectedObjects();
if ( selected_objs.length == 0 )
{
	/* If nothing selected, select all curves */
	moi.geometryDatabase.getObjects().getCurves().setProperty( 'selected', true );
}
else if ( selected_objs.length == 1 && selected_objs.numBReps == 1 )
{
	/* 1 solid or surface is selected, select naked edges */
	var obj = selected_objs.item(0);
	obj.selected = false;
	obj.getNakedEdges().setProperty( 'selected', true );
}
else if ( selected_objs.length == 1 && selected_objs.numFaces == 1 )
{
	/* 1 face selected, select edges of face */
	var face = selected_objs.item(0);
	face.selected = false;
	face.getEdges().setProperty( 'selected', true );
}
else if ( selected_objs.numEdges > 0 )
{
	/* If any edges are selected do loop selection */
	moi.geometryDatabase.selectLoop();
}


- Michael
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Flowgun
11127.3 In reply to 11127.2 
This is amazing! thank you a lot, Michael!

it works exactly like I asked. Nevertheless, testing it gave me more ideas to make it more exhaustive:

- curve selection: if some curves are already selected, it selects the rest of curves also.

- Group selection: I use your script that groups selected objects by name (object_0001, object_0002,..).
Is it possible to make this script loop through named objects? (if one group is selected / nothing is selected)?

- point selection: if some points on a curve are selected, it selects the rest.

also, I noticed that if more than one object is selected, the script does nothing.
Maybe it would be useful to select the naked edges of all selected objects (can be useful for example to blend between two objects).
I had some other ideas for this, but for consistency's sake and keeping the script for selection only, I guess this is the most logical thing to do.
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
11127.4 In reply to 11127.3 
Hi Flowgun,

re:

> - curve selection: if some curves are already selected, it selects the rest of curves also.

Updated version below should do this now.


> - Group selection: I use your script that groups selected objects by name (object_0001, object_0002,..).
> Is it possible to make this script loop through named objects? (if one group is selected / nothing is selected)?

Sorry I'm not understanding what you want with this part, can you describe it in some more detail please?


> - point selection: if some points on a curve are selected, it selects the rest.

This is in the updated version below.


> also, I noticed that if more than one object is selected, the script does nothing.
> Maybe it would be useful to select the naked edges of all selected objects

This should also be in the updated version below.

- Michael


code:
script: var selected_objs = moi.geometryDatabase.getSelectedObjects();
var all_objs = moi.geometryDatabase.getObjects();
var done = false;

/* If some points on a curve are selected, select the rest */

for ( var i = 0; i < all_objs.length; ++i )
{
	var obj = all_objs.item(i);
	if ( obj.isCurve && obj.hasSelectedEditPoints )
	{
		moi.geometryDatabase.selectAll();
		done = true;
		break;
	}
}

if ( !done )
{
	if ( selected_objs.length == 0 )
	{
		/* If nothing selected, select all curves */
		moi.geometryDatabase.getObjects().getCurves().setProperty( 'selected', true );
	}
	else if ( selected_objs.numStandaloneCurves > 0 )
	{
		/* If some curves are already selected, select the rest of the curves. */
		moi.geometryDatabase.getObjects().getCurves().setProperty( 'selected', true );
	}
	else if ( selected_objs.numBReps > 0 )
	{
		/* If breps are selected, select their naked edges */
		selected_objs.setProperty( 'selected', false );

		var breps = selected_objs.getBReps();
		for ( var i = 0; i < breps.length; ++i )
		{
			var brep = breps.item(i);
			brep.getNakedEdges().setProperty( 'selected', true );
		}
	}
	else if ( selected_objs.length == 1 && selected_objs.numFaces == 1 )
	{
		/* 1 face selected, select edges of face */
		var face = selected_objs.item(0);
		face.selected = false;
		face.getEdges().setProperty( 'selected', true );
	}
	else if ( selected_objs.numEdges > 0 )
	{
		/* If any edges are selected do loop selection */
		moi.geometryDatabase.selectLoop();
	}
}
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Flowgun
11127.5 In reply to 11127.4 
Hi Michael,

Thank you a lot,

>> - Group selection: I use your script that groups selected objects by name (object_0001, object_0002,..).
>> Is it possible to make this script loop through named objects? (if one group is selected / nothing is selected)?

> Sorry I'm not understanding what you want with this part, can you describe it in some more detail please?

I meant that if we have a named object that is selected, it deselects it and selects the next named object in the list (looping through the list continuously).
I find that useful because it allows me to jump between object groups that I make with this script:

script: /* Assign unique object name to selection */ var all_objects = moi.geometryDatabase.getObjects(); var used_names = new Array(); for ( var i = 0; i < all_objects.length; ++i ) { var obj = all_objects.item(i); if ( obj.name != '' ) { used_names[obj.name] = true; } } var counter = 1; var name; while ( 1 ) { var numtag = counter.toString(); while ( numtag.length < 4 ) { numtag = '0' + numtag; } name = 'object_' + numtag; if ( used_names[name] ) { ++counter; continue; } used_names[name] = true; break; } moi.geometryDatabase.getSelectedObjects().setProperty( 'name', name );

This might be a niche functionality that other users won't find very useful though.
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
11127.6 In reply to 11127.5 
Hi Flowgun,

re:
> I meant that if we have a named object that is selected, it deselects it and selects the next named
> object in the list (looping through the list continuously).

Ok, I added a section that should do that:

code:
script: var selected_objs = moi.geometryDatabase.getSelectedObjects();
var all_objs = moi.geometryDatabase.getObjects();
var done = false;

/* If some points on a curve are selected, select the rest */

for ( var i = 0; i < all_objs.length; ++i )
{
	var obj = all_objs.item(i);
	if ( obj.isCurve && obj.hasSelectedEditPoints )
	{
		moi.geometryDatabase.selectAll();
		done = true;
		break;
	}
}

/* If a named object is selected, deselect it and select the next one */

if ( !done )
{
	var objects = [], name_index = [];
	for ( var i = 0; i < all_objs.length; ++i )
	{
		var obj = all_objs.item(i);
		var name = obj.name;
		if ( name )
		{
			if ( name_index[name] == undefined )
			{
				name_index[name] = objects.length;
				objects.push( [] );
			}

			var index = name_index[name];
			objects[index].push( obj );
		}
	}

	function sortfunc( a, b )
	{
		var a_name = a[0].name;
		var b_name = b[0].name;

		return a_name.localeCompare(b_name);
	}

	objects.sort( sortfunc );

	var index_to_select = -1;

	for ( var i = 0; i < objects.length && index_to_select == -1; ++i )
	{
		var obj_array = objects[i];
		for ( var j = 0; j < obj_array.length && index_to_select == -1; ++j )
		{
			var obj = obj_array[j];
			if ( obj.selected )
			{
				index_to_select = (i+1) % objects.length;
			}
		}
	}

	if ( index_to_select != -1 )
	{
		moi.geometryDatabase.deselectAll();

		var obj_array = objects[index_to_select];
		for ( var i = 0; i < obj_array.length; ++i )
		{
			var obj = obj_array[i];
			obj.selected = true;
		}

		done = true;
	}
}

if ( !done )
{
	if ( selected_objs.length == 0 )
	{
		/* If nothing selected, select all curves */
		moi.geometryDatabase.getObjects().getCurves().setProperty( 'selected', true );
	}
	else if ( selected_objs.numStandaloneCurves > 0 )
	{
		/* If some curves are already selected, select the rest of the curves. */
		moi.geometryDatabase.getObjects().getCurves().setProperty( 'selected', true );
	}
	else if ( selected_objs.numBReps > 0 )
	{
		/* If surfaces are selected, select their naked edges */
		selected_objs.setProperty( 'selected', false );

		var breps = selected_objs.getBReps();
		for ( var i = 0; i < breps.length; ++i )
		{
			var brep = breps.item(i);
			brep.getNakedEdges().setProperty( 'selected', true );
		}
	}
	else if ( selected_objs.length == 1 && selected_objs.numFaces == 1 )
	{
		/* 1 face selected, select edges of face */
		var face = selected_objs.item(0);
		face.selected = false;
		face.getEdges().setProperty( 'selected', true );
	}
	else if ( selected_objs.numEdges > 0 )
	{
		/* If any edges are selected do loop selection */
		moi.geometryDatabase.selectLoop();
	}
}
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Flowgun
11127.7 In reply to 11127.6 
Thank you a lot Michael,
I appreciate the time you are spending on my suggestions enormously, which makes me self-conscious to ask for more modifications.

the script is already extremely helpful, and it's getting where I want it to be. but the last version seems to have some conflicts.
Upon further thought, I came up with this list of suggestions that should resolve these conflicts and make it more user-friendly:

- if some edit points are selected on some curves, it adds the rest of edit points to the selection (only on curves that already have some points selected)

- if multiple objects that share the same name are selected, but not all of the objects that have that name, then it expands the selection to all of them (that have the same name)

- if nothing is selected or if multiple objects that share the same name are selected, then select the next named objects on the list
/* This should go to the next named objects only if all the objects that share that name are selected, indicated by the full moon icon in front of the name */

- if a curve or unnamed curves are selected and edit points are not selected, then it selects all curves

- if a single object is selected, then select its naked edges

the rest of the script should work as previous scripts:

- with edges selected, then it selects the loop
- with a face/surface selected, then it selects its edges

I hope that my suggestions are clear, and I hope that other users find it useful and share their feedback.
Personally, I like to set it up to the backtick/tilde key to get all my selection needs with just one key.

EDITED: 22 Jun 2023 by FLOWGUN

  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
11127.8 In reply to 11127.7 
Hi Flowgun,

re:
> - if some edit points are selected on some curves, it adds the rest of edit points to the selection
> (only on curves that already have some points selected)

The current script should be doing this now. Can you show me an example of how it's not working as you want for this part?


> - if a curve or unnamed curves are selected and edit points are not selected, then it selects all curves

Also it should be doing this part currently as well. Can you show an example for how this case is not working as you expected as well?

Currently if the curve is a named object then it gets handled by the named objects processing and further processing is skipped.


>- if a single object is selected, then select its naked edges

Same thing with this case, the current script is doing this now.


> I hope that my suggestions are clear

There are a couple of areas that I'm not too clear on like if you have a named curve should it be processed by the "named objects" part of the script or the "select all curves" part of the script or both?

It would be a lot clearer if you could post example files and show what the desired result should be with each file, that gives me something I can test with rather than just guessing at how to resolve ambiguous cases. I think I'll need some testable examples like that to help me understand what to do to make further progress.


- Michael
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Flowgun
11127.9 In reply to 11127.8 
Hello Michael,
Sorry for the late.
Here's an example file with a breakdown of the cases.

case 1:
selection: a single box or a surface that are a part of a named collection
wanted behavior: select its naked edges/boundaries
Desired result: it selects the next named objects on the list

case 2:
selection: a couple of boxes that are a part of the same named collection
wanted behavior: select the rest of that same collection (the remaining box)
Desired result: it selects the next named collection in the list. I want this behavior either when nothing is selected, or all the elements of the collection are selected (indicated by the full moon icon)

case 3:
Selection: Edit points on a curve or multiple curves(with whatever else selected - should be checked first?)
wanted behavior: the rest of edit points on the same curve(s) are added to whatever else is selected.
Desired result: Edit points are not added to selection, but the curves are. if I also have a box selected, it defaults to "case 2".
Why: it's sometimes faster and easier to deselect some edit points when all are selected than to have to click
them one by one.

What works as I want:
- selecting a named curve (ex: the S-shaped curves) should be processed by the "named objects". I guess if someone names the curves, he'd want to get them specifically.
- selecting an unnamed curve (ex: a circle) selects all the rest of curves
- selecting edges selects the "edge loop".
- selecting a face selects its boundaries.
Attachments:

  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
11127.10 In reply to 11127.9 
Hi Flowgun, thanks for the example file, it helps to be able to reference a specific example.

A couple of things are still not clear to me.


> case 1:
> selection: a single box or a surface that are a part of a named collection
> wanted behavior: select its naked edges/boundaries

Ok, but In a previous message above (http://moi3d.com/forum/index.php?webtag=MOI&msg=11127.3) you wrote:

> also, I noticed that if more than one object is selected, the script does nothing.
> Maybe it would be useful to select the naked edges of all selected objects

So should naked edges be selected on all selected objects like you asked for previously, or should it only be when there is a single object selected like in your latest message?


Also if the object is a named object, it seems like there are 2 different rules that could apply to it - both the "expand to other named objects or switch to next name" and also the "select naked edges" . Which one of these should be applied? And what if there are no naked edges on the object?


Also for case 3:
> case 3:
> Selection: Edit points on a curve or multiple curves(with whatever else selected - should be checked first?)
> wanted behavior: the rest of edit points on the same curve(s) are added to whatever else is selected.

Can you show me a step-by-step example of how this is not working for you currently?

Here's an example of the current behavior - if a curve has edit points showing, and some are selected like this:



Then triggering the script will result in this:



This matches what you asked for in this previous message (http://moi3d.com/forum/index.php?webtag=MOI&msg=11127.3) which was this:

> - point selection: if some points on a curve are selected, it selects the rest.


- Michael

EDITED: 24 Jun 2023 by MICHAEL GIBSON


  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Flowgun
11127.11 In reply to 11127.10 
ah yes. my bad.

sorry that I'm being confusing and having trouble deciding and testing :facepalm:
The script is getting kinda complicated that I myself have trouble keeping up with it.
Thank you a lot for baring with me...

case 3: works as expected.

case 1: after further reflection:
- if a single object is selected: selects its naked edges. if it has none, it can simply deselect it.
- if edges are selected on an object, and there are other objects in the selection: it adds the
naked edges of other objects to the edge selection (or it deselects them if they have none, but without
deselecting the edges).
>>> these two conditions together would allow to scout the scene, selecting one object at a time
and pressing the hotkey to select the naked edges on multiple objects.

- if multiple objects are selected, but no edges are selected:
"expand to other named objects or switch to next name"
(basically the way it behaves now)

Some more brainstorming:
- I know that many users use Styles instead of names to group elements. Maybe it can be useful
to check for the styles too if the objects are not named and they don't have the default style, and expand
to the same style or switch to the next one (similarly to my suggestions with the names). I don't know
yet what can of worms this would open though :S

Edit: another behavior: if nothing is selected, then select the result of the last operation:
script:var a = moi.command.lastCommandRevisionStart; var b = moi.command.lastCommandRevisionEnd; var objects = moi.geometryDatabase.getObjects(); for ( var i = 0; i < objects.length; ++i ) { var obj = objects.item(i); if ( obj.databaseRevision> a && obj.databaseRevision <= b ) obj.selected = true; }

-----------------------
I hope this is enough/not too much to create a satisfactory script for everyone to use.

EDITED: 24 Jun 2023 by FLOWGUN

  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
11127.12 In reply to 11127.11 
Hi Flowgun,

re:
> - if a single object is selected: selects its naked edges. if it has none, it can simply deselect it.

What if the single selected object is named?


> Edit: another behavior: if nothing is selected, then select the result of the last operation:

That conflicts with another condition that you specified originally which was this:

> if nothing is selected {
> // select all curves


= Michael
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Flowgun
11127.13 In reply to 11127.12 
yup.
I'm aware of these conflicts. I meant to update old requests with new ones.
there are probably still more conflicts incoming though. I'm tired, boss xD

Maybe it's not that wise to try to cram everything together after all.

to continue with the effort for a holistic script:
- if a single object is named, select naked edges should hold higher priority.
Single objects are easily selectable from the viewport, and if the wanted result is to select
multiple named object, then selecting a couple of them and hitting the hotkey would select
all the rest.
- "Select all curves": only if an unnamed curve is selected.

Otherwise, to avoid conflicts, and even offer more options, the script can be broken into 3:
- one for dealing with objects: selects last-created objects if nothing is selected, otherwise looping through named objects (and it wouldn't matter if they are curves or not)
- one for selecting edges (naked edges or edge loops / boundaries)
- one to select curves: it would loop through selecting open curves, closed curves, and all curves. even if nothing is selected.

these 3 scripts can check first for selected edit points to select the rest of them.
they can be set for example to tilde, alt+tilde, and ctrl+tilde.
That's already lot neater and easier to keep up with than my older config with around 10 hotkeys for selection that I never got used to.
and I believe it's more user-friendly and more predictable than the all-in-one problematic solution.
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
11127.14 In reply to 11127.13 
Hi Flowgun,

re:
> - if a single object is named, select naked edges should hold higher priority.

So the consequence of doing this is that if you have a named object and it's the only one with that name, that the "cycle between named objects" mechanism will get stuck when it hits this object since it will try to select naked edges when the selection is on it rather than continuing with the named object cycling.


> Otherwise, to avoid conflicts, and even offer more options, the script can be broken into 3:
> - one for dealing with objects: selects last-created objects if nothing is selected, otherwise
> looping through named objects (and it wouldn't matter if they are curves or not)
> - one for selecting edges (naked edges or edge loops / boundaries)
> - one to select curves: it would loop through selecting open curves, closed curves, and all curves. even if nothing is selected.

Do you have all the pieces you need to compose these 3 scripts yourself?

Maybe if you get stuck on a particular part I can help you with that particular part instead of trying to create the whole script myself, because there have just been way too many changes for me to keep track of, sorry.

For selecting the last created you already showed the code to do that above. There is also this method:
moi.geometryDatabase.selectLastCreated();


I have an update here though which fixes up the named object cycling so that if some but not all of a named object set are selected it gets all of the current name first before stepping to the next name:

code:
script: var selected_objs = moi.geometryDatabase.getSelectedObjects();
var all_objs = moi.geometryDatabase.getObjects();
var done = false;

/* If some points on a curve are selected, select the rest */

for ( var i = 0; i < all_objs.length; ++i )
{
	var obj = all_objs.item(i);
	if ( obj.isCurve && obj.hasSelectedEditPoints )
	{
		moi.geometryDatabase.selectAll();
		done = true;
		break;
	}
}

/* If a named object is selected, deselect it and select the next one */

if ( !done )
{
	var objects = [], name_index = [];
	for ( var i = 0; i < all_objs.length; ++i )
	{
		var obj = all_objs.item(i);
		var name = obj.name;
		if ( name )
		{
			if ( name_index[name] == undefined )
			{
				name_index[name] = objects.length;
				objects.push( [] );
			}

			var index = name_index[name];
			objects[index].push( obj );
		}
	}

	function sortfunc( a, b )
	{
		var a_name = a[0].name;
		var b_name = b[0].name;

		return a_name.localeCompare(b_name);
	}

	objects.sort( sortfunc );

	var index_to_select = -1;

	for ( var i = 0; i < objects.length && index_to_select == -1; ++i )
	{
		var obj_array = objects[i];
		for ( var j = 0; j < obj_array.length && index_to_select == -1; ++j )
		{
			var obj = obj_array[j];
			if ( obj.selected )
			{
				var any_unselected = false;

				for ( var k = 0; k < obj_array.length; ++k )
				{
					if ( !obj_array[k].selected )
					{
						any_unselected = true;
						break;
					}
				}

				if ( any_unselected )
					index_to_select = i;
				else
					index_to_select = (i+1) % objects.length;
			}
		}
	}

	if ( index_to_select != -1 )
	{
		moi.geometryDatabase.deselectAll();

		var obj_array = objects[index_to_select];
		for ( var i = 0; i < obj_array.length; ++i )
		{
			var obj = obj_array[i];
			obj.selected = true;
		}

		done = true;
	}
}

if ( !done )
{
	if ( selected_objs.length == 0 )
	{
		/* If nothing selected, select all curves */
		moi.geometryDatabase.getObjects().getCurves().setProperty( 'selected', true );
	}
	else if ( selected_objs.numStandaloneCurves > 0 )
	{
		/* If some curves are already selected, select the rest of the curves. */
		moi.geometryDatabase.getObjects().getCurves().setProperty( 'selected', true );
	}
	else if ( selected_objs.numBReps > 0 )
	{
		/* If surfaces are selected, select their naked edges */
		selected_objs.setProperty( 'selected', false );

		var breps = selected_objs.getBReps();
		for ( var i = 0; i < breps.length; ++i )
		{
			var brep = breps.item(i);
			brep.getNakedEdges().setProperty( 'selected', true );
		}
	}
	else if ( selected_objs.length == 1 && selected_objs.numFaces == 1 )
	{
		/* 1 face selected, select edges of face */
		var face = selected_objs.item(0);
		face.selected = false;
		face.getEdges().setProperty( 'selected', true );
	}
	else if ( selected_objs.numEdges > 0 )
	{
		/* If any edges are selected do loop selection */
		moi.geometryDatabase.selectLoop();
	}
}



- Michael
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
11127.15 In reply to 11127.13 
So if you wanted to give the select naked edges part priority over the named object handling, take this piece:

code:
	else if ( selected_objs.numBReps > 0 )
	{
		/* If surfaces are selected, select their naked edges */
		selected_objs.setProperty( 'selected', false );

		var breps = selected_objs.getBReps();
		for ( var i = 0; i < breps.length; ++i )
		{
			var brep = breps.item(i);
			brep.getNakedEdges().setProperty( 'selected', true );
		}
	}


Remove the "else" at the start there and insert a done = true; inside it, and move it above the named object handling which starts at this comment:
/* If a named object is selected, deselect it and select the next one */

And if you want it to do naked edges if only one object is selected change this:
if ( selected_objs.numBReps > 0 )
to this:
if ( selected_objs.numBReps == 1 )


But like I mentioned, if you do that then if you have a solid that is a named object and it is the only object with that name, the "step to next named object" is going to get stuck on it.

= Michael
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Flowgun
11127.16 In reply to 11127.15 
Thank you a lot, Michael.
With this, I should have all the pieces I need.
sorry for asking too much.
I appreciate you.

- Flowgun
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
11127.17 In reply to 11127.13 
Some more pieces to use for the 3 separate scripts:


> - one for dealing with objects: selects last-created objects if nothing is selected, otherwise
> looping through named objects (and it wouldn't matter if they are curves or not)

Nothing selected is detected like this:

code:
var selected_objs = moi.geometryDatabase.getSelectedObjects();

if ( selected_objs.length == 0 )
{
        .....
}


Selecting last created objects is: moi.geometryDatabase.selectLastCreated();

Stepping through named objects is the chunk of the script above that follows this:

/* If a named object is selected, deselect it and select the next one */


> - one for selecting edges (naked edges or edge loops / boundaries)

Selecting naked edges is the part of the script above with:
/* If surfaces are selected, select their naked edges */

Selecting edges of a face is this part:
/* 1 face selected, select edges of face */

Loop selection is this part:
/* If any edges are selected do loop selection */


> - one to select curves: it would loop through selecting open curves, closed curves, and all curves. even if nothing is selected.

Selecting all curves is the part marked with this:
/* If nothing selected, select all curves */

to select just open or closed curves, that would be like this:

code:
var curves = moi.geometryDatabase.getObjects().getCurves();

for ( var i = 0; i < curves.length; ++i )
{
    var crv = curves.item(i);
    if ( crv.isClosed )
        crv.selected = true;
}


- Michael
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Flowgun
11127.18 In reply to 11127.17 
Here's what I came up with for now. It's not perfect, but still very useful:
code:
script: var selected_objs = moi.geometryDatabase.getSelectedObjects();
var all_objs = moi.geometryDatabase.getObjects();
var done = false;

if ( selected_objs.length == 0 )
{
	/* If nothing selected, select last-created object */
	moi.geometryDatabase.selectLastCreated();
	done = true;
}
else if ( selected_objs.numBReps > 0 )
	{
		/* If only joined surfaces are selected, select their naked edges */
		selected_objs.setProperty( 'selected', false );

		var breps = selected_objs.getBReps();
		var allNaked = true;
		
		for ( var i = 0; i < breps.length; ++i )
		{
			var brep = breps.item(i);
			if (brep.getNakedEdges().length == 0)
				allNaked = false;
		}
		if (allNaked)
		{
			for ( var i = 0; i < breps.length; ++i )
			{
				var brep = breps.item(i);
				var naked = brep.getNakedEdges();
				if (naked.length> 0)
					naked.setProperty( 'selected', true );
			}				
			done = true;
		}			
		else
			selected_objs.setProperty( 'selected', true );
}

/* If some points on a curve are selected, select the rest */
if ( !done )
{
for ( var i = 0; i < all_objs.length; ++i )
{
	var obj = all_objs.item(i);
	if ( obj.isCurve && obj.hasSelectedEditPoints ) /* this only works in latest versions */
	{
		moi.geometryDatabase.selectAll();
		done = true;
		break;
	}
}
}
/* If a named object is selected, deselect it and select the next one */

if ( !done )
{
	var objects = [], name_index = [];
	for ( var i = 0; i < all_objs.length; ++i )
	{
		var obj = all_objs.item(i);
		var name = obj.name;
		if ( name )
		{
			if ( name_index[name] == undefined )
			{
				name_index[name] = objects.length;
				objects.push( [] );
			}

			var index = name_index[name];
			objects[index].push( obj );
		}
	}

	function sortfunc( a, b )
	{
		var a_name = a[0].name;
		var b_name = b[0].name;

		return a_name.localeCompare(b_name);
	}

	objects.sort( sortfunc );

	var index_to_select = -1;

	for ( var i = 0; i < objects.length && index_to_select == -1; ++i )
	{
		var obj_array = objects[i];
		for ( var j = 0; j < obj_array.length && index_to_select == -1; ++j )
		{
			var obj = obj_array[j];
			if ( obj.selected )
			{
				var any_unselected = false;

				for ( var k = 0; k < obj_array.length; ++k )
				{
					if ( !obj_array[k].selected )
					{
						any_unselected = true;
						break;
					}
				}

				if ( any_unselected )
					index_to_select = i;
				else
					index_to_select = (i+1) % objects.length;
			}
		}
	}
	if ( index_to_select != -1 )
	{
		moi.geometryDatabase.deselectAll();

		var obj_array = objects[index_to_select];
		for ( var i = 0; i < obj_array.length; ++i )
		{
			var obj = obj_array[i];
			obj.selected = true;
		}
		done = true;
	}
}

if ( !done )
{
	if ( selected_objs.numStandaloneCurves> 0 )
	{
		/* If some curves are already selected, we loop through curve selection. */
		var curves = moi.geometryDatabase.getObjects().getCurves();
		var allSelected = true;
		var closedCurves = true;

		for ( var i = 0; i < curves.length; ++i )
		{
			var crv = curves.item(i);
			if (! crv.selected)
			{
				allSelected = false;
				if ( crv.isClosed)
					closedCurves = false;
			}
		}
		selected_objs.setProperty( 'selected', false );

		if (allSelected)
		{
			for ( var i = 0; i < curves.length; ++i )
			{
				var crv = curves.item(i);
				if (crv.isClosed)
				crv.selected = true;
			}
		}
		else if (closedCurves)
		{
			for ( var i = 0; i < curves.length; ++i )
			{
				var crv = curves.item(i);
				if (! crv.isClosed)
					crv.selected = true;
			}
		}
		else /* select all */
		{
			curves.setProperty( 'selected', true );
		}
	}
	else if ( selected_objs.length> 0 && selected_objs.numFaces >= selected_objs.length )
	{
		/* faces selected, select the edges of the faces */
		for ( var i = 0; i < selected_objs.numFaces; ++i )
		{
			var face = selected_objs.item(i);
			face.selected = false;
			face.getEdges().setProperty( 'selected', true );
		}
	}
	else if ( selected_objs.numEdges> 0 )
	{
		/* If any edges are selected do loop selection */
		moi.geometryDatabase.selectLoop();
	}
}



// - Flowgun


Edited by Michael - convert // comments into /* */ and add a couple missing semi-colons so it can work pasted into a single line as a shortcut key.

EDITED: 14 Jul 2023 by MICHAEL GIBSON

  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
11127.19 In reply to 11127.18 
Hi Flowgun, thanks for sharing it. I edited your post to put it inside of <code></code> so it would show tabs.

- Michael
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Psygorn (DRILLBIT)
11127.20 In reply to 11127.19 
Hi Michael,

With this script one can select loop of edges?

How about edges that are connected to a face?

I should assign a short key to this script, if I want to use it?

- Psygorn
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged
 

Reply to All Reply to All

 

 
Show messages:  1-20  21-31