Hi Elang,
re:
> How can I assign a name to this line immediately after it is created, without manually
> selecting it first, so that I can easily refer to it later in the script?
The preferred way to do that is to retrieve the object reference when it is created.
There are 2 ways that geometry factories can be used, one is by hooking up UI controls to factory inputs, triggering a factory.update() when the UI controls are changed, and then calling factory.commit() at the end. This is the way that most commands in MOI work since they have UI controls.
That way handles various things automatically like removing any previously generated objects from the geometry database and inserting the newly generated ones into the geometry database so they are displayed, clearing them out if the command is canceled, and other similar stuff.
Your example here is not like that:
code:
var linefactory = moi.command.createFactory( 'line' );
linefactory.setInput( 0, moi.vectorMath.createPoint( 0,0,0 ) );
linefactory.setInput( 1, moi.vectorMath.createPoint( 10,10,10 ) );
linefactory.commit();
The regular line command does a lot more than that, it sets up a point picker and spins an event loop waiting for the user to click a point with the mouse or type in the coordinates of the point, set a checkbox option, etc...
Your script here just wants to generate a line between 2 specific points, not collected from user input and no UI options to be set. Perhaps you don't even want the line to even be displayed and just use it as a building block to be sent in as an input to another factory.
For that case it is better to use the 2nd method of using a geometry factory, the factory.calculate() function. That will return an object list of what the factory generated and doesn't do anything else. The result will not even be displayed yet since it will be a "loose object" and not inserted into the Geometry Database yet, you can do that manually with moi.geometryDatabase.addObject().
Using factory.calculate() and getting the line object reference for use later on in the script is like this:
code:
var linefactory = moi.command.createFactory( 'line' );
linefactory.setInput( 0, moi.vectorMath.createPoint( 0,0,0 ) );
linefactory.setInput( 1, moi.vectorMath.createPoint( 10,10,10 ) );
var result_object_list = linefactory.calculate();
var line = result_object_list.item(0);
You could then assign a name using by doing
line.name = 'the line';
but you wouldn't normally do that as a lookup method later on, you would pass in the line variable that is an object reference.
It is possible to collect the output using the update()/commit()/cancel() method, to do that you would need to make sure factory.update() is called, then use factory.getCreatedObjects() before the .commit() :
code:
var linefactory = moi.command.createFactory( 'line' );
linefactory.setInput( 0, moi.vectorMath.createPoint( 0,0,0 ) );
linefactory.setInput( 1, moi.vectorMath.createPoint( 10,10,10 ) );
linefactory.update();
var result_object_list = linefactory.getCreatedObjects();
linefactory.commit();
var line = result_object_list.item(0);
line.name = 'the line';
But note that the line will be in the geometry database and so displayed in the viewports (the viewports draw the contents of the geometry database, they don't draw "loose objects" that are only referenced by script code and not listed in the geometry database).
If your script is generating the line as a building block with specific inputs instead of using a pointpicker the calculate() method is preferred.
Hope this makes sense, let me know if anything here isn't clear.
- Michael