5
$\begingroup$

I am trying to modify an object after importing it. This post is related to this question

import bpy
full_path_to_file = "C:\\Users\\xyz\\Documents\\test.obj"
bpy.ops.import_scene.obj(filepath=full_path_to_file)
# create the material
mat = bpy.data.materials.new('MaterialName')
mat.diffuse_color = (1.0,0.0,1.0)
mat.diffuse_shader = 'LAMBERT'
mat.diffuse_intensity = 1.0

# get the object
obj = bpy.data.objects['Cube']

# get the material
mat = bpy.data.materials['MaterialName']

# assign material to object
obj.data.materials.append(mat)

I wanted to add a material to the imported object but that is not happening. So I tried to scale it even that is not happening.

I realized that no operation is being performed on the imported objects. But the same operations are executing on the objects that are created (i.e. not imported).

Any help would be appreciated.

$\endgroup$
2
  • $\begingroup$ You try to get an object named 'Cube', are you sure that is what you want? I think the imported object should be named 'test', like the .obj file. $\endgroup$
    – maddin45
    Feb 2 '15 at 17:47
  • $\begingroup$ yeah the name I entered is right. First I just import the object, check its name and then type in the rest of the code so that wrong name isn't a problem $\endgroup$
    – Ross
    Feb 2 '15 at 17:51
7
$\begingroup$

Operators like bpy.ops.import_scene.obj() can't return the object name itself. If you know the name of the imported object(s) simply use this: bpy.data.objects['yourname'] and assign it to a variable to apply a material on it:

# assignment
obj = bpy.data.objects['yourname']
# append material
obj.data.materials.append(mat)

If you don't know the name of the the imported object(s) you have to select them after the import with something like this:

# single selection
obj_object = bpy.context.object
# multiple selection
obj_objects = bpy.context.selected_objects[:]

Here is a working example for importing multiple objects from 1 obj file. It appends a newly created material on the imported objects and prints out their names for error handling.

import bpy

full_path_to_file = "test.obj"
bpy.ops.import_scene.obj(filepath=full_path_to_file)

# make sure to get all imported objects
obj_objects = bpy.context.selected_objects[:]

# create the material
mat = bpy.data.materials.new('MaterialName')
mat.diffuse_color = (0.1,0.0,0.7)
mat.diffuse_shader = 'LAMBERT'
mat.diffuse_intensity = 1.0

# iterate through all objects
for obj in obj_objects:

    # print the name of the current obj
    print (obj.name)

    # set current object to the active one
    bpy.context.scene.objects.active = obj

    # get the generated material
    mat = bpy.data.materials['MaterialName']

    # if a material exists overwrite it
    if len(obj.data.materials):
        # assign to 1st material slot
        obj.data.materials[0] = mat

    # if there is no material append it
    else:
        obj.data.materials.append(mat)

    # move the object, if it's named "Cube"
    if (obj.name == "Cube"):
        obj.location = (0,0,10)
$\endgroup$
1
  • 1
    $\begingroup$ Thanks a lot for your help. I had been working on this for a week but without luck. Actually on importing there was a default material. Thus appednding only created the material but did not apply it to the faces. 'obj.data.materials[0] = mat' helped to get the texture on the object :) $\endgroup$
    – Ross
    Feb 3 '15 at 6:59

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.