Skip to content

Instantly share code, notes, and snippets.

@DaisukeMiyamoto
Created January 27, 2017 19:08
Show Gist options
  • Save DaisukeMiyamoto/5af65c1dd102624b69b2635894b380e8 to your computer and use it in GitHub Desktop.
Save DaisukeMiyamoto/5af65c1dd102624b69b2635894b380e8 to your computer and use it in GitHub Desktop.
# # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Blender python script for rendering x3d scenes
# Tobias Holzmann
# February 2016
# [email protected]
# # # # # # # # # # # # # # # # # # # # # # # # # # # #
import bpy
# Loop through all x3d files
# Change the number 500 to the timesteps you have
for num in range(5):
# Open the file
# Your file locations
file = 'G:/propeller/BlenderPropeller/x3d/blenderX3D_%d.x3d' % num
print('Render %s' % file)
bpy.ops.import_scene.x3d(filepath=file, axis_forward='X', axis_up='Z')
# Deselect everything
bpy.ops.object.select_all(action='TOGGLE')
# Delete not necessary stuff
for item in bpy.data.objects:
if item.type != "MESH":
bpy.data.objects[item.name].select = True
bpy.ops.object.delete()
else:
if ( item.name == "Cube" ):
bpy.data.objects[item.name].select = True
bpy.ops.object.delete()
# Rename the left data
bpy.data.objects["ShapeIndexedFaceSet"].data.name = 'WaterMesh'
bpy.data.objects["ShapeIndexedFaceSet.001"].data.name = 'PropellerMesh'
for obj in bpy.context.scene.objects:
if obj.name == 'ShapeIndexedFaceSet':
obj.name = 'Water'
if obj.name == 'ShapeIndexedFaceSet.001':
obj.name = 'Propeller'
bpy.data.objects["Water"].select = True
bpy.data.objects["Propeller"].select = True
bpy.ops.transform.resize(value=(12.0, 12.0,12.0))
bpy.ops.transform.translate(value=(0.0, 0.0, 2.0))
# Create a new plane (groundplane) and scale
bpy.ops.mesh.primitive_plane_add(radius=1, view_align=False, location=(0, 0, 0))
bpy.ops.transform.resize(value=(10,10,10))
bpy.ops.transform.translate(value=(0,0,-1.00))
# Create a new plane (light) move and rotate
bpy.ops.mesh.primitive_plane_add(radius=1, view_align=False, location=(0, 0, 0))
bpy.ops.transform.resize(value=(2,2,2))
bpy.ops.transform.translate(value=(1.5, 0.4, 3.0))
bpy.ops.transform.rotate(value=-0.523599, axis=(0,1,1))
bpy.ops.transform.resize(value=(1.2, 1.2, 1.2))
bpy.ops.transform.translate(value=(-1.2, 0, 4.0))
# Rename the left data
bpy.data.objects["Plane"].data.name = 'GroundMesh'
bpy.data.objects["Plane.001"].data.name = 'LightMesh'
for obj in bpy.context.scene.objects:
if obj.name == 'Plane':
obj.name = 'Ground'
if obj.name == 'Plane.001':
obj.name = 'Light'
# Deselect everything
bpy.ops.object.select_all(action='TOGGLE')
# Materials
#-------------------------------------------------------------
####################
### Activate Water
####################
# Select Water
bpy.data.objects['Water'].select = True
bpy.ops.object.shade_smooth()
bpy.context.scene.objects.active = None
bpy.context.scene.objects.active = bpy.data.objects["Water"]
ob = bpy.context.active_object
# Create material
mat = bpy.data.materials.new(name="MaterialWater")
# Assign it to object
if len(ob.data.materials):
# assign to 1st material slot
ob.data.materials[0] = mat
else:
# no solots
ob.data.materials.append(mat)
# Activated material -> cmat
cmat = ob.active_material
cmat.use_nodes = True
TreeNodes = cmat.node_tree
links = TreeNodes.links
# Remove nodes (clean it)
for node in TreeNodes.nodes:
TreeNodes.nodes.remove(node)
# Add the guy to the node view
# Output node
node_out = TreeNodes.nodes.new(type='ShaderNodeOutputMaterial')
node_out.location = 200, 0
# Glass BSDF
node_glass = TreeNodes.nodes.new(type='ShaderNodeBsdfGlass')
node_glass.location = 0, 180
node_glass.distribution = 'GGX'
node_glass.inputs['Color'].default_value = (1.0, 1.0, 1.0, 1)
node_glass.inputs['Roughness'].default_value = 0.0
node_glass.inputs['IOR'].default_value = 1.330
# Connect the guys
links.new(node_glass.outputs[0], node_out.inputs[0])
######################
### Activate Light
######################
bpy.context.scene.objects.active = None
bpy.context.scene.objects.active = bpy.data.objects["Light"]
ob = bpy.context.active_object
# Create material
mat = bpy.data.materials.new(name="MaterialLight")
# Assign it to object
if len(ob.data.materials):
# assign to 1st material slot
ob.data.materials[0] = mat
else:
# no slots
ob.data.materials.append(mat)
# Activated material -> cmat
cmat=ob.active_material
cmat.use_nodes=True
TreeNodes=cmat.node_tree
links = TreeNodes.links
# Remove nodes (clean it)
for node in TreeNodes.nodes:
TreeNodes.nodes.remove(node)
# Add the guy to the node view
# Output node
node_out = TreeNodes.nodes.new(type='ShaderNodeOutputMaterial')
node_out.location = 200,0
# Emission
node_emission = TreeNodes.nodes.new(type='ShaderNodeEmission')
node_emission.location = 0,0
node_emission.inputs[0].default_value = (1.0, 1.0, 1.0,1) # RGBA
node_emission.inputs[1].default_value = 9.0 # strength
# Connect the guys
links.new(node_emission.outputs[0], node_out.inputs[0])
######################
### Activate Ground
######################
bpy.context.scene.objects.active = None
bpy.context.scene.objects.active =bpy.data.objects["Ground"]
ob = bpy.context.active_object
# Create material
mat = bpy.data.materials.new(name="MaterialGround")
# Assign it to object
if len(ob.data.materials):
# assign to 1st material slot
ob.data.materials[0] = mat
else:
# no slots
ob.data.materials.append(mat)
# Activated material -> cmat
cmat=ob.active_material
cmat.use_nodes=True
TreeNodes=cmat.node_tree
links = TreeNodes.links
# Remove nodes (clean it)
for node in TreeNodes.nodes:
TreeNodes.nodes.remove(node)
# Add the guy to the node view
# Output node
node_out = TreeNodes.nodes.new(type='ShaderNodeOutputMaterial')
node_out.location = 200,0
# Toon
node_Toon = TreeNodes.nodes.new(type='ShaderNodeBsdfDiffuse')
node_Toon.location = 0,0
node_Toon.inputs[0].default_value = (1.0, 1.0, 1.0,1) # RGBA
#node_Toon.component = 'GLOSSY'
#node_Toon.inputs['Smooth'].default_value = 0.187 # strength
# Connect the guys
links.new(node_Toon.outputs[0], node_out.inputs[0])
######################
### Activate Propeller
######################
bpy.context.scene.objects.active = None
bpy.context.scene.objects.active =bpy.data.objects["Propeller"]
ob = bpy.context.active_object
bpy.ops.transform.resize(value=(1.001, 1.001, 1.001))
# Create material
mat = bpy.data.materials.new(name="MaterialPropeller")
# Assign it to object
if len(ob.data.materials):
# assign to 1st material slot
ob.data.materials[0] = mat
else:
# no slots
ob.data.materials.append(mat)
# Activated material -> cmat
cmat=ob.active_material
cmat.use_nodes=True
TreeNodes=cmat.node_tree
links = TreeNodes.links
# Remove nodes (clean it)
for node in TreeNodes.nodes:
TreeNodes.nodes.remove(node)
# Add the guy to the node view
# Output node
node_out = TreeNodes.nodes.new(type='ShaderNodeOutputMaterial')
node_out.location = 200,0
# Toon
node_Toon = TreeNodes.nodes.new(type='ShaderNodeBsdfDiffuse')
node_Toon.location = 0,0
node_Toon.inputs[0].default_value = (0.8,0.7,0.6,1) # RGBA
# Connect the guys
links.new(node_Toon.outputs[0], node_out.inputs[0])
######################
### Add a camera
######################
bpy.ops.object.camera_add(location=(0,0,0))
o = bpy.ops
o.transform.translate(value=(2.2,0.6,0.9))
o.transform.rotate(value=1.5708, axis=(0,0,1))
o.transform.rotate(value=1.3400, axis=(0,1,0))
o.transform.rotate(value=0.216889, axis=(0,0,1))
o.transform.rotate(value=0.0872664, axis=(0,0,1))
bpy.data.objects["Camera"].rotation_euler[0] = 1.18682
o.transform.translate(value=(-1,0,0))
o.transform.rotate(value=0.785398, axis=(0,0,1))
o.transform.translate(value=(8.0,0.0,0))
o.transform.translate(value=(0,8.0,0))
o.transform.translate(value=(0,0,10.0))
bpy.data.objects["Camera"].rotation_euler[0] = 0.890118
bpy.data.objects["Camera"].rotation_euler[2] = 2.44346
############
# For saving
############
# for scene in bpy.data.scenes:
# scene.render.resolution_x = 1000
# scene.render.resolution_y = 500
# scene.render.resolution_percentage = 50
# scene.render.use_border = False
cam = bpy.data.objects['Camera']
bpy.context.scene.camera = cam
bpy.context.scene.cycles.sampels = 120
bpy.context.scene.cycles.use_progressive_refine = True
bpy.data.scenes['Scene'].render.engine = 'CYCLES'
bpy.data.scenes['Scene'].render.filepath = 'G:/propeller/BlenderPropeller/images/blended_%02d.png' % num
bpy.ops.render.render( write_still = True )
# Delete everything
bpy.ops.object.select_all(action='TOGGLE')
bpy.ops.object.select_all(action='TOGGLE')
bpy.ops.object.delete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment