import bpy
import os
import math
from math import *

scn = bpy.context.scene

# Change to current directory
filepath = os.path.dirname(os.path.realpath(__file__))
dname = (os.path.dirname(filepath))
os.chdir(dname)


# Create base material
mat = bpy.data.materials.new('Base')

mat.use_nodes = True
nodes = mat.node_tree.nodes
links = mat.node_tree.links

texcoordnode = nodes.new(type = 'ShaderNodeTexCoord')
texcoordnode.location = -500,0

teximagenode = nodes.new(type = 'ShaderNodeTexImage')
teximagenode.location = -250,0
#impath = galimage
#image = bpy.data.images.load(impath)
#teximagenode.image = image

emnode = nodes.new(type = 'ShaderNodeEmission')
emnode.inputs[1].default_value = 5.0
emnode.location = -50.0,100.0

rgbbwnode = nodes.new(type = 'ShaderNodeRGBToBW')
rgbbwnode.location = -50.0,-50.0

lightnode = nodes.new(type = 'ShaderNodeLightPath')
lightnode.location = -50.0,400.0

transnode = nodes.new(type = 'ShaderNodeBsdfTransparent')
transnode.location = 150.0,400.0

mixnode1 = nodes.new(type = 'ShaderNodeMixShader')
mixnode1.location = 150.0,200.0

mixnode2 = nodes.new(type = 'ShaderNodeMixShader')
mixnode2.location = 350.0,325.0

# Created by default with a new material - just move this
matoutnode = nodes.get('Material Output')
matoutnode.location = 550.0,325.0

# Also created by default but needs to be deleted
diffnode = nodes.get('Diffuse BSDF')
nodes.remove(diffnode)

# Setup links between nodes
links.new(texcoordnode.outputs[0], teximagenode.inputs[0])
links.new(teximagenode.outputs[0], emnode.inputs[0])
links.new(teximagenode.outputs[0], rgbbwnode.inputs[0])
links.new(rgbbwnode.outputs[0], mixnode2.inputs[0])
links.new(lightnode.outputs[0], mixnode1.inputs[0])
links.new(emnode.outputs[0], mixnode1.inputs[2])
links.new(transnode.outputs[0], mixnode2.inputs[1])
links.new(mixnode1.outputs[0], mixnode2.inputs[2])
links.new(mixnode2.outputs[0], matoutnode.inputs[0])

# Assing base material to base mesh
ob = bpy.data.objects['Base']
ob.data.materials.append(mat)


infile = open('BlenderData_A70.txt','r')

#Skip header
infile.readline()

n = 0

for line in infile:
    n = n + 1
    if n < 5000:
        print(n)
        galname, rad, ra, dec, vel = line.split()
        
        galimage = dname+'/Images/'+galname+'.jpg'
        if str(os.path.isfile(galimage)) == 'True':
        
            R = radians(float(ra))
            D = radians(90.0-float(dec))
            distance = float(vel)/71.0
        
            radius = 0.5*10.0*((2.0*float(rad)/3600.0)/360.0)* 2.0*pi*distance
            x = distance * cos(R) * sin(D)
            y = distance * sin(R) * sin(D)
            z = distance * cos(float(D))
        
            #bpy.ops.mesh.primitive_plane_add(radius=radius, location=(x,y,z))
            #ob = bpy.context.object
            me = bpy.context.object.data
            me_copy = me.copy()
            ob = bpy.data.objects.new("Mesh Copy", me_copy)
            ob.name = galname
            
            ob.location[0] = x
            ob.location[1] = y
            ob.location[2] = z
            ob.scale[0] = radius
            ob.scale[1] = radius
            ob.scale[2] = radius
            
            # Duplicate base material, load image texture
            mat = bpy.data.materials['Base'].copy()
            mat.name = galname
            
            nodes = mat.node_tree.nodes
            teximagenode = nodes.get('Image Texture')

            impath = galimage
            image = bpy.data.images.load(impath)
            teximagenode.image = image
            
            # By default, object will have no existing material slots, so appending is the easiest
            # If it DID have slots we would use 'ob.data.materials[0] = mat'
            #ob.data.materials.append(mat)
            ob.data.materials[0] = mat
            
            scn.objects.link(ob)
        
 
infile.close()

bpy.context.scene.update()