How to use a variable to change the color attribute of the sphere command ?

20 views
Skip to first unread message

Pete

unread,
Jan 4, 2018, 9:39:33 AM1/4/18
to VPython-users
This is my code :-

for i in range(53):
    planet = objekt[i]
    col = colour[i]
    col = 'vector'+col[1:3]+','+col[3:4]+','+col[4:6]
    print planet, col
    planet = sphere(pos=vector(xcoord[i]*KM, ycoord[i]*KM, zcoord[i]*KM), radius=radii[i], color=col)

objekt is an array filled with planet names, colour is an array filled with color attributes e.g. (1,0,0) which is RED

I put the print statement in to see if the correct instruction is being set for color=, but I get the following :-

>>> 
Sun vector(1,1,0)

Traceback (most recent call last):
  File "C:\Python27\Planet Orbits\SolarSystem-MajorandMinorPlanets(b).py", line 66
    planet = sphere(pos=vector(xcoord[i]*KM, ycoord[i]*KM, zcoord[i]*KM), radius=radii[i], color=col)
  File "C:\Python27\Lib\site-packages\visual_common\primitives.py", line 93, in __init__
    self.process_init_args_from_keyword_dictionary( keywords )
  File "C:\Python27\Lib\site-packages\visual_common\primitives.py", line 192, in process_init_args_from_keyword_dictionary
    self.color = keywords['color']
ArgumentError: Python argument types in
    None.None(sphere, str)
did not match C++ signature:
    None(class cvisual::primitive {lvalue}, class cvisual::rgb)
>>>

Any ideas ? can this be done ? the other attributes work just fine.

Cheers,

Pete

Bruce Sherwood

unread,
Jan 4, 2018, 9:46:14 AM1/4/18
to VPython-users
You seem to be constructing a string and trying to assign the string to a color attribute, which expects a vector, not a string. Your colour array should contain vectors, not strings.

Bruce

Pete

unread,
Jan 4, 2018, 10:08:23 AM1/4/18
to VPython-users
Do you mean that I should have the value vector(1,0,0) in an array or just (1,0,0) ? I've tried the latter so I know that doesn't work, could you be a bit more specific Bruce please ?

Bruce Sherwood

unread,
Jan 4, 2018, 1:58:39 PM1/4/18
to VPython-users
Here's what I mean:

col = [color.red, color.blue, vector(0,1,0.7)]
box(color=col[2])

Since you're using the no longer supported VPython 6, you can omit the optional word "vector", but it makes sense to include it, since it's required in current versions of VPython.

Bruce
Reply all
Reply to author
Forward
0 new messages