Rotating object by python


(hamster99) #1

Hello all,
I am working on a project to take a list of latitudes and longitudes and place a marker on a globe.
I have a script that runs , but the placement of the markers is not correct.
I am creating a sphere, and the rotating it so the position is at the center top of the sphere, I then create a cylinder at that location.

I hope this is intelligable :wink:

Please forgive the script, I have pieced it together by searching for code and useing bits.
If anyone can suggest a better method I would appreciate it.

The final version is to add a pointer at the location to poiint at the sun or moon.

the file is a comma delimited text file containing latitude, longitude, azimuth and elevation
I am not using azimutth or elevation yet

longitude and latitude are from -180 to 180 and -90 to 90 .

some of the code like float() was added when I got errors about parameters needing to be float type.

i am using values from latitudes -50 to 50 step 10 and all on longitude 80. The coding does not assume an ordered data file.



8import bpy
from math import radians
#
# create sphere location 0,0,0 add cylinders per file
#
print ("----------------start--------------")
bpy.ops.mesh.primitive_uv_sphere_add(location = (0,0,0))
cur = bpy.context.scene.objects.active
cur.name = "Earth"

#
fi = open("C:\ atemp\Formula\sun_position_calc\mylocations_rim_file.txt","r")
for lin in fi:
    p = lin.split(",")
    mlat = float(p[0])
    mlon = float(p[1])
    maz = float(p[2])
    mel = float(p[3])
    #
    # rotate the earth using z for longitude and x for latitude (use 90-mlat)
    #
    bpy.ops.object.select_all(action='DESELECT') #deselect all object
    erth = bpy.context.scene.objects["Earth"]
    erth.select = True
    bpy.ops.transform.rotate(value=radians(mlon), axis=(0, 0, 1.0)) 
    bpy.ops.transform.rotate(value=radians(90-mlat), axis=(1.0, 0, 0))
    bpy.ops.object.select_all(action='DESELECT') #deselect all object
    #
    # add cylinder pointer and parent
    #

    bpy.ops.mesh.primitive_cylinder_add(radius=0.02,depth=0.0,location=(0.0,0.0,1.0))
    cur = bpy.context.scene.objects.active
    print(cur.name,mlat,mlon)
     
   
    #
    # parent
    #
    b = bpy.data.objects['Earth']
    a = bpy.data.objects[cur.name]
    bpy.ops.object.select_all(action='DESELECT') #deselect all object
    a.select = True
    b.select = True     #select the object for the 'parenting'
    bpy.context.scene.objects.active = b   #the active object will be the parent of all selected object
    bpy.ops.object.parent_set(type='OBJECT', xmirror=False, keep_transform=True)
    #
    # select earth sphere and set back to 0 all rotations
    #
    bpy.ops.object.select_all(action='DESELECT') #deselect all object
    erth.select = True
    bpy.ops.transform.rotate(value=0.0, axis=(0, 0, 1))
    bpy.ops.transform.rotate(value=0.0, axis=(1, 0, 0))
    bpy.ops.object.select_all(action='DESELECT') #deselect all object



(hamster99) #2

Update: not sure what I did but I made some changes and it now seems to work. erth.rotation_euler = (0, 0, 0) at the end of the loop.

I am now adding the pointer though this code

mypath = “C:\\mytemp\blender\obj\pointer.obj”
bpy.ops.import_scene.obj(filepath=mypath, filter_glob=".obj;.mtl")

which works, but there is something wrong with selecting and positiong it.