Playing around with Python

82 views
Skip to first unread message

Jack L. Northrup

unread,
Jun 7, 2015, 3:22:13 PM6/7/15
to blende...@googlegroups.com
I wanted to add the bright named stars to the star field (the 4k sky from the texture resources).  After playing around in the python console, I was able to put together a little script form that I could populate using Word's Mail Merge.  After the first run I discovered that as the sky moved the names became unreadable.  So I added a section on the "Track_to" constrain to the camera and alignments.

bpy.ops.object.text_add(view_align=False, enter_editmode=False, location=(«Loc_x», «Loc_y», «Loc_z»), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False));

ob=bpy.context.object;ob.data.body = "«Name»";

bpy.ops.object.constraint_add(type='TRACK_TO');

bpy.context.object.constraints["Track To"].target = bpy.data.objects["00-fisheyeRig"];

bpy.context.object.constraints["Track To"].up_axis = 'UP_Y';bpy.context.object.constraints["Track To"].track_axis = 'TRACK_Z'

«Next Record»


Define variables:
Loc_x    Location x
Loc_y    Location y
Loc_z    Location z
Name    Text that will be used in the text object

Circumpolar Stars with names Video

Ron Proctor

unread,
Jun 7, 2015, 5:12:53 PM6/7/15
to blende...@googlegroups.com, jack.l....@gmail.com
Dude—that's some nice work, right there. I like your "spotlight" technique to keep the data from getting too crazy.

How did you get x,y,z positions? Can this be approached with angular position information (I suppose an object could be generated then rotated for angular positioning—not very efficient though).

Jack L. Northrup

unread,
Jun 8, 2015, 9:01:56 AM6/8/15
to blende...@googlegroups.com, jack.l....@gmail.com
Getting the coordinates was a little math magic.  My list of stars was in Excel and it had the RA and Dec.  I set my star names to be 25 away from the camera because it makes it a good size without having to add a size command to the python.

Convert RA & Dec to (x, y, z)
1.  RA has to be converted to Degrees
     Degree = -(((Hr+(Arcmin/60)*360)/24)
2.  Calculate the Location z
     z = 25*sin(Dec)
3.  Calculate the Location y
     y = 25* | (cos(Dec)) | *cos(Degree)
4.  Calculate the Location x
    x = 25* | (cos(Dec)) | *sin(Degree)

Ron Proctor

unread,
Jun 8, 2015, 12:29:58 PM6/8/15
to blende...@googlegroups.com, jack.l....@gmail.com
Nice. So with distance and magnitude data, one could populate the stars in 3D space!

Scott Sumner

unread,
Jun 8, 2015, 12:45:32 PM6/8/15
to blende...@googlegroups.com
I just started working on that thanks to Jack's script!  I'll post my results as soon as I have some

Scott


--
You received this message because you are subscribed to the Google Groups "Blendertarium" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blendertariu...@googlegroups.com.
Visit this group at http://groups.google.com/group/blendertarium.
To view this discussion on the web visit https://groups.google.com/d/msgid/blendertarium/18d96e0d-dee4-4c42-8aec-5e0d0292744c%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Jack L. Northrup

unread,
Jun 8, 2015, 3:01:00 PM6/8/15
to blende...@googlegroups.com, jack.l....@gmail.com
Yes but the hard part is to parent them to an empty for pitch, yaw, and turn individually because sometimes it will want to flip the z axis.

Ron Proctor

unread,
Jun 8, 2015, 3:30:53 PM6/8/15
to blende...@googlegroups.com
If you want to attack it with transformations, maybe there's a way to reference the cursor coordinates as the pivot point.

On Mon, Jun 8, 2015 at 1:00 PM, Jack L. Northrup <jack.l....@gmail.com> wrote:
Yes but the hard part is to parent them to an empty for pitch, yaw, and turn individually because sometimes it will want to flip the z axis.

--
You received this message because you are subscribed to the Google Groups "Blendertarium" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blendertariu...@googlegroups.com.
Visit this group at http://groups.google.com/group/blendertarium.

Scott Sumner

unread,
Jun 8, 2015, 3:55:22 PM6/8/15
to blende...@googlegroups.com
Yeah, you have to create an empty at 0,0,0, parent the sphere (star) to the empty, do the rotation, then unparent it.

Scott


Scott Sumner

unread,
Jun 8, 2015, 4:13:07 PM6/8/15
to blende...@googlegroups.com
I did a quick test and got stars in a sphere, I'm generating the whole dataset now.  Looks like it'll take about 15 minutes.  Then we can look and see if we have constellations :-)

Scott

Jack L. Northrup

unread,
Jun 8, 2015, 4:23:27 PM6/8/15
to blende...@googlegroups.com
That would be a cool thing to check out.  I would like to make it so it can auto assign a material from the materials list.  I was able to make it assign a material, but it made a material for each name.

I found this section of code to be useful
http://blender.stackexchange.com/questions/8475/python-set-material-to-material-slot

Scott Sumner

unread,
Jun 8, 2015, 4:44:51 PM6/8/15
to blende...@googlegroups.com
Yeah, the key is to keep your own list of materials.  Since tuples in Python can be keys here's the pseudo code:

materials = list()
for object in listOfThingsToMake:
     [make the things]
     desiredMaterial = [the color this thing should be]
     if desiredMaterial in materials:
          [code from stack overflow to assign materials]
     else:
          [Make a new material with color (R,G,B)]
          materials.append ( ( R , G , B ) )

This way if it exists you assign the one already created.  If not you create it and add it to your list for next time.  That will reuse as many materials as possible

Scott


--
You received this message because you are subscribed to the Google Groups "Blendertarium" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blendertariu...@googlegroups.com.
Visit this group at http://groups.google.com/group/blendertarium.

Scott Sumner

unread,
Jun 8, 2015, 5:16:03 PM6/8/15
to blende...@googlegroups.com
So the blend file became 254 meg and I stopped it after about 5000 stars.  I'll create one tonight with just the script and everyone can generate their own starfield.  Mine did crash blender when I tried to render...

SCott

Jack L. Northrup

unread,
Jun 8, 2015, 10:18:24 PM6/8/15
to blende...@googlegroups.com
Very cool!  Are you using UVSpheres?  A couple of times that my students were very vertex heavy on items that you couldn't see the detail were able to switch over to icosphere subdivision 1 only has 12 vertexes.

bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=1, size=1, view_align=False, enter_editmode=False, location=(-5.49504, -5.43604, 5.68324), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))

Ron Proctor

unread,
Jun 9, 2015, 7:28:27 AM6/9/15
to blende...@googlegroups.com

It would be more code, but if you can place vertices instead of geometry, you can use something like dupliverts to reference a small set of geometry and possibly save on file size. You might even be able to control the individual sizes programmatically.

Come to think of it: under this approach, the appearance of the stars could be dynamically changed.

On Jun 8, 2015 8:18 PM, "Jack L. Northrup" <jack.l....@gmail.com> wrote:
Very cool!  Are you using UVSpheres?  A couple of times that my students were very vertex heavy on items that you couldn't see the detail were able to switch over to icosphere subdivision 1 only has 12 vertexes.

bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=1, size=1, view_align=False, enter_editmode=False, location=(-5.49504, -5.43604, 5.68324), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))

--
You received this message because you are subscribed to the Google Groups "Blendertarium" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blendertariu...@googlegroups.com.
Visit this group at http://groups.google.com/group/blendertarium.

Scott Sumner

unread,
Jun 9, 2015, 3:19:17 PM6/9/15
to blende...@googlegroups.com
Worked like a charm!  I hit the "run script" button and the starfield almost immediately appeared.  Now the trouble is that I can't get dupliverts to render anything.  I'm using the "classic" fisheye camera.  I think in the orientation I left it in Cygnus / Northern Cross is right behind the camera in the viewport?  Not sure though.  Blend file attached for your review

Scott


starfield generator3.blend

Jack L. Northrup

unread,
Jun 9, 2015, 4:59:33 PM6/9/15
to blende...@googlegroups.com
The new project of the day was on a similar track but with textures.  I got it to add the plane, add the material, add the texture, but not link in the image.  It looks like it is the the portion of the code that finds the image on the drive, I am running a Mac.  After that command the rest of the code falls to bits. 

bpy.ops.material.new()
new_mat = bpy.data.materials[-1]  # the new material is the last one in the list
new_mat.name = "NAME"
bpy.ops.object.material_slot_add()
bpy.context.object.active_material = new_mat
bpy.context.object.active_material.use_shadeless = True
bpy.ops.texture.new()
cTex = bpy.data.textures.new('ColorTex', type = 'IMAGE')
realpath = os.path.expanduser(“~/DOCUMENTS/BLENDERFILES/IMG_2924.JPG”)
img = bpy.data.images.load(realpath)
cTex.image = bpy.data.images.load(img)

This is the feedback I get back from the console


cTex = bpy.data.textures.new('ColorTex', type = 'IMAGE')
>>> realpath = os.path.expanduser(“~/DOCUMENTS/BLENDERFILES/IMG_2924.JPG”)
  File "<blender_console>", line 1
    realpath = os.path.expanduser(“~/DOCUMENTS/BLENDERFILES/IMG_2924.JPG”)
                                                         ^
SyntaxError: invalid character in identifier

>>> img = bpy.data.images.load(realpath)
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
NameError: name 'realpath' is not defined

>>> cTex.image = img
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
NameError: name 'img' is not defined

IMG_2924.JPG

Scott Sumner

unread,
Jun 9, 2015, 6:46:01 PM6/9/15
to blende...@googlegroups.com
Try adding an r in front of the double quotes:

r"path/to/my/file"

That'll tell Python to treat it as a straight up string rather than interpreting slashes as the start of escape sequences.

Scott


--
You received this message because you are subscribed to the Google Groups "Blendertarium" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blendertariu...@googlegroups.com.
Visit this group at http://groups.google.com/group/blendertarium.

Jack L. Northrup

unread,
Jun 10, 2015, 3:50:44 PM6/10/15
to blende...@googlegroups.com
Thanks!
I gave the r method a try and it still kicked an error (I even found the path for the file from root),  I imported the posixpath to make sure python to path on a unix based system.


bpy.ops.material.new()
new_mat = bpy.data.materials[-1]  # the new material is the last one in the list
new_mat.name = "NAME"
bpy.ops.object.material_slot_add()
bpy.context.object.active_material = new_mat
bpy.context.object.active_material.use_shadeless = True
bpy.ops.texture.new()
cTex = bpy.data.textures.new('ColorTex', type = 'IMAGE')
#realpath = os.path.expanduser(r“/Users/894administrator/Documents/BlenderFiles/IMG_2924.JPG”)
#realpath = os.posixpath.expanduser(“%s/Documents/BlenderFiles/IMG_2924.JPG” % “/Users/894administrator”)
#realpath = os.posixpath.expanduser(r“~/Documents/BlenderFiles/IMG_2924.JPG”)
img = bpy.data.images.load(r“~/Documents/BlenderFiles/IMG_2924.JPG”)
cTex.image = img

bpy.ops.image.pack()


Scott Sumner

unread,
Jun 10, 2015, 3:53:39 PM6/10/15
to blende...@googlegroups.com
Python may not be able to expand the ~, I don't remember if that's mapped as an actual directory or just a shortcut.  Try putting it explicity, AKA /home/[username]/Documents/BlendeFiles...  so possibly /home/jack/Documents/BlenderFiles...

Scott


--
You received this message because you are subscribed to the Google Groups "Blendertarium" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blendertariu...@googlegroups.com.
Visit this group at http://groups.google.com/group/blendertarium.

Jack L. Northrup

unread,
Jun 10, 2015, 6:00:47 PM6/10/15
to blende...@googlegroups.com
I tried that and it still wasn't happy.  It still errors on the first character after the / in the root.
 So I am going to move on to my next project, three days is too long for a what if project.  I have enough of it working to do what I want it to do and I guess I will just have to go back and add the images later.  Thanks for all your help Scott.

Scott Sumner

unread,
Jun 15, 2015, 10:02:16 PM6/15/15
to blende...@googlegroups.com
All,

I've gotten Blender + Python to read a star catalog and generate a blender object with a vertex at each star location.  I also got it to duplicate an icosphere at the vertices for the stars.  But nothing shows up when I render...  I'm trying to use the fisheye rig, can someone check and see if I'm using the right rig / render engine etc.?  Blend file attached...

Thanks,
Scott

On Tuesday, June 9, 2015 at 2:19:17 PM UTC-5, Scott Sumner wrote:
Worked like a charm!  I hit the "run script" button and the starfield almost immediately appeared.  Now the trouble is that I can't get dupliverts to render anything.  I'm using the "classic" fisheye camera.  I think in the orientation I left it in Cygnus / Northern Cross is right behind the camera in the viewport?  Not sure though.  Blend file attached for your review

Scott

On Tue, Jun 9, 2015 at 6:28 AM, Ron Proctor <ronpr...@gmail.com> wrote:

It would be more code, but if you can place vertices instead of geometry, you can use something like dupliverts to reference a small set of geometry and possibly save on file size. You might even be able to control the individual sizes programmatically.

Come to think of it: under this approach, the appearance of the stars could be dynamically changed.

On Jun 8, 2015 8:18 PM, "Jack L. Northrup" <jack.l....@gmail.com> wrote:
Very cool!  Are you using UVSpheres?  A couple of times that my students were very vertex heavy on items that you couldn't see the detail were able to switch over to icosphere subdivision 1 only has 12 vertexes.

bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=1, size=1, view_align=False, enter_editmode=False, location=(-5.49504, -5.43604, 5.68324), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))

--
You received this message because you are subscribed to the Google Groups "Blendertarium" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blendertarium+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Blendertarium" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blendertarium+unsubscribe@googlegroups.com.
starfield generator3.blend

Jack L. Northrup

unread,
Jun 16, 2015, 9:38:50 AM6/16/15
to blende...@googlegroups.com
 I went into the fisheye camera settings and changed the clip from 1000 to 50,000,000.  I am wondering if the icospheres are too small at those distances and are getting lost between the pixels.  When I rendered the image the first time my computer indicated it was a peak 50MB file, after the change the file bumped up to 65MB.  So it is seeing the stars it just is not able to render them at that distance.

Scott Sumner

unread,
Jun 16, 2015, 9:44:41 AM6/16/15
to blende...@googlegroups.com
Oh OK, I'll play with it some more...  I shrunk the object itself so stars were within 10 grid squares of the camera and made the icosphere huge but I still wasn't able to get anything.

Scott


On Tue, Jun 16, 2015 at 8:38 AM, Jack L. Northrup <jack.l....@gmail.com> wrote:
 I went into the fisheye camera settings and changed the clip from 1000 to 50,000,000.  I am wondering if the icospheres are too small at those distances and are getting lost between the pixels.  When I rendered the image the first time my computer indicated it was a peak 50MB file, after the change the file bumped up to 65MB.  So it is seeing the stars it just is not able to render them at that distance.

--
You received this message because you are subscribed to the Google Groups "Blendertarium" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blendertariu...@googlegroups.com.

Jack L. Northrup

unread,
Jun 17, 2015, 6:32:00 PM6/17/15
to blende...@googlegroups.com
I was playing around and tried using cubes instead of icospheres and they still didn't show up.  I almost wonder if they camera has become corrupted.  Have you thought about reloading the data and making it create individual stars instead of parts of the same icosphere. 

PS have you tried to keyframe rotation?

Scott Sumner

unread,
Jun 17, 2015, 6:34:11 PM6/17/15
to blende...@googlegroups.com

I tried individual objects but the script took forever to run (an hour for 1000 stars) and crashed when I rendered with a 254 meg file size.

On Jun 17, 2015 5:32 PM, "Jack L. Northrup" <jack.l....@gmail.com> wrote:
I was playing around and tried using cubes instead of icospheres and they still didn't show up.  I almost wonder if they camera has become corrupted.  Have you thought about reloading the data and making it create individual stars instead of parts of the same icosphere. 

PS have you tried to keyframe rotation?

--
You received this message because you are subscribed to the Google Groups "Blendertarium" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blendertariu...@googlegroups.com.
Visit this group at http://groups.google.com/group/blendertarium.
Reply all
Reply to author
Forward
0 new messages