Python Script Embedded in Rhino Button Issue

Hi All,

I have a downloaded a python script to convert material colours to object colours for the purpose of exporting to autocad. It works perfectly when running it from the python script interface through rhino, but for some unknown reason when I embedded that script within a custom rhino button it doesn’t seem to work.

I am a little unsure what is causing the issue as I have several custom buttons set-up in the same manner that work perfectly. Below is the original python code embedded in the rhino button.

! _-RunPythonScript (

import rhinoscriptsyntax as rs

def MaterialColorToObject():
ids = rs.GetObjects(“Select objects”,preselect=True)
if not ids:return

for id in ids:
    idx = rs.ObjectMaterialIndex(id)
    if idx == -1:
        color = (250,250,250)

if name == ‘main’:MaterialColorToObject()

)

Any advice on how to get it running through a rhino button would be greatly appreciated.

Cheers,
Michael

Hi @m.wilson,

Not at my computer but try removing if name == main and just call the function in that line instead.

import rhinoscriptsyntax as rs

def MaterialColorToObject():
ids = rs.GetObjects(“Select objects”,preselect=True)
if not ids:return

for id in ids:
    idx = rs.ObjectMaterialIndex(id)
    if idx == -1:
        color = (250,250,250)

MaterialColorToObject()

Thanks for the quick response @michaelvollrath. I have tried updating the script as per your suggestion and now I am getting an error message, see image below.
Script Button Error Message

I am not much of a coder, so I am not too sure how to correct these issues…

Cheers,
Michael

Hi @m.wilson,

Sorry edited that on my phone so the formatting was off and Python is “indent sensitive”:

after creating a definition, you need to indent the lines within that definition like so:

import rhinoscriptsyntax as rs

def MaterialColorToObject():
	ids = rs.GetObjects(“Select objects”,preselect=True)
	if not ids:
		return

	for id in ids:
		idx = rs.ObjectMaterialIndex(id)
		if idx == -1:
			color = (250,250,250)

MaterialColorToObject()

note how now after def MaterialColorToObject(): the next lines are indented and then the line at the bottom is not indented because it is calling that definition and not actually something inside the definition if that makes sense.

Anyways, give that properly indented code a go and if it doesn’t work I’ll look into it more specifically in relation to your command not working.

Cheers!

Hi @michaelvollrath, many thanks for that. I did notice the “expected indent” in the error message, I tested indenting a couple of the lines of code without any success. This is an interesting one, as I have never come across an issue with formatting when copy’n’pasting code in the past.

Even when copying the last formatted code you provided still had errors with the pasted formatting, which is interesting. I then went back to the original script within python and copied it again and edited as you initially suggested and it worked. I think I was always just coping your edits from the forum page which obviously was not retaining the original required formatting.

Anyway, it I have got it working now. Many thanks to your advice!

Your efforts are much appreciated.

Cheers,
Michael

1 Like

Hi @m.wilson , I’m glad you got it working!

Copy/pasting can be hit or miss for sure.

Cheers!