2
$\begingroup$

I'm new to Blender (and to Python), usually using Rhino+Grasshopper. I try to create a workflow so I can automatically import an obj file into Blender, apply a new texture (I could also do it with a material which is already in the file if it's easier) and then render the object.

Import and rendering works perfectly, however creating and applying the texture doesn't work as I want... When I run it once, it does the import, but doesn't create and apply the new texture. If I run it a second time, the new geometry is going to have no texture but the old one will get the new texture...

If you've any idea, I would be super grateful.

Best

import bpy
import math
import numpy as np


#importing the geometry
file_loc = '/myFilepath/test01.obj'
imported_object = bpy.ops.import_scene.obj(filepath=file_loc)
obj_object = bpy.context.object


#clear materials
for material in bpy.data.materials:
    material.user_clear()
    bpy.data.materials.remove(material)


#activeObject = bpy.context.active_object #Set active object to variable
#mat = bpy.data.materials.new(name="MaterialName") #set new material to variable
#activeObject.data.materials.append(mat) #add the material to the object
#obj_object.active_material.diffuse_color = (0, 0, 0,0) #change color


#creating material
mat = bpy.data.materials.new(name='Material')

mat.use_nodes = True

mat_nodes = mat.node_tree.nodes
mat_links = mat.node_tree.links

mat_nodes['Principled BSDF'].inputs['Base Color'].default_value=(1,1,1,1)
mat_nodes['Principled BSDF'].inputs['Roughness'].default_value = 0.65


newObject = bpy.context.active_object

#making imported geometry as current and apply texture
newObject.data.materials.append(mat)
$\endgroup$

1 Answer 1

1
$\begingroup$

One problem is that bpy.ops.import_scene.obj does not return the imported object nor does it make the imported object the active object. If there is only one object in the obj file, then you can find it quickly as bpy.data.objects[-1].

A second problem is that while you clear the materials, you don't clear the material slots of the object. Fixing those two problems, the following will do what you want:

import bpy
import math
import numpy as np

#importing the geometry
file_loc = PATH_TO_OBJ_FILE
bpy.ops.import_scene.obj(filepath=file_loc)
# obj_object = bpy.context.object
obj_object = bpy.data.objects[-1]

# Remove all the materials from the object
obj_object.active_material_index = 0 #select the top material
for slot in obj_object.material_slots: #For all of the materials in the selected object:
    bpy.ops.object.material_slot_remove({'object': obj_object})
    
#clear materials
for material in bpy.data.materials:
    material.user_clear()
    bpy.data.materials.remove(material)

#creating material
mat = bpy.data.materials.new(name='Material')

mat.use_nodes = True

mat_nodes = mat.node_tree.nodes
mat_links = mat.node_tree.links

mat_nodes['Principled BSDF'].inputs['Base Color'].default_value=(1,1,1,1)
mat_nodes['Principled BSDF'].inputs['Roughness'].default_value = 0.65


#making imported geometry as current and apply texture
obj_object.data.materials.append(mat)

but only if there is only one object in the obj file. Otherwise you have to use a different technique to find out what objects were imported and select the one you want to modify.

$\endgroup$
2
  • 1
    $\begingroup$ Thank you very much for taking your time! It works perfectly. $\endgroup$ Mar 2 at 9:00
  • $\begingroup$ You're welcome. $\endgroup$ Mar 2 at 14:30

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.