├── output_NqUkoL.gif ├── README.md └── main.py /output_NqUkoL.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paulchhuang/4DViewerBlender/HEAD/output_NqUkoL.gif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 4DViewerBlender 2 | 3 | The Python script in this repo demonstrates the following things in Blender software: 4 | 1. `Load a mesh sequence frame by frame.` 5 | 2. `Set the camera location and orientation.` 6 | 3. `Control the movement of light.` 7 | 8 | The users should know how to run Python scripts in Blender. A good tutorial can be found [here](https://en.wikibooks.org/wiki/Blender_3D:_Noob_to_Pro/Advanced_Tutorials/Python_Scripting/Introduction). 9 | 10 | ### Steps: 11 | 1. `Open an empty Blender project. Remove the default cube.` 12 | 2. `Modify the data path and camera parameters as needed.` 13 | 3. `Run the python script in Blender.` 14 | 4. `Click the render or animation button and you should have something like this:` 15 | ![image](https://github.com/paulchhuang/4DViewerBlender/blob/master/output_NqUkoL.gif) 16 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | import mathutils 3 | import math 4 | 5 | startFrame = 0 6 | endFrame = 150 7 | sequencePath = "path/2/objfiles" 8 | bpy.context.scene.frame_start = 0 9 | bpy.context.scene.frame_end = endFrame - startFrame 10 | current_Frame = 0 11 | 12 | bpy.data.objects["Camera"].location = mathutils.Vector((0.0, -4.5, 1)) 13 | bpy.data.objects["Camera"].rotation_euler = mathutils.Euler((math.radians(90.0),0.0, 0.0), 'XYZ') 14 | 15 | bpy.data.objects["Lamp"].location = mathutils.Vector((2.0, -5.0, 4)) 16 | bpy.data.objects["Lamp"].select = True 17 | bpy.data.objects["Lamp"].keyframe_insert(data_path='location', frame=startFrame) 18 | bpy.ops.transform.translate(value=(-4, 0, 0)) 19 | bpy.data.objects["Lamp"].keyframe_insert(data_path='location', frame=endFrame/2) 20 | bpy.ops.transform.translate(value=(4, 0, 0)) 21 | bpy.data.objects["Lamp"].keyframe_insert(data_path='location', frame=endFrame) 22 | 23 | bpy.ops.object.select_all(action = 'DESELECT') 24 | 25 | for i in range(startFrame,endFrame): 26 | file = "%s%04d.obj" % (sequencePath,i) 27 | mesh = bpy.ops.import_scene.obj(filepath = file,filter_glob="*.obj")#,axis_forward='-X', axis_up='Z') 28 | 29 | bpy.ops.object.shade_smooth() 30 | 31 | bpy.context.selected_objects[0].hide_render = True 32 | bpy.context.selected_objects[0].keyframe_insert(data_path = "hide_render",index=-1,frame = 0) 33 | bpy.context.selected_objects[0].keyframe_insert(data_path = "hide_render",index=-1,frame = current_Frame +1) 34 | bpy.context.selected_objects[0].hide_render = False 35 | bpy.context.selected_objects[0].keyframe_insert(data_path = "hide_render",index=-1,frame = current_Frame ) 36 | 37 | bpy.context.selected_objects[0].hide = True 38 | bpy.context.selected_objects[0].keyframe_insert(data_path = "hide",index=-1,frame = 0) 39 | bpy.context.selected_objects[0].keyframe_insert(data_path = "hide",index=-1,frame = current_Frame +1) 40 | bpy.context.selected_objects[0].hide = False 41 | bpy.context.selected_objects[0].keyframe_insert(data_path = "hide",index=-1,frame = current_Frame ) 42 | bpy.ops.object.select_all(action = 'DESELECT') 43 | 44 | current_Frame += 1 --------------------------------------------------------------------------------