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.
This commit is contained in:
Campbell Barton 2015-11-08 02:12:29 +11:00
parent ee84cf3650
commit 51446dc2af
3 changed files with 62 additions and 45 deletions

View File

@ -78,7 +78,7 @@ class ImportX3D(bpy.types.Operator, ImportHelper, IOX3DOrientationHelper):
).to_4x4()
keywords["global_matrix"] = global_matrix
return import_x3d.load(self, context, **keywords)
return import_x3d.load(context, **keywords)
class ExportX3D(bpy.types.Operator, ExportHelper, IOX3DOrientationHelper):
@ -156,7 +156,7 @@ class ExportX3D(bpy.types.Operator, ExportHelper, IOX3DOrientationHelper):
).to_4x4() * Matrix.Scale(self.global_scale, 4)
keywords["global_matrix"] = global_matrix
return export_x3d.save(self, context, **keywords)
return export_x3d.save(context, **keywords)
def menu_func_import(self, context):

View File

@ -1547,7 +1547,9 @@ def gzip_open_utf8(filepath, mode):
return file
def save(operator, context, filepath="",
def save(context,
filepath,
*,
use_selection=True,
use_mesh_modifiers=False,
use_triangulate=False,
@ -1557,7 +1559,7 @@ def save(operator, context, filepath="",
use_h3d=False,
global_matrix=None,
path_mode='AUTO',
name_decorations=True,
name_decorations=True
):
bpy.path.ensure_ext(filepath, '.x3dz' if use_compress else '.x3d')

View File

@ -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,