0
$\begingroup$

I've got an automated process that produces 100's of blend files for use in Unity. I need to change each of these files to fix some things. I've got other scripts that are ugly but get the job done, like renaming materials and object names and turn off auto smooth and double sided. Now I would like to generate a UV map and scale it to a real world size (I'm trying to use the Magic-UV add-on.) I've made this script:

import bpy

def ApplyWorldScaleUV():
    scene = bpy.context.scene
    scene.layers = [True] * 20 # Show all layers

    for obj in scene.objects:
        if obj.type == 'MESH':
            print(">>>> START >>>>     ApplyWorldScaleUV obj name: %s" %obj.name)       
            scene.objects.active = obj
            obj.select = True

            bpy.ops.object.mode_set(mode='EDIT')
            bpy.ops.mesh.select_all(action='SELECT')
            bpy.ops.uv.smart_project(angle_limit=66.0, island_margin=0.001, user_area_weight=0.0, stretch_to_bounds=False)

            bpy.ops.mesh.select_all(action='DESELECT')
            bpy.ops.mesh.primitive_cube_add()
            bpy.ops.uv.cube_project()
            bpy.ops.uv.muv_wsuv_measure()
            bpy.ops.mesh.delete(type='FACE')
            bpy.ops.mesh.select_all(action='SELECT')
            bpy.ops.uv.muv_wsuv_apply()

            bpy.ops.object.mode_set(mode='OBJECT')
            print(">>>> END >>>>     ") 

ApplyWorldScaleUV()

If I open a belnder file, open this script in the text editor and run it, it works. But I've got 100's of files, so I want to automate it. When I try to run it from a command line (Windows 7 FYI) using:

blender.exe example.blend -P C:\temp\realWorldUV.py

I get the error:

RuntimeError: Error: Traceback (most recent call last):
  File "C:\Program Files\Blender Foundation\Blender\2.78\scripts\addons\uv_magic_uv\muv_wsuv_ops.py", line 127, in execute
    orig_area = bpy.context.area.type
AttributeError: 'NoneType' object has no attribute 'type'

I'm quite confused by all the context issues, and I've been working on this for over a day.

Is there a way I can get this to work?

$\endgroup$
  • $\begingroup$ Try https://docs.python.org/3/library/__main__.html $\endgroup$ – Strapicarus Aug 10 '17 at 17:53
  • $\begingroup$ I did this, but it's still the same error. if __name__ == "__main__": ApplyWorldScaleUV() $\endgroup$ – Ryan Foss Aug 10 '17 at 19:09
  • $\begingroup$ You might have to overwrite context when running from cmd. Check https://docs.blender.org/api/current/bpy.ops.html#overriding-context $\endgroup$ – Strapicarus Aug 10 '17 at 21:02
  • $\begingroup$ Can you try wrapping the ApplyWorldScaleUV() like this: bpy.app.handlers.render_pre.append(ApplyWorldScaleUV). Not sure, but this might be because the context is not right at the point of execution. $\endgroup$ – Wouter Vandenneucker Aug 14 '17 at 10:46
  • $\begingroup$ Tried wrapping it and it stops the error, but it also doesn't do it. It does't output my print messages either. $\endgroup$ – Ryan Foss Aug 17 '17 at 19:34

Your Answer

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

Browse other questions tagged or ask your own question.