Specifically I am try to get HDRI lighting of my sceen without using an HDRI image. I have HDRShop and the lightgen plugin to generate a text file describing the lights in RGB values and their position.
Note: I am very new to blender and not good at programming (in the process of learning). So please keep it as simple as possible.
It looks like your only choice is to do some Python programming, unless someone has already written something which understands your file.
It’s not too hard to do. You’ll want to visit the Blender Python API page..
The modules you’ll be most interested in are
Blender.Window (for the FileSelector() function)
os and Blender.sys (for opening and reading the file)
Blender.Lamp (for creating and modifying the light sources)
The most difficult thing to do will be parsing the line of text from the file. Look for the split() function in the Python Documentation Index.
pseudo code:
use Blender.Window.FileSelector() to get the name of the file to import
open the file
for each line in the file
parse it to get data
if data is a lamp then add lamp, then modify it’s position and color as appropriate
close the file Blender.Redraw() exit
This is my current attempt at programming with python for blender.
This script gives me errors and then crashes blender (???).
I am going to keep try to figure it out, but could use some help as this is my first python program.
The file opening/reading/looping part works in python as a seperate scipt (no blender stuff). The blender file open/light create and placement works in blender (not including the information that tells blender how to read the selected file.
I can’t seem to get them working together.
#!BPY
#
import Blender
from Blender import Window
import os
from os import path
#
def HDRI_LIGHTS(filename): # callback for the FileSelector
file = open(filename)
#Open only a text file created by Mediancut plugin for HDRShop
Window.FileSelector (HDRI_LIGHTS, "Open Mediancut Text File")
#
kills = 0 #counter to terminate loop
position = 0 #counter to keep place in list
line = file.readlines()
line = line[4:] #gets rid of junk at begining of file
for x in line:
#Looking for line in text that gives coord. of lights
if line[position][0:4] == 'Dir:':
kills = 0
#If light coord. are found they are stored as a list here
direction = line[position][4:].split()
position = position + 1
continue
#Looking for line in text that gives power of lights in RGB values
elif line[position][0:4] == 'Pow:':
kills = 0
#If light power is found it is stored here as a list
power = line[position][4:].split()
position = position + 1
else:
position = position + 1
kills = kills + 1
if kills == 1: #allows the loop to continue if only one blank line is found
continue
elif kills == 2: #terminates loop if two consecutive blank lines are found
break
else:
pass
#
scene = Blender.Scene.getCurrent () # get the current scene
ob = Blender.Object.New ('Lamp','Lamp'+str(position)) # make lamp object
l = Blender.Lamp.New () # make lamp data object
ob.link (l) # link lamp data with the object
scene.link (ob) # link the object into the scene
ob.setLocation (direction) # position the object in the scene
#
#
#
Blender.Redraw() # redraw the scene to show the updates.
exit
I am so glad when someone decides to learn Python. Bravo!
Just brought it to a working state with a few changes. Basically wrapped the main code as a function (which I named importlight) for the fileselector. I learnt something too! You will find some minor changes, take them as suggestions. Someone else can correct me too.
You should test if the file was really open before attempting to read it. Note that power values are still ignored.
import Blender
def HDRI_LIGHTS(filename): # callback for the FileSelector
importlight (filename)
#Open only a text file created by Mediancut plugin for HDRShop
Blender.Window.FileSelector (HDRI_LIGHTS, "Open Mediancut Text File")
def importlight(filename):
file = Blender.Text.Load(filename) # Using Blender's text module.
scene = Blender.Scene.getCurrent () # get the current scene
#Flags to know whether there is enough data to create new lamp.
gotdirection = gotpower = 0 Initialize to 0
#Counter for naming lamps
position = 0
for line in file.asLines():
#Looking for line in text that gives coord. of lights
data = line.split()
if len (data) == 4: #Attempt to read data.
key = data[0]
if key == 'Dir:':
#If light coord. are found they are stored as a list here
direction = [float(data[1]),float(data[2]),float(data[3])]
gotdirection =1
#Looking for line in text that gives power of lights in RGB values
elif key == 'Pow:':
#If light power is found it is stored here as a list
power = [float(data[1]),float(data[2]),float(data[3])]
gotpower = 1
if (gotpower & gotdirection): #Enough data to create lamp.
position +=1
ob = Blender.Object.New ('Lamp','Lamp'+str(position)) # make lamp object
l = Blender.Lamp.New () # make lamp data object
ob.link (l) # link lamp data with the object
scene.link (ob) # link the object into the scene
ob.setLocation (direction) # position the object in the scene
gotdirection = gotpower = 0 # Reset values.
Blender.Redraw() # redraw the scene to show the updates.
I will examine your code and see how it differs from mine. Also, I was able to fix and complete my code over the weekend (before I read your post) and it seems to work great.
All I have to do now is learn how to render with my new light settings.
Once I do I will write a tutorial and put it in a new post (with my python script). I think that this will be a very helpfull importer.
To all: Check out the link in my initial post to see what I am talking about.
Do you know how to set the “Ray Shadow” on using python script?
I tried using l.Modes(“Shadows”)
I didn’t get an error, but nothing appeared to happen. The “ray shadow” button was definitly not on.
Help Please??
See my most recent code below (this code works great except for the what I mentioned above).
#!BPY
""" Registration info for Blender menus:
Name: 'Mediancut light import (.txt)'
Blender: 237
Group: 'Import'
Tip: 'Imports Mediacut text files only'
"""
__author__ = "Cale Fallgatter (calegatter)"
"""
This file can be used freely. You may modify it, but may not distribute
the modification as a part of my file. Please give me credit where credit is due.
Thanks and I hope this works well for you
The program that creates the light data was written for HDRShop and was
written by Francesco Banterle
"""
import Blender
from Blender import Window
def HDRI_LIGHTS(filename): # callback for the FileSelector
file = open(filename)
#Open only a text file created by Mediancut plugin for HDRShop
kills = 0 #counter to terminate loop
position = 0 #counter to keep place in list
line = file.readlines()
line = line[4:] #gets rid of junk at begining of file
line = line +['
']+['
']+['
'] #add blank lines to end of file to avoid read error
power_total = []
#this for loop is used for presorting file to add total number of light pixels
#this will be used to give each lamp an emit power less than 1 so they total to 1
for x in line:
#Looking for line in text that gives coord. of lights
if line[position][0:4] == 'Dir:':
kills = 0
position = position + 1
continue
#Looking for line in text that gives power of lights in RGB values
elif line[position][0:4] == 'Pow:':
kills = 0
#If light power is found it is stored here as a list
power = line[position][4:].split()
power = [float(power[0]),float(power[1]),float(power[2])]
power_total[:0] = power
position = position + 1
continue
else:
position = position + 1
kills = kills + 1
if kills == 1: #allows the loop to continue if only one blank line is found
continue
elif kills == 2: #terminates loop if two consecutive blank lines are found
power_total_sum = sum(power_total)
break
else:
continue
kills = 0
position = 0
for x in line:
#Looking for line in text that gives coord. of lights
if line[position][0:4] == 'Dir:':
kills = 0
#If light coord. are found they are stored as a list here
direction = line[position][4:].split()
direction = [float(direction[0]),float(direction[1]),float(direction[2])]
position = position + 1
continue
#Looking for line in text that gives power of lights in RGB values
elif line[position][0:4] == 'Pow:':
kills = 0
#If light power is found it is stored here as a list
power = line[position][4:].split()
power = [float(power[0]),float(power[1]),float(power[2])]
power_sum = sum(power)
RGB = [power[0]/power_sum, power[1]/power_sum, power[2]/power_sum]
Energy = (power_sum/power_total_sum)*100
position = position + 1
else:
position = position + 1
kills = kills + 1
if kills == 1: #allows the loop to continue if only one blank line is found
continue
elif kills == 2: #terminates loop if two consecutive blank lines are found
break
else:
pass
scene = Blender.Scene.getCurrent () # get the current scene
ob = Blender.Object.New ('Lamp','Lamp'+str(position)) # make lamp object
l = Blender.Lamp.New () # make lamp data object
ob.link (l) # link lamp data with the object
scene.link (ob) # link the object into the scene
ob.setLocation (direction) # position the object in the scene
l.setCol(RGB) # sets light color
l.setEnergy(Energy) # sets light energy
l.setMode('Shadows')
Window.FileSelector (HDRI_LIGHTS, "Open Mediancut Text File")
#
#
Blender.Redraw() # redraw the scene to show the updates.