Import text file data? (light RGB and Position data)

Is there a way to import data from text files such as point could data or multiple lights (as used here http://gl.ict.usc.edu/research/MedianCut/)?

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

Hope this helps.

Thanks for the insight. I will play around with writing a script and see what I can come up with.

If I am successful, I will post it.

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

The text file I am trying to read is below

Generated by MedianCut
written by Francesco Banterle

Number_light: 256
Dir: 0.305760 0.951435 0.035809
Pow: 294.799133 173.242188 298.415405

Dir: 0.470962 0.881921 0.020241
Pow: 403.987915 334.567444 562.850464

Dir: 0.508792 0.860867 0.006244
Pow: 454.223114 395.994690 706.367737

Dir: 0.497908 0.854558 0.147709
Pow: 452.709595 409.175446 832.979065

Dir: 0.565721 0.824589 0.003471
Pow: 487.963379 448.139984 887.069702

PLEASE HELP

Oh and thanks to Duoas for getting me started and pointing me in the right direction. :smiley:

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.

Thanks for the help.

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.

tolobán or anyone with an answer,

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.

Please post python question in the python section.
and no crosspost.
Thanks

Continue this thread here:
https://blenderartists.org/forum/viewtopic.php?t=56788
[locked]