0
$\begingroup$

CONTEXT : I'm building a website where users can upload many files. [WEB SERVER, no gpu]

WHAT I WANT : that an uploaded .obj => render an image preview as a .png

WHY : to manage a library of multiple files so the image is to have a preview, imagine a multimedia library like google photo, but with videos, audios, photos, 3d files (with their thumbnail), ETC.

I CAN : with aasimp : I can easily convert from .obj, .fbx..... to .glb or .gltf [but aasimp have no solution to render an image from a 3d file]

I ALSO CAN : render 1 frame to png (( but only from a .blend ))

SEE HOW I SUCCESSFULLY INSTALLED BLEND + RENDER .blend TO .PNG ON LINUX : https://almalinux.discourse.group/t/how-to-install-blender-3d-withtout-snap-and-without-a-desktop/526

QUESTION : is it possible to invoke from the command line (linux web server without a desktop...) to create a new .blend and import a .obj or .fbx into that new .blend and than I can do my render?

WHY THE QUESTION : the command "--render-frame" accepts only .blend so the only "missing" stuff I need is to convert a .obj or .fbx from the command line (shell or python script) to convert to .blend

when I TRY this

xvfb-run -a --server-args="-screen 0, 1280x720x24" /opt/blender293/blender pathtoOBJ.obj --render-frame 1 -F PNG -b

I get this

Error: File format is not supported in file 'pathtoOBJ.obj'

when I do the same command with a .blend it's working fine see this. This image was generated from a .blend via a linux COMMAND LINE (shell) without GPU, without desktop enter image description here

$\endgroup$
1
$\begingroup$

One way would be to write a Python script that reads the Blender command line to find the obj file and then invokes the obj importer, and then creates the render.

You would invoke the script with a command like

blender [other blender args] --python YourScript ObjFileName

See the answer to this question for info on how to parse the command line from python.

The command to import a obj file is

bpy.ops.import_scene.obj(filepath="", filter_glo...bal_clamp_size=0, axis_forward='-Z', axis_up='Y')

so you might want your script to parse options to set the forward and up axis.

You will then have to set up the scene by adding lighting and a camera. That will look something like:

light_data = bpy.data.lights.new('light', type='POINT')
light = bpy.data.objects.new('light', light_data)
bpy.context.collections.objects.link(light)

cam_data = bpy.data.cameras.new('camera')
cam = bpy.data.objects.new('camera', cam_data)
bpy.context.collection.objects.link(cam)
scene = bpy.context.scene
scene.camera=cam

Finally you'll have to set up your render and save it with code along these lines

scene = bpy.context.scene
scene.render.image_settings.file_format='PNG'
scene.render.filepath='/tmp/blender.png'
bpy.ops.render.render(write_still=1)

I've left out a lot of detail, like camera and light placement, as well as setting the resolution and there are other things like render.filepath that you might want to take from the command line. I completely skipped adding materials to your object, by the way.

But it'll point you in the right direction.

$\endgroup$
2
  • $\begingroup$ Thanks for this good starting point, I'll do some testing and get back with the result. $\endgroup$
    – Jintor
    Sep 22 at 3:45
  • $\begingroup$ Hi, I concocted 2 python. 1 to save (.obj to .blend) and an other from your post (.obj to .png) see my answer $\endgroup$
    – Jintor
    Sep 25 at 2:51
0
$\begingroup$

FROM OBJ TO .BLEND

I concocted those (working yay)

•••••• SAVE .blend

Command /usr/bin/xvfb-run -a --server-args="-screen 0, 1280x720x24" /opt/blender293/blender --python /path/objtoblend.py -- /path/sample3.obj /path/95.blend -b

import bpy
import sys
argv = sys.argv

bpy.ops.import_scene.obj(filepath=argv[4])
obj_object = bpy.context.object

bpy.ops.wm.save_as_mainfile(filepath=argv[5])

bpy.ops.wm.quit_blender()

•••••• RENDER .obj only

command /usr/bin/xvfb-run -a --server-args="-screen 0, 1280x720x24" /opt/blender293/blender --python /path/blendobjrenderone.py -- /path/sample3.obj /pathto/render.png -b

import bpy
import sys
argv = sys.argv

bpy.ops.import_scene.obj(filepath=argv[4])
obj_object = bpy.context.object

bpy.context.scene.render.filepath=argv[5]
bpy.context.scene.render.image_settings.file_format='PNG'
bpy.ops.render.render(use_viewport = True, write_still=True)

bpy.ops.wm.quit_blender()
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.