0
$\begingroup$

I am using blender 3.2, am trying to execute the below steps using python script

  1. Import Rigged avatar (.glb) into Blender
  2. Select the mesh of upper human body
  3. Go to shade editor, delete existing material and add new material
  4. @shade editor, select "Principled BSDF" and use Hot key ctrl + shift + t to upload the image of texture (Node wrangler is already enabled)
  5. Adjust the scale parameters at the Mapping node
  6. Export the updated avatar in same glb format

I generated the below script for the above steps:

import bpy
bpy.ops.import_scene.gltf(filepath="C:\\Users\\ASUS\\Desktop\\stage\\STAGEGLB3.glb", files=[{"name":"STAGEGLB3.glb", "name":"STAGEGLB3.glb"}], loglevel=50)
bpy.context.space_data.shading.type = 'MATERIAL'
bpy.ops.material.new()
bpy.ops.node.select(deselect_all=True, location=(420, 240))
bpy.context.space_data.system_bookmarks_active = 1
bpy.ops.node.nw_add_textures_for_principled(filepath="C:\\Users\\ASUS\\Desktop\\brown_color.jpg", directory="C:\\Users\\ASUS\\Desktop\\", files=[{"name":"brown_color.jpg", "name":"brown_color.jpg"}], relative_path=True)
bpy.data.materials["Material.003"].node_tree.nodes["Mapping"].inputs[3].default_value[0] = 5
bpy.data.materials["Material.003"].node_tree.nodes["Mapping"].inputs[3].default_value[1] = 5
bpy.context.space_data.shading.type = 'MATERIAL'
bpy.context.space_data.system_bookmarks_active = 3
bpy.context.space_data.system_bookmarks_active = 1
bpy.context.space_data.system_bookmarks_active = 1
bpy.context.space_data.params.filename = "STAGEGLB4.glb"

The error am getting at line 3 is

AttributeError: ‘NoneType’ object has no attribute ‘space_data’

I tried with this link AttributeError: '_RestrictContext' object has no attribute 'space_data' still no luck

pls suggest the way forward.

Thanks

vij

$\endgroup$
2
  • $\begingroup$ Uninitialized context is often because Blender didn't have time to build everything correctly. Try using an application timer to delay your script a little bit (100 ms should be enough) docs.blender.org/api/current/bpy.app.timers.html $\endgroup$
    – Gorgious
    Apr 7, 2023 at 12:46
  • $\begingroup$ Thanks @Gorgious for yur timely help, yur timer has helped us... Now the error at line 7 AttributeError: module 'bpy.ops.node' has no attribute 'nw_add_textures_for_principled' can u help us here pls.. $\endgroup$ Apr 7, 2023 at 13:15

1 Answer 1

0
$\begingroup$

This issue was resolved by using timer Thanks to @Gorgious

Updated code is

def sd(message):
         bpy.ops.import_scene.gltf(filepath="C:\\Users\\STAGEGLB3.glb", files= 
         [{"name":"STAGEGLB3.glb", "name":"STAGEGLB3.glb"}], loglevel=50)

  bpy.context.space_data.shading.type = 'MATERIAL'
  bpy.app.timers.register(functools.partial(sd, "Hello"), first_interval=12.0)
  bpy.ops.material.new()
 bpy.ops.node.select(deselect_all=True, location=(420, 240))
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .