2
$\begingroup$

As written in the title and kindly need help, my script is almost ready but I have some troubles because it stores only the first element on the collection under its named from file. I'm pretty new to python, however.enter image description here Using the MapsImporter Plugin for scraping Google Maps from RenderDOC into Blender.

Original Question: https://github.com/eliemichel/MapsModelsImporter/issues/128

import bpy
import os
import sys
import glob

directory_im = 'C:/LowPoly2/'
files = glob.glob(directory_im + ".rdc")
for f in files:
    head, tail = os.path.split(f)
    collection_name = tail.replace('.rdc', '')
    bpy.ops.import_rdc.google_maps(filepath=(f), filter_glob=".rdc", max_blocks=-1)
    myCol = bpy.data.collections.new(collection_name)
    bpy.context.scene.collection.children.link(myCol)
    for ob in bpy.context.selected_objects:
        myCol.objects.link(ob)
$\endgroup$

2 Answers 2

2
$\begingroup$

OK, so this code works with the test data on my rig:

import bpy
import os
import sys
import glob

directory_im = 'C:\\LowPoly2\\'
files = glob.glob(directory_im + "*.rdc")

# helper method to create a new LayerCollection
# also setting it to the active LayerCollection
# the Google Maps importer then puts the objects into that LayerCollection automatically
def create_coll(parent_layer_collection, collection_name):
    new_col = bpy.data.collections.new(collection_name)
    parent_layer_collection.collection.children.link(new_col)
    new_child_layer_coll = parent_layer_collection.children.get(new_col.name)
    
    bpy.context.view_layer.active_layer_collection = new_child_layer_coll
    
    return new_child_layer_coll
    

# create a master collection to batch import the files to, or re-use the currently active collection in the scene - up to you
master_collection =  bpy.context.view_layer.active_layer_collection

# iterate over files
for f in files:
    head, tail = os.path.split(f)
    collection_name = tail.replace('.rdc', '')
    
    # create a new LayerCollection to import the objects to
    # I am storing the reference to it here in case you need to run further actions on it
    # e.g. changing color etc etc
    myCol = create_coll(master_collection, collection_name)

    # run the importer, it will work within the new sub-collection
    bpy.ops.import_rdc.google_maps(filepath=(f), filter_glob=".rdc", max_blocks=-1)

It becomes rather tricky because you need to deal with both Collections and LayerCollections, to be able to set an active layer collection. The first function creates just that, and returns a LayerCollection to be used, which is also set as active collection on the way. By doing that, the importer automatically places its objects in there "for free".

$\endgroup$
0
$\begingroup$

I'm focusing on a multiple obj import and need to strenghten a bit the routine you published. The idea is:

Import OBJ recursively scanning on multiple subfolders, ONE by ONE

Apply Lily Texture Packer Plugin for each object imported and link to newly created material

Delete any other unused material

Save Lily Textured file .png in the same folder

Export a new FBX into the native folder

(I saw that the tiles imported are shrinking from 500mb to 60mb, doing by hand, and it would be useful for massive import, i'm on 200 tiles at the moment).

import bpy
import os
import sys
from bpy_extras.io_utils import ImportHelper
from bpy.props import (BoolProperty,
                       FloatProperty,
                       StringProperty,
                       EnumProperty,
                       CollectionProperty
                       )
import glob



bpy.ops.outliner.orphans_purge()
directory_im = './3042726071604147-20-894/'
files = glob.glob(directory_im + "*.obj")


# helper method to create a new LayerCollection
# also setting it to the active LayerCollection
# the Google Maps importer then puts the objects into that LayerCollection automatically

def create_coll(parent_layer_collection, collection_name):
    new_col = bpy.data.collections.new(collection_name)
    parent_layer_collection.collection.children.link(new_col)
    new_child_layer_coll = parent_layer_collection.children[new_col.name]

    bpy.context.view_layer.active_layer_collection = new_child_layer_coll
    return new_child_layer_coll
    
# create a master collection to batch import the files to, or re-use the currently active collection in the 
scene - up to you

master_collection =  bpy.context.view_layer.active_layer_collection

# iterate over files

for f in files:
    head, tail = os.path.split(f)
    collection_name = tail.replace('.obj', '')
    
    # create a new LayerCollection to import the objects to
    # I am toring the reference to it here in case you need to run further actions on it
    # e.g. changing color etc etc

    myCol = create_coll(master_collection, collection_name)

    # run the importer, it will work within the new sub-collection
    
    #bpy.ops.import_rdc.google_maps(filepath=(f), filter_glob=".rdc", max_blocks=-1)
    bpy.ops.import_scene.obj(filepath=f, filter_glob="*.obj;*.mtl", use_image_search=True, axis_forward='-Z', axis_up='Y')
    
    
bpy.ops.object.lily_texture_packer(spacing=0)  
obj_object = bpy.context.selected_objects[0] ####<--Fix

#for SlcObj in obj_select:
#    bpy.ops.object.material_slot_select(mat)
#    bpy.ops.object.make_links_data(type='Material')
    
    
ob = bpy.context.active_object

# Get material
mat = bpy.data.materials.get("Material")
if mat is None:
    # create material
    mat = bpy.data.materials.new(name="Material")

# Assign it to object
if ob.data.materials:
    # assign to 1st material slot
    ob.data.materials[0] = mat
else:
    # no slots
    ob.data.materials.append(mat)
    
    
print('Imported Collection: ', obj_object.name)
    
    # bpy.ops.export_scene.fbx(bpy.ops.export_scene.fbx(filepath=...)
$\endgroup$
2
  • 2
    $\begingroup$ Hello, is this an answer to the question or a call for new content ? If you need help, please use the comments or ask a new question $\endgroup$
    – Gorgious
    Commented Feb 10, 2021 at 9:45
  • $\begingroup$ Hello, this is an ask for new help. I think it would be needed as a new question $\endgroup$ Commented Feb 11, 2021 at 10:26

You must log in to answer this question.

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