source: blender-povray-animation/frams-pov-import.py @ 549

Last change on this file since 549 was 499, checked in by Maciej Komosinski, 9 years ago

Fixed typos

File size: 12.8 KB
Line 
1bl_info = {
2    "name": "Framsticks POV import & camera manipulator",
3    "author": "Szymon Ulatowski, jm soler",
4    "blender": (2, 70, 0),
5    "location": "File > Import-Export",
6    "category": "Import-Export",
7    "description": "Imports POV-Ray files generated by Framsticks and exports Blender camera to POV-Ray files",
8    "wiki_url": "http://www.framsticks.com/3d-animations-in-blender"
9}
10
11import os, math, bpy, re, copy, functools
12from bpy_extras.io_utils import ImportHelper
13from bpy.props import (CollectionProperty, StringProperty, BoolProperty, EnumProperty, FloatProperty)
14
15re_field=re.compile('^#declare field_([^_]+)_([^=]+)=(.*)$')
16re_object=re.compile('^BeginObject\(\)$')
17re_part=re.compile('^BeginObject\(\)$')
18re_partgeo=re.compile('^PartGeometry\(<([^>]+)>,<([^>]+)>\)$')
19re_joint=re.compile('^BeginJoint\(([0-9]+),([0-9]+)\)$')
20re_jointgeo=re.compile('^JointGeometry\(<([^>]+)>,<([^>]+)>,<([^>]+)>\)$')
21re_neuro=re.compile('^BeginNeuro\(([^)])\)$')
22
23Folder = ""
24FirstPOV=-1
25LastPOV=-1
26CREATURES={}
27
28# assumes the filename is "*_number.pov"
29def numFromFilename(f):
30    try:
31        return int(f[f.find('_')+1:f.find('.')])
32    except:
33        return -1
34
35# creates a global dictionary of filenames ("*_number.pov"), where key=number, updates FirstPOV/LastPOV
36def scanDir(startfile):
37    global Files, Folder, FileName
38    global FirstPOV, LastPOV
39
40    Files={}
41    Folder, FileName=os.path.split(startfile)
42
43    #print("startfile=",startfile,"Folder=",Folder,"FileName=",FileName)
44
45    underscore=FileName.find("_")
46    if underscore==-1:
47        return
48    if FileName.find(".pov")==-1:
49        return
50
51    FirstPOV=numFromFilename(FileName)
52    LastPOV=FirstPOV
53
54    for f in os.listdir(Folder):
55        if not f.endswith('.pov'):
56            continue
57        if not f.startswith(FileName[:underscore+1]):
58            continue
59        num=numFromFilename(f)
60        if num<0:
61            continue
62        Files[num]=f
63        if num>LastPOV:
64            LastPOV=num
65    #print("N=",len(Files))
66
67def extractValue(val):
68    if val[0]=='"':
69        # "string"
70        return val[1:-1]
71    if val.find('.')!=-1:
72       # floating point
73        return float(val)
74    else:
75       # integer
76       return int(val)
77
78def floatList(str):
79    return [float(x) for x in str.split(',')]
80
81def analysePOV(fname):
82    global re_field,re_object,re_part,re_partgeo,re_joint,re_jointgeo,re_neuro
83    f=open(fname,'r',encoding='latin-1')
84    POVlines=f.readlines()
85    f.close()
86    tmpfields={}
87    objects={}
88    for line in POVlines:
89        m=re_field.match(line)
90        if m:
91            value=m.group(3)
92            if value.endswith(';'):
93                value=value[:-1]
94            value=extractValue(value)
95            objname=m.group(1) # creature,m,p,j,n
96            fieldname=m.group(2)
97            if not objname in tmpfields:
98                tmpfields[objname]={}
99            tmpfields[objname][fieldname]=value
100            #print("obj=",m.group(1)," name=",m.group(2)," value=",value)
101        m=re_object.match(line)
102        if m:
103            objkey=tmpfields['Creature']['name']+'_'+tmpfields['Creature']['uid']
104            objkey=objkey.replace(' ','_')
105            recentobj={'fields':copy.deepcopy(tmpfields['Creature']),'parts':[],'joints':[],'neurons':[]}
106            objects[objkey]=recentobj
107        m=re_jointgeo.match(line)
108        if m:
109            joint={'StickLoc1':floatList(m.group(1)),
110                   'StickLoc2':floatList(m.group(2)),
111                   'StickRot':floatList(m.group(3)),
112                   'fields':copy.deepcopy(tmpfields['j'])}
113            #print(joint)
114            recentobj['joints'].append(joint)
115        m=re_partgeo.match(line)
116        if m:
117            part={'Loc':floatList(m.group(1)),
118                   'Rot':floatList(m.group(2)),
119                   'fields':copy.deepcopy(tmpfields['p'])}
120            #print(joint)
121            recentobj['parts'].append(part)
122    #print(tmpfields)
123    #print(objects)
124    #print(json.dumps(objects,indent=4))
125    return objects
126
127# vector length
128def vecLength(p1,p2):
129    p2[0]=p2[0]-p1[0]
130    p2[1]=p2[1]-p1[1]
131    p2[2]=p2[2]-p1[2]
132    return (p2[0]**2+p2[1]**2+p2[2]**2)**0.5
133
134def vecSub(p1,p2):
135    return [p1[0]-p2[0], p1[1]-p2[1], p1[2]-p2[2]]
136
137def vecMul(p1,m):
138    return [p1[0]*m, p1[1]*m, p1[2]*m]
139
140# create an object containing reference to blender cylinder object
141class cylindre:
142    def __init__(self,nom='cylinderModel',type='cyl',r1=0.1, r2=0.1,h=1.0, n=8,smooth=1):
143          me=bpy.data.meshes.new(name=nom)
144          r=[r1,r2]
145          verts=[]
146          for i in range(0,2):
147              for j in range(0,n):
148                  z=math.sin(j*math.pi*2/(n))*r[i]
149                  y=math.cos(j*math.pi*2/(n))*r[i]
150                  x=float(i)*h
151                  verts.append((x,y,z))
152
153          vlist=[v for v in range(n)]
154          vlist.append(0)
155          faces=[]
156          for i in range(n):
157              faces.append((vlist[i],vlist[i+1],vlist[i+1]+n,vlist[i]+n))
158
159          if type=='cyl':
160              pos=[[0.0,0.0,0.0],[0.0,0.0,h]]
161              verts.append((pos[0][0],pos[0][1],pos[0][2]))
162              verts.append((pos[1][0],pos[1][1],pos[1][2]))
163
164              for i in range(n):
165                  faces.append((vlist[i],vlist[i+1],len(vlist)-2))
166                  faces.append((vlist[i],vlist[i+1],len(vlist)-1))
167
168          me.from_pydata(verts,[],faces)
169          me.update()
170          self.objet=bpy.data.objects.new(nom,me)
171          bpy.context.scene.objects.link(self.objet)
172
173# build or update blender objects from a POV file
174def updateBlender(SceneParent,num):
175    global Folder, Files, Current
176    Incoming=analysePOV(os.path.join(Folder,Files[num]))
177    for oname,obj in Incoming.items():
178        if not oname in Current:
179            # add object properties
180            print('Creature added:',oname)
181            newobj=[] # will contain: [ parent, joint0, joint1, ... ]
182            Current[oname]=newobj
183            # create new blender objects
184            bcrea=bpy.data.objects.new(oname,None)
185            bpy.context.scene.objects.link(bcrea)
186            bcrea.parent=SceneParent
187            newobj.append(bcrea)
188            for j in obj['joints']:
189                cyl=cylindre(oname+'_j','tube',0.1,0.1,1.0,6)
190                cyl.objet.parent=bcrea
191                newobj.append(cyl.objet)
192
193        # update blender loc/rot/scale
194        existing_b=Current[oname]
195        if len(obj['joints']):
196            avg_loc=vecMul(functools.reduce(lambda a,b: [a[0]+b[0],a[1]+b[1],a[2]+b[2]], [j['StickLoc1'] for j in obj['joints']]),1/len(obj['joints']))
197        elif len(obj['parts']):
198            avg_loc=vecMul(functools.reduce(lambda a,b: [a[0]+b[0],a[1]+b[1],a[2]+b[2]], [p['Loc'] for p in obj['parts']]),1/len(obj['parts']))
199        else:
200            avg_loc=[0,0,0]
201        if len(existing_b)>0:
202            existing_b[0].location=avg_loc
203            existing_b[0].keyframe_insert(data_path='location',frame=bpy.context.scene.frame_current)
204        for i in range(len(obj['joints'])):
205            if i>=(len(existing_b)-1):
206                continue # number of joints has changed -> ignore
207            incoming_geo=obj['joints'][i]
208            #print('incoming:',incoming_geo)
209            bo=existing_b[i+1] # blender object
210            scale=[vecLength(incoming_geo['StickLoc1'],incoming_geo['StickLoc2']), 1.0, 1.0]
211            for xyz in [0,1,2]:
212                getattr(bo,'location')[xyz]=vecSub(incoming_geo['StickLoc1'],avg_loc)[xyz]
213                #getattr(bo,'location')[xyz]=incoming_geo['StickLoc1'][xyz]
214                getattr(bo,'rotation_euler')[xyz]=incoming_geo['StickRot'][xyz]
215                getattr(bo,'scale')[xyz]=scale[xyz]
216            for field in ['location','rotation_euler','scale']:
217                bo.keyframe_insert(data_path=field,frame=bpy.context.scene.frame_current)
218
219# import a sequence of POV files, create object hiererchy, animate
220def framsImport(startfile):
221       global  FirstPOV, LastPOV, Files
222       global  Folder, FileName
223       global  Current, FirstFrame, FrameCount
224       global  SceneParent
225       global  SkipFrames
226
227       scanDir(startfile)
228
229       if len(Files)<1:
230           print("No files found")
231           return
232
233       bpy.context.scene.frame_end=max(bpy.context.scene.frame_end,FirstFrame+FrameCount-1)
234
235       SceneParent=bpy.data.objects.new("Framsticks_"+str(FirstFrame),None)
236       bpy.context.scene.objects.link(SceneParent)
237       SceneParent.framspov_file=startfile
238       SceneParent.framspov_frame=FirstFrame
239       SceneParent.framspov_count=FrameCount
240
241       Current={}
242       NextSkip=0
243       for k in sorted(Files.keys()):
244           if k<NextSkip:
245               continue
246           NextSkip=k+SkipFrames
247           bpy.context.scene.frame_set(FirstFrame-FirstPOV+k)
248           if bpy.context.scene.frame_current >= FirstFrame+FrameCount:
249               break
250           print("Frame %d - loading POV %s" % (bpy.context.scene.frame_current,Files[k]))
251           updateBlender(SceneParent,k)
252
253###############################
254
255def povUpdateFile(filename,cam):
256    f=open(filename,'r',encoding='latin-1')
257    lines=f.readlines()
258    f.close()
259    for i in range(len(lines)):
260        line=lines[i]
261        if line.startswith('Camera('):
262            line='Camera(<%g,%g,%g>,<%g,%g,%g>)\n' % tuple(cam);
263            lines[i]=line
264    f=open(filename,'w',encoding='latin-1')
265    f.writelines(lines)
266    f.close()
267
268def framsCameraFromObj(obj):
269    #print(obj.location.x)
270    m=obj.matrix_local
271    return [obj.location.x, obj.location.y, obj.location.z, obj.location.x-m[0][2], obj.location.y-m[1][2], obj.location.z-m[2][2]]
272
273def povUpdateScene(obj):
274    global Folder,FirstFrame,FrameCount,Files,FirstPOV
275    print("Updating scene %s" % obj.name)
276    scanDir(obj.framspov_file)
277    if len(Files)<1:
278        print("No files found for "+obj.name)
279        return
280    FirstFrame=obj.framspov_frame
281    FrameCount=obj.framspov_count
282    for k in sorted(Files.keys()):
283        #bpy.context.scene.frame_current=FirstFrame-FirstPOV+k
284        bpy.context.scene.frame_set(FirstFrame-FirstPOV+k)
285        if bpy.context.scene.frame_current >= FirstFrame+FrameCount:
286            break
287        print("Frame %d - updating camera in %s" % (bpy.context.scene.frame_current,Files[k]))
288        cam=framsCameraFromObj(bpy.context.scene.camera)
289        cam[0]-=obj.location.x
290        cam[1]-=obj.location.y
291        cam[2]-=obj.location.z
292        cam[3]-=obj.location.x
293        cam[4]-=obj.location.y
294        cam[5]-=obj.location.z
295        povUpdateFile(os.path.join(Folder,Files[k]),cam)
296
297####################################
298
299class FramsticksPOVImporter(bpy.types.Operator, ImportHelper):
300    """Load a collection of Framsticks POV files"""
301    bl_idname = "framspov.import"
302    bl_label = "Import Framsticks POV"
303    bl_options = {'UNDO'}
304
305    files = CollectionProperty(name="File Path",
306                          description="File path used for importing",
307                          type=bpy.types.OperatorFileListElement)
308    directory = StringProperty()
309
310    framspov_skip = bpy.props.IntProperty(name="Frame step",min=1,max=100)
311
312    filename_ext = ".pov"
313    filter_glob = StringProperty(default="*.pov", options={'HIDDEN'})
314
315    def execute(self, context):
316        global FirstFrame, FrameCount,FileName,SkipFrames
317        FirstFrame = bpy.context.scene.frame_current
318        FrameCount = 9999
319        SkipFrames = self.framspov_skip
320        framsImport(os.path.join(self.directory, self.files[0].name))
321        return {'FINISHED'}
322   
323
324def menu_func_import(self, context):
325    self.layout.operator(FramsticksPOVImporter.bl_idname, text="Framsticks POV (.pov)")
326
327class OBJECT_PT_framspov(bpy.types.Panel):
328    bl_label = "Framsticks POV"
329    bl_space_type = "PROPERTIES"
330    bl_region_type = "WINDOW"
331    bl_context = "object"
332 
333    @classmethod
334    def poll(cls, context):
335        obj=context.object
336        return obj.framspov_file!=''
337 
338    def draw(self, context):
339        layout = self.layout
340        row = layout.row()
341        row.operator("framspov.updatecam",icon='SCRIPT')
342
343
344class VIEW3D_OT_UpdatePOVCamera(bpy.types.Operator):
345    bl_idname = "framspov.updatecam"
346    bl_label = "Update POV camera"
347 
348    def execute(self, context):
349        povUpdateScene(context.object)
350        return{'FINISHED'}
351
352def register():
353    bpy.types.Object.framspov_file=bpy.props.StringProperty(name="Name of the first POV file")
354    bpy.types.Object.framspov_frame=bpy.props.IntProperty(name="First frame",min=0,max=999999)
355    bpy.types.Object.framspov_count=bpy.props.IntProperty(name="Number of frames",min=0,max=999999)
356    bpy.utils.register_class(FramsticksPOVImporter)
357    bpy.utils.register_class(VIEW3D_OT_UpdatePOVCamera)
358    bpy.utils.register_class(OBJECT_PT_framspov)
359    bpy.types.INFO_MT_file_import.append(menu_func_import)
360
361def unregister():
362    bpy.utils.unregister_class(FramsticksPOVImporter)
363    bpy.utils.unregister_class(VIEW3D_OT_UpdatePOVCamera)
364    bpy.utils.unregister_class(OBJECT_PT_framspov)
365    bpy.types.INFO_MT_file_import.remove(menu_func_import)
366
Note: See TracBrowser for help on using the repository browser.