Script to assign colour to objects

Next
 From:  shayno
11193.1 
Hi Michael

Sorry but I'm still using ver 3
Is there a script to assign a specified colour to the selected objects please

Kind regards
shayne
  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
11193.2 In reply to 11193.1 
Hi shayne,

re:
> Is there a script to assign a specified colour to the selected objects please

In MoI objects do not have a colour set on them directly, the colors are set on a style and objects reference the style by their styleIndex property.

It is possible to make a script that creates a style with a specific name and color and then assign the selected objects to that style, do you want that?

- 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:  shayno
11193.3 In reply to 11193.2 
Thanks Michael
I know about the colour pallates which I currently use .

I add colour a lot of ring and setting parts gold or platinum and it would speed me up to assign a key to colours I use all the time

I would probably use some of the F keys that I don't use
a script that cycles through some preset colours would be awesome.

I did recently find the add default styles arrow which is great on old CADs that I drew years ago bringing them up to date with current colours

cheers
  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
11193.4 In reply to 11193.3 
Hi shayne,

re:
> I add colour a lot of ring and setting parts gold or platinum and it would speed me up to assign a key to colours I use all the time

Here's a script that can be put on a shortcut key to assign selected objects to a particular style.

This one will assign to the style named Red, change the name 'Red' to the one you want to target.


script: /* Assign selected objects to this style */ var name = 'Red'; var style = moi.geometryDatabase.findStyle( name ); if ( style ) moi.geometryDatabase.getSelectedObjects().setProperty( 'styleIndex', style.index );


- 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:  shayno
11193.5 In reply to 11193.3 
I would just want to set specific labels and colours in as presets in the script that cycles through them each key press

Once it is written for metals I am able to customise it for another key for gemstones

gold 207 194 37
platinum 145 145 145
silver 207 208 203
rose gold 214 141 92

Thanks
shayne
  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
11193.6 In reply to 11193.5 
Hi shayne, try pasting this in for the "command" part of a shortcut key:

code:
script: /* Cycle selection through colors */

var styles = [
	[ 'gold', 207, 194, 37 ], 
	[ 'platinum', 145, 145, 145 ], 
	[ 'silver', 207, 208, 203 ], 
	[ 'rose gold', 214, 141, 92 ]
];

/* Create styles if not currently existing */

var style_indexes = [];

for ( var i = 0; i < styles.length; ++i )
{
	var style = moi.geometryDatabase.findStyle( styles[i][0], true /*Create if not found*/ );
	style_indexes.push( style.index );
	var r = styles[i][1];
	var g = styles[i][2];
	var b = styles[i][3];
	style.color = ( (r << 16) | (g << 8) | b );
}

var objects = moi.geometryDatabase.getSelectedObjects();

if ( objects.length > 0 )
{
	var obj = objects.item(0);
	var current = -1;
	for ( var i = 0; i < style_indexes.length; ++i )
	{
		if ( obj.styleIndex == style_indexes[i] )
			current = i;
	}

	var next = (current + 1) % style_indexes.length;
	objects.setProperty( 'styleIndex', style_indexes[next] );
}



- 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:  shayno
11193.7 In reply to 11193.6 
Hi Michael
That works perfectly ,
I had to remove any returns to make it all one line as it would not past in
thanks so much 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
11193.8 In reply to 11193.7 
Hi shayne, I'm glad that was what you needed. For MoI version 4 or v5 the returns should be removed automatically when you paste it in.

- 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:  shayno
11193.9 In reply to 11193.8 
Thanks Michael
I am tempted to upgrade to ver4 but my ver3 is so custom now and full of scripts I feel it would be time consuming to change it .
I have previously reconfigured the sidepane to only have the commands I use and only 2 panels so not split for construct/ transform etc
I have colours I use for everything and a single key press I find is very fast to access them.
would it be difficult to script to hide a specific colour say r 207 g 194 b 37 ?
kind regards
shayne
  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
11193.10 In reply to 11193.9 
Hi shayne,


re:
> would it be difficult to script to hide a specific colour say r 207 g 194 b 37 ?

Try this:

code:
script: var r = 207, g = 194, b = 37;
var rgbcolor = (r << 16) | (g << 8) | b;
var style_index = -1;
var styles = moi.geometryDatabase.getObjectStyles();
for ( var i = 0; i < styles.length; ++i )
{
    var style = styles.item(i);
    if ( style.color == rgbcolor )
    {
         style_index = i;
         break;
    }
}

if ( style_index != -1 )
{
     var objs = moi.geometryDatabase.getObjects();
    for ( var i = 0; i < objs.length; ++i )
    {
        var obj = objs.item(i);
        if ( obj.styleIndex == style_index )
        {
            obj.hidden = 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
 From:  shayno
11193.11 In reply to 11193.10 
Hi Michael
Do you have a donation box so I can give you some money ?
cheers
shayne
  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