diff options
author | Campbell Barton <ideasman42@gmail.com> | 2015-11-07 18:12:29 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2015-11-07 18:14:36 +0300 |
commit | 51446dc2af8aa29ef2342ded6b10774ff626c288 (patch) | |
tree | 94e13fad21e10667cae9b0948f126588d312cb38 /io_scene_x3d/import_x3d.py | |
parent | ee84cf36506fea6866b41dcf9bf8701ff76b83f2 (diff) |
X3D Minor updates
- Pass context/scene as arguments.
- Avoid try/except on entire geometry loading (instead use dict.get).
- Use keyword-only args for load/save module functions.
- Don't pass the operator as first arg.
Diffstat (limited to 'io_scene_x3d/import_x3d.py')
-rw-r--r-- | io_scene_x3d/import_x3d.py | 97 |
1 files changed, 56 insertions, 41 deletions
diff --git a/io_scene_x3d/import_x3d.py b/io_scene_x3d/import_x3d.py index d2c18dd9..8c39fccf 100644 --- a/io_scene_x3d/import_x3d.py +++ b/io_scene_x3d/import_x3d.py @@ -2975,9 +2975,11 @@ def appearance_LoadPixelTexture(pixelTexture, ancestry): # Called from importShape to insert a data object (typically a mesh) # into the scene -def importShape_ProcessObject(vrmlname, bpydata, geom, geom_spec, node, - bpymat, has_alpha, texmtx, ancestry, - global_matrix): +def importShape_ProcessObject( + bpyscene, vrmlname, bpydata, geom, geom_spec, node, + bpymat, has_alpha, texmtx, ancestry, + global_matrix): + vrmlname += geom_spec bpydata.name = vrmlname @@ -3021,7 +3023,7 @@ def importShape_ProcessObject(vrmlname, bpydata, geom, geom_spec, node, # bpymesh.transform(getFinalMatrix(node)) bpyob = node.blendObject = bpy.data.objects.new(vrmlname, bpydata) bpyob.matrix_world = getFinalMatrix(node, None, ancestry, global_matrix) - bpy.context.scene.objects.link(bpyob).select = True + bpyscene.objects.link(bpyob).select = True if DEBUG: bpyob["source_line_no"] = geom.lineno @@ -3060,10 +3062,11 @@ geometry_importers = { 'Box': importMesh_Box, 'Cylinder': importMesh_Cylinder, 'Cone': importMesh_Cone, - 'Text': importText} + 'Text': importText, + } -def importShape(node, ancestry, global_matrix): +def importShape(bpyscene, node, ancestry, global_matrix): # Under Shape, we can only have Appearance, MetadataXXX and a geometry node def isGeometry(spec): return spec != "Appearance" and not spec.startswith("Metadata") @@ -3074,7 +3077,7 @@ def importShape(node, ancestry, global_matrix): bpyob = node.blendData = node.blendObject = bpyob.copy() # Could transform data, but better the object so we can instance the data bpyob.matrix_world = getFinalMatrix(node, None, ancestry, global_matrix) - bpy.context.scene.objects.link(bpyob).select = True + bpyscene.objects.link(bpyob).select = True return vrmlname = node.getDefName() @@ -3109,18 +3112,18 @@ def importShape(node, ancestry, global_matrix): # ccw is handled by every geometry importer separately; some # geometries are easier to flip than others - try: - bpydata = geometry_importers[geom_spec](geom, ancestry, bpyima) + geom_fn = geometry_importers.get(geom_spec) + if geom_fn is not None: + bpydata = geom_fn(geom, ancestry, bpyima) # There are no geometry importers that can legally return # no object. It's either a bpy object, or an exception - importShape_ProcessObject(vrmlname, bpydata, geom, geom_spec, - node, bpymat, tex_has_alpha, texmtx, - ancestry, global_matrix) - except KeyError: + importShape_ProcessObject( + bpyscene, vrmlname, bpydata, geom, geom_spec, + node, bpymat, tex_has_alpha, texmtx, + ancestry, global_matrix) + else: print('\tImportX3D warning: unsupported type "%s"' % geom_spec) - # except Exception as exc: - # print('\tImportX3D error: %s' % exc) # ----------------------------------------------------------------------------------- @@ -3210,7 +3213,7 @@ def importLamp_SpotLight(node, ancestry): return bpylamp, mtx -def importLamp(node, spec, ancestry, global_matrix): +def importLamp(bpyscene, node, spec, ancestry, global_matrix): if spec == 'PointLight': bpylamp, mtx = importLamp_PointLight(node, ancestry) elif spec == 'DirectionalLight': @@ -3222,7 +3225,7 @@ def importLamp(node, spec, ancestry, global_matrix): raise ValueError bpyob = node.blendData = node.blendObject = bpy.data.objects.new("TODO", bpylamp) - bpy.context.scene.objects.link(bpyob).select = True + bpyscene.objects.link(bpyob).select = True bpyob.matrix_world = getFinalMatrix(node, mtx, ancestry, global_matrix) @@ -3230,7 +3233,7 @@ def importLamp(node, spec, ancestry, global_matrix): # ----------------------------------------------------------------------------------- -def importViewpoint(node, ancestry, global_matrix): +def importViewpoint(bpyscene, node, ancestry, global_matrix): name = node.getDefName() if not name: name = 'Viewpoint' @@ -3248,17 +3251,17 @@ def importViewpoint(node, ancestry, global_matrix): mtx = Matrix.Translation(Vector(position)) * translateRotation(orientation) bpyob = node.blendData = node.blendObject = bpy.data.objects.new(name, bpycam) - bpy.context.scene.objects.link(bpyob).select = True + bpyscene.objects.link(bpyob).select = True bpyob.matrix_world = getFinalMatrix(node, mtx, ancestry, global_matrix) -def importTransform(node, ancestry, global_matrix): +def importTransform(bpyscene, node, ancestry, global_matrix): name = node.getDefName() if not name: name = 'Transform' bpyob = node.blendData = node.blendObject = bpy.data.objects.new(name, None) - bpy.context.scene.objects.link(bpyob).select = True + bpyscene.objects.link(bpyob).select = True bpyob.matrix_world = getFinalMatrix(node, None, ancestry, global_matrix) @@ -3442,21 +3445,24 @@ ROUTE champFly001.bindTime TO vpTs.set_startTime translateTimeSensor(time_node, action, ancestry) -def load_web3d(path, - PREF_FLAT=False, - PREF_CIRCLE_DIV=16, - global_matrix=None, - HELPER_FUNC=None, - ): +def load_web3d( + bpyscene, + filepath, + *, + PREF_FLAT=False, + PREF_CIRCLE_DIV=16, + global_matrix=None, + HELPER_FUNC=None + ): # Used when adding blender primitives GLOBALS['CIRCLE_DETAIL'] = PREF_CIRCLE_DIV #root_node = vrml_parse('/_Cylinder.wrl') - if path.lower().endswith('.x3d'): - root_node, msg = x3d_parse(path) + if filepath.lower().endswith('.x3d'): + root_node, msg = x3d_parse(filepath) else: - root_node, msg = vrml_parse(path) + root_node, msg = vrml_parse(filepath) if not root_node: print(msg) @@ -3484,15 +3490,15 @@ def load_web3d(path, # by an external script. - gets first pick pass if spec == 'Shape': - importShape(node, ancestry, global_matrix) + importShape(bpyscene, node, ancestry, global_matrix) elif spec in {'PointLight', 'DirectionalLight', 'SpotLight'}: - importLamp(node, spec, ancestry, global_matrix) + importLamp(bpyscene, node, spec, ancestry, global_matrix) elif spec == 'Viewpoint': - importViewpoint(node, ancestry, global_matrix) + importViewpoint(bpyscene, node, ancestry, global_matrix) elif spec == 'Transform': # Only use transform nodes when we are not importing a flat object hierarchy if PREF_FLAT == False: - importTransform(node, ancestry, global_matrix) + importTransform(bpyscene, node, ancestry, global_matrix) ''' # These are delt with later within importRoute elif spec=='PositionInterpolator': @@ -3517,7 +3523,7 @@ def load_web3d(path, node = defDict[key] if node.blendData is None: # Add an object if we need one for animation node.blendData = node.blendObject = bpy.data.objects.new('AnimOb', None) # , name) - bpy.context.scene.objects.link(node.blendObject).select = True + bpyscene.objects.link(node.blendObject).select = True if node.blendData.animation_data is None: node.blendData.animation_data_create() @@ -3555,16 +3561,21 @@ def load_web3d(path, c.parent = parent # update deps - bpy.context.scene.update() + bpyscene.update() del child_dict -def loadWithProfiler(operator, context, filepath="", global_matrix=None): +def load_with_profiler( + context, + filepath, + *, + global_matrix=None + ): import cProfile import pstats pro = cProfile.Profile() - pro.runctx("load_web3d(filepath,PREF_FLAT=True," - "PREF_CIRCLE_DIV=16,global_matrix=global_matrix,)", + pro.runctx("load_web3d(context.scene, filepath, PREF_FLAT=True, " + "PREF_CIRCLE_DIV=16, global_matrix=global_matrix)", globals(), locals()) st = pstats.Stats(pro) st.sort_stats("time") @@ -3572,10 +3583,14 @@ def loadWithProfiler(operator, context, filepath="", global_matrix=None): # st.print_callers(0.1) -def load(operator, context, filepath="", global_matrix=None): +def load(context, + filepath, + *, + global_matrix=None + ): # loadWithProfiler(operator, context, filepath, global_matrix) - load_web3d(filepath, + load_web3d(context.scene, filepath, PREF_FLAT=True, PREF_CIRCLE_DIV=16, global_matrix=global_matrix, |