ptrvilya / blendify

Lightweight Python framework that provides a high-level API for creating and rendering scenes with Blender.
https://virtualhumans.mpi-inf.mpg.de/blendify/
GNU General Public License v3.0
408 stars 12 forks source link

Hello, how to render fbx? #4

Closed lucasjinreal closed 1 year ago

lucasjinreal commented 1 year ago

Specifically, how to cooperated with blender load fbx API and send to sence.add_mesh for rendering?

bpy.ops.import_scene.fbx(filepath=model_path)
vguzov commented 1 year ago

Hello, as mentioned in the previous issue #1, Blendify does not handle mesh loading, only the rendering part. The easiest way you can load static .fbx mesh is to convert it to .obj and load it with trimesh, as shown in our example 03.

Another way is to preload and set colors/materials for your .fbx in Blender itself and save it to .blend file, after that you can attach it to Blendify scene with scene.attach_blend(filepath) and use Blendify to set up the rest (cameras, lights, other objects).

# Import the scene from blendify to initialize it
from blendify import scene

# Attach your preloaded mesh in .blend file
scene.attach_blend("/path/to/blend")

# Add lights and set camera
scene.lights.add_sun()
scene.set_perspective_camera((512, 512), fov_x=0.7, 
            quaternion=(0.82, 0.42, 0.18, 0.34), translation=(5, -5, 5))

# Render as usual
img = scene.render()

Also, you can use direct bpy calls in the same script as Blendify:

# Import the scene from blendify to initialize it
from blendify import scene

# Use direct bpy calls to load the FBX mesh.
# Note: meshes made with directs calls will not appear in blendify's scene,
# so materials and colors for them must be created though bpy calls as well
import bpy
bpy.ops.import_scene.fbx(filepath=model_path)
material = bpy.data.materials.new(name="mat")
material.use_nodes = True
colors_node = material.node_tree.nodes.new('ShaderNodeRGB')
colors_node.outputs["Color"].default_value = (0.3, 0.4, 0.5, 1.0)
material.node_tree.links.new(bsdf_node.inputs["Base Color"], colors_node.outputs['Color'])
obj = bpy.data.objects["<your FBX object name>"]
obj.active_material = material

# Add lights and set camera
scene.lights.add_sun()
scene.set_perspective_camera((512, 512), fov_x=0.7, 
            quaternion=(0.82, 0.42, 0.18, 0.34), translation=(5, -5, 5))

# Render as usual
img = scene.render()

Please keep in mind that objects created with direct calls or attached with .attach_blend() will not appear in scene.renderables, so you'll have to set them up manually before rendering.

lucasjinreal commented 1 year ago

@vguzov thank u for the script. I figured it out using bpy, but I got some other troubles, may I ask u about it?

  1. does your scene.render() invokes a saving to local operation or directly get img to numpy array from render GPU?
  2. I wannt using SPIN or PARE or HybrIK's camera output to project the mesh on image, do u know how do using setup the right perpective Camera for every single frame?
vguzov commented 1 year ago

Hello,

  1. scene.render always creates a file (this is a limitation of Blender), but in case no filepath parameter is supplied, it writes the file into temporary folder, immediately reads it and returns as a result.
  2. Unfortunately, I am not familiar with camera outputs of the mentioned systems.

Closing this issue as the initial problem is solved.