enzyme69 / blendersushi

Blender Sushi related scripts. Mostly about Sverchok, Geometry Nodes, Animation Nodes, and related Python scripts.
243 stars 33 forks source link

BSLIVE 0008 / Blender Automation for USDZ #427

Open enzyme69 opened 6 years ago

enzyme69 commented 6 years ago

TASK: Without opening Blender (running Blender in the background via Command Line and Python) create a setup that allows user to provide CUSTOM TEXT that will generate 3D mesh and output OBJ and USDZ for Augmented Reality (AR Quick Look, iOS 12, on iPhone or iPad).

PROCEDURES:

blender_auto_usdz.zip

enzyme69 commented 6 years ago

Actual intermediate BLEND file is like this:

screen shot 2018-07-28 at 11 47 09 am

It has Animation Nodes ready to update the 3D Text based on the external TEXT we supply.

enzyme69 commented 6 years ago

This is Python code that is running when Blender is summoned on the command line: blender -b mytext.blend -P BSexport.py

# BSexport.py
# SOURCE https://blenderartists.org/t/synchronizing-text-with-changes-from-outside/499187/5

import bpy
import os

def updateExternalTexts():
    """ Check modified external scripts in the scene and update if possible
    """
    ctx = bpy.context.copy()
    #Ensure  context area is not None

    ctx['area'] = ctx['screen'].areas[0]

    for t in bpy.data.texts:
        if t.is_modified and not t.is_in_memory:
            print("  *** Warning: Updating external script ***", t.name)
            # Change current context to contain a TEXT_EDITOR
            oldAreaType = ctx['area'].type
            ctx['area'].type = 'TEXT_EDITOR'
            ctx['edit_text'] = t

            bpy.ops.text.resolve_conflict(ctx, resolution='RELOAD')
            #print("---------everything works!")

            #Restore context
            ctx['area'].type = oldAreaType    

def exportOBJ():

    blend_file_path = bpy.data.filepath
    directory = os.path.dirname(blend_file_path)
    target_file = os.path.join(directory, 'mytext.obj')

    bpy.ops.export_scene.obj(filepath=target_file)

def convertUSDZ():
    os.system("xcrun usdz_converter mytext.obj mytext.usdz -v -a -l")

print("....updating text")            
updateExternalTexts()

print("....exporting OBJ")            
exportOBJ()

print("....converting OBJ to USDZ")            
convertUSDZ()